SVPopupMenu.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2007 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); You may not
  4. // use this file except in compliance with the License. You may obtain a copy of
  5. // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
  6. // applicable law or agreed to in writing, software distributed under the
  7. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  8. // OF ANY KIND, either express or implied. See the License for the specific
  9. // language governing permissions and limitations under the License.
  10. package com.google.scrollview.ui;
  11. import com.google.scrollview.events.SVEventType;
  12. import com.google.scrollview.ui.SVMenuItem;
  13. import com.google.scrollview.ui.SVWindow;
  14. import java.awt.Component;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.util.HashMap;
  18. import javax.swing.JMenu;
  19. import javax.swing.JPopupMenu;
  20. /**
  21. * The SVPopupMenu class provides the functionality to add a popup menu to
  22. * ScrollView. Each popup menu item gets associated with a (client-defined)
  23. * command-id, which SVPopupMenu will return upon clicking it.
  24. *
  25. * @author wanke@google.com
  26. *
  27. */
  28. public class SVPopupMenu implements ActionListener {
  29. /** The root entry to add items to. */
  30. private JPopupMenu root;
  31. /** Contains a map of item name to its actual entry. */
  32. private HashMap<String, SVAbstractMenuItem> items;
  33. /** The window the menubar belongs to. */
  34. private SVWindow svWindow;
  35. /**
  36. * Create a new SVPopupMenu and associate it with a ScrollView window.
  37. *
  38. * @param sv The window our popup menu belongs to.
  39. */
  40. SVPopupMenu(SVWindow sv) {
  41. root = new JPopupMenu();
  42. svWindow = sv;
  43. items = new HashMap<String, SVAbstractMenuItem>();
  44. }
  45. /**
  46. * Add a new entry to the menubar. For these items, the server will poll the
  47. * client to ask what to do.
  48. *
  49. * @param parent The menu we add our new entry to (should have been defined
  50. * before). If the parent is "", we will add the entry to the root
  51. * (top-level).
  52. * @param name The caption of the new entry.
  53. * @param id The Id of the new entry. If it is -1, the entry will be treated
  54. * as a menu.
  55. */
  56. public void add(String parent, String name, int id) {
  57. // A duplicate entry - we just throw it away, since its already in.
  58. if (items.get(name) != null) { return; }
  59. // A new submenu at the top-level.
  60. if (parent.equals("")) {
  61. JMenu jli = new JMenu(name);
  62. SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
  63. items.put(name, mli);
  64. root.add(jli);
  65. }
  66. // A new sub-submenu.
  67. else if (id == -1) {
  68. SVAbstractMenuItem jmi = items.get(parent);
  69. JMenu jli = new JMenu(name);
  70. SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
  71. items.put(name, mli);
  72. jmi.add(jli);
  73. }
  74. // A new child entry. Add to appropriate parent.
  75. else {
  76. SVAbstractMenuItem jmi = items.get(parent);
  77. if (jmi == null) {
  78. System.out.println("ERROR: Unknown parent " + parent);
  79. System.exit(1);
  80. }
  81. SVAbstractMenuItem mli = new SVEmptyMenuItem(id, name);
  82. mli.mi.addActionListener(this);
  83. items.put(name, mli);
  84. jmi.add(mli);
  85. }
  86. }
  87. /**
  88. * Add a new entry to the menubar. In this case, we also know its value and
  89. * possibly even have a description. For these items, the server will not poll
  90. * the client to ask what to do, but just show an input dialog and send a
  91. * message with the new value.
  92. *
  93. * @param parent The menu we add our new entry to (should have been defined
  94. * before). If the parent is "", we will add the entry to the root
  95. * (top-level).
  96. * @param name The caption of the new entry.
  97. * @param id The Id of the new entry. If it is -1, the entry will be treated
  98. * as a menu.
  99. * @param value The value of the new entry.
  100. * @param desc The description of the new entry.
  101. */
  102. public void add(String parent, String name, int id, String value, String desc) {
  103. SVAbstractMenuItem jmi = items.get(parent);
  104. SVMenuItem mli = new SVMenuItem(id, name, value, desc);
  105. mli.mi.addActionListener(this);
  106. items.put(name, mli);
  107. if (jmi == null) { // add to root
  108. root.add(mli.mi);
  109. } else { // add to parent
  110. jmi.add(mli);
  111. }
  112. }
  113. /**
  114. * A click on one of the items in our menubar has occurred. Forward it
  115. * to the item itself to let it decide what happens.
  116. */
  117. public void actionPerformed(ActionEvent e) {
  118. // Get the corresponding menuitem
  119. SVAbstractMenuItem svm = items.get(e.getActionCommand());
  120. svm.performAction(svWindow, SVEventType.SVET_POPUP);
  121. }
  122. /**
  123. * Gets called by the SVEventHandler of the window to actually show the
  124. * content of the popup menu.
  125. */
  126. public void show(Component Invoker, int x, int y) {
  127. root.show(Invoker, x, y);
  128. }
  129. }