您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MenuBar.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components.menubar;
  23. import com.dmdirc.addons.ui_swing.components.MDIBar;
  24. import java.awt.Component;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. import javax.swing.Box;
  31. import javax.swing.JMenu;
  32. import javax.swing.JMenuBar;
  33. import javax.swing.JMenuItem;
  34. import net.miginfocom.layout.PlatformDefaults;
  35. import net.miginfocom.swing.MigLayout;
  36. /**
  37. * DMDirc menu bar.
  38. */
  39. @Singleton
  40. public class MenuBar extends JMenuBar {
  41. /** A version number for this class. */
  42. private static final long serialVersionUID = 1;
  43. /** Normal menu count. */
  44. private final int menuItemCount;
  45. /** Stores a list of tokens used to remove added menu items. */
  46. private final Map<String, JMenuItem> menuItems; // NOPMD
  47. /**
  48. * Instantiates a new menu bar.
  49. *
  50. * @param serverMenu The server menu to use.
  51. * @param channelMenu The channel menu to use.
  52. * @param settingsMenu The settings menu to use.
  53. * @param helpMenu The help menu to use.
  54. * @param mdiBar The MDI bar to use.
  55. */
  56. @Inject
  57. public MenuBar(
  58. final ServerMenu serverMenu,
  59. final ChannelMenu channelMenu,
  60. final SettingsMenu settingsMenu,
  61. final HelpMenu helpMenu,
  62. final MDIBar mdiBar) {
  63. menuItems = new HashMap<>();
  64. setLayout(new MigLayout("ins 0, fillx"));
  65. add(serverMenu);
  66. add(channelMenu);
  67. add(settingsMenu);
  68. add(helpMenu);
  69. final int tempCount = getComponentCount();
  70. add(Box.createHorizontalGlue(), "growx, pushx");
  71. add(mdiBar);
  72. add(Box.createHorizontalStrut(PlatformDefaults.getPanelInsets(1)
  73. .getUnit()));
  74. menuItemCount = getComponentCount() - tempCount;
  75. getActionMap().setParent(null);
  76. getActionMap().clear();
  77. }
  78. @Override
  79. protected void addImpl(final Component comp, final Object constraints,
  80. final int index) {
  81. super.addImpl(comp, constraints, getComponentCount() - menuItemCount);
  82. }
  83. /**
  84. * Adds a new menu item to a named parent menu, creating the parent menu if required.
  85. *
  86. * @param parentMenu Name of the parent menu
  87. * @param menuItem Menu item to add
  88. */
  89. public void addMenuItem(final String parentMenu, final JMenuItem menuItem) {
  90. Optional<JMenu> menu = getParentMenuItem(parentMenu);
  91. if (!menu.isPresent()) {
  92. menu = Optional.ofNullable(new JMenu(parentMenu));
  93. add(menu.get());
  94. }
  95. menu.get().add(menuItem, 0);
  96. }
  97. public void removeMenuItem(final String parentMenu, final String childItem) {
  98. final Optional<JMenu> menu = getParentMenuItem(parentMenu);
  99. if (menu.isPresent()) {
  100. final Optional<JMenuItem> menuItem = getChildItem(menu.get(), childItem);
  101. menuItem.ifPresent(jMenuItem -> menu.get().remove(jMenuItem));
  102. }
  103. }
  104. private Optional<JMenu> getParentMenuItem(final String name) {
  105. JMenu menu = null;
  106. for (int i = 0; i < getMenuCount(); i++) {
  107. menu = getMenu(i);
  108. if (menu != null && menu.getText().equals(name)) {
  109. break;
  110. }
  111. menu = null;
  112. }
  113. return Optional.ofNullable(menu);
  114. }
  115. private Optional<JMenuItem> getChildItem(final JMenu menu, final String name) {
  116. Component child = null;
  117. for (int i = 0; i < menu.getMenuComponentCount(); i++) {
  118. child = menu.getMenuComponent(i);
  119. if (child instanceof JMenuItem) {
  120. final JMenuItem childMenu = (JMenuItem) child;
  121. if (childMenu.getText().equals(name)) {
  122. break;
  123. }
  124. }
  125. child = null;
  126. }
  127. if (child == null) {
  128. return Optional.empty();
  129. } else {
  130. return Optional.ofNullable((JMenuItem) child);
  131. }
  132. }
  133. }