SVMenuBar.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.SVWindow;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import java.util.HashMap;
  16. import javax.swing.JMenu;
  17. import javax.swing.JMenuBar;
  18. /**
  19. * The SVMenuBar class provides the functionality to add a menubar to
  20. * ScrollView. Each menubar item gets associated with a (client-defined)
  21. * command-id, which SVMenuBar will return upon clicking it.
  22. *
  23. * @author wanke@google.com
  24. *
  25. */
  26. public class SVMenuBar implements ActionListener {
  27. /** The root entry to add items to. */
  28. private JMenuBar root;
  29. /** Contains a map of item name to its actual entry. */
  30. private HashMap<String, SVAbstractMenuItem> items;
  31. /** The window the menubar belongs to. */
  32. private SVWindow svWindow;
  33. /**
  34. * Create a new SVMenuBar and place it at the top of the ScrollView window.
  35. *
  36. * @param scrollView The window our menubar belongs to.
  37. */
  38. public SVMenuBar(SVWindow scrollView) {
  39. root = new JMenuBar();
  40. svWindow = scrollView;
  41. items = new HashMap<String, SVAbstractMenuItem>();
  42. svWindow.setJMenuBar(root);
  43. }
  44. /**
  45. * A click on one of the items in our menubar has occurred. Forward it
  46. * to the item itself to let it decide what happens.
  47. */
  48. public void actionPerformed(ActionEvent e) {
  49. // Get the corresponding menuitem.
  50. SVAbstractMenuItem svm = items.get(e.getActionCommand());
  51. svm.performAction(svWindow, SVEventType.SVET_MENU);
  52. }
  53. /**
  54. * Add a new entry to the menubar.
  55. *
  56. * @param parent The menu we add our new entry to (should have been defined
  57. * before). If the parent is "", we will add the entry to the root
  58. * (top-level)
  59. * @param name The caption of the new entry.
  60. * @param id The Id of the new entry. If it is -1, the entry will be treated
  61. * as a menu.
  62. */
  63. public void add(String parent, String name, int id) {
  64. // A duplicate entry - we just throw it away, since its already in.
  65. if (items.get(name) != null) { return; }
  66. // A new submenu at the top-level
  67. if (parent.equals("")) {
  68. JMenu jli = new JMenu(name);
  69. SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
  70. items.put(name, mli);
  71. root.add(jli);
  72. }
  73. // A new sub-submenu
  74. else if (id == -1) {
  75. SVAbstractMenuItem jmi = items.get(parent);
  76. JMenu jli = new JMenu(name);
  77. SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
  78. items.put(name, mli);
  79. jmi.add(jli);
  80. }
  81. // A new child entry. Add to appropriate parent.
  82. else {
  83. SVAbstractMenuItem jmi = items.get(parent);
  84. if (jmi == null) {
  85. System.out.println("ERROR: Unknown parent " + parent);
  86. System.exit(1);
  87. }
  88. SVAbstractMenuItem mli = new SVEmptyMenuItem(id, name);
  89. mli.mi.addActionListener(this);
  90. items.put(name, mli);
  91. jmi.add(mli);
  92. }
  93. }
  94. /**
  95. * Add a new checkbox entry to the menubar.
  96. *
  97. * @param parent The menu we add our new entry to (should have been defined
  98. * before). If the parent is "", we will add the entry to the root
  99. * (top-level)
  100. * @param name The caption of the new entry.
  101. * @param id The Id of the new entry. If it is -1, the entry will be treated
  102. * as a menu.
  103. * @param b Whether the entry is initially flagged.
  104. *
  105. */
  106. public void add(String parent, String name, int id, boolean b) {
  107. SVAbstractMenuItem jmi = items.get(parent);
  108. if (jmi == null) {
  109. System.out.println("ERROR: Unknown parent " + parent);
  110. System.exit(1);
  111. }
  112. SVAbstractMenuItem mli = new SVCheckboxMenuItem(id, name, b);
  113. mli.mi.addActionListener(this);
  114. items.put(name, mli);
  115. jmi.add(mli);
  116. }
  117. }