You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowMenuFrameManager.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2014 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.framemanager.windowmenu;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.SwingController;
  26. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  27. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  28. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  29. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.interfaces.ui.Window;
  32. import com.dmdirc.plugins.PluginDomain;
  33. import com.google.common.collect.Maps;
  34. import java.util.Map;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. import javax.swing.AbstractButton;
  38. import javax.swing.Action;
  39. import javax.swing.JMenu;
  40. import javax.swing.JMenuItem;
  41. import net.engio.mbassy.listener.Handler;
  42. /**
  43. * Manages the window menu window list.
  44. */
  45. @Singleton
  46. public class WindowMenuFrameManager extends JMenu {
  47. /** A version number for this class. */
  48. private static final long serialVersionUID = 1;
  49. /** Window action factory. */
  50. private final WindowActionFactory windowActionFactory;
  51. /** Window selection font changer. */
  52. private final WindowSelectionFontChangerFactory windowSelectionFontChanger;
  53. /** Window to item mapping. */
  54. private final Map<Window, AbstractButton> menuItems;
  55. /** Swing event bus. */
  56. private final SwingEventBus swingEventBus;
  57. /** Close menu item. */
  58. private final CloseActiveWindowMenuItem closeMenuItem;
  59. /** Global config. */
  60. private final AggregateConfigProvider globalConfig;
  61. /** Plugin domain. */
  62. private final String domain;
  63. /** Menu separator. */
  64. private final WindowMenuSeparator separator;
  65. @Inject
  66. public WindowMenuFrameManager(@GlobalConfig final AggregateConfigProvider globalConfig,
  67. @PluginDomain(SwingController.class) final String domain,
  68. final SwingEventBus swingEventBus,
  69. final CloseActiveWindowMenuItem closeMenuItem,
  70. final WindowActionFactory windowActionFactory,
  71. final WindowSelectionFontChangerFactory windowSelectionFontChanger,
  72. final WindowMenuSeparator separator) {
  73. this.windowActionFactory = windowActionFactory;
  74. this.windowSelectionFontChanger = windowSelectionFontChanger;
  75. this.swingEventBus = swingEventBus;
  76. this.closeMenuItem = closeMenuItem;
  77. this.globalConfig = globalConfig;
  78. this.separator = separator;
  79. this.domain = domain;
  80. menuItems = Maps.newHashMap();
  81. }
  82. /**
  83. * Initialises the frame managers and adds appropriate listeners.
  84. */
  85. public void init() {
  86. swingEventBus.subscribe(this);
  87. setText("Window");
  88. setMnemonic('w');
  89. closeMenuItem.init();
  90. add(closeMenuItem);
  91. separator.init();
  92. add(separator);
  93. WindowMenuScroller.createScroller(this, globalConfig, domain, getMenuComponentCount());
  94. }
  95. @Handler(invocation = EdtHandlerInvocation.class)
  96. public void windowAdded(final SwingWindowAddedEvent event) {
  97. if (event.getParentWindow().isPresent()) {
  98. addChildWindow(event.getParentWindow().get(), event.getChildWindow());
  99. } else {
  100. addTopLevelWindow(event.getChildWindow());
  101. }
  102. }
  103. @Handler(invocation = EdtHandlerInvocation.class)
  104. public void windowDeleted(final SwingWindowDeletedEvent event) {
  105. if (event.getParentWindow().isPresent()) {
  106. deleteChildWindow(event.getParentWindow().get(), event.getChildWindow());
  107. } else {
  108. deleteTopLevelWindow(event.getChildWindow());
  109. }
  110. }
  111. private JMenu getMenu(final TextFrame window) {
  112. final Action action = windowActionFactory.getWindowAction(window);
  113. final JMenu menu = new JMenu(action);
  114. windowSelectionFontChanger.getWindowSelectionFontChanger(menu, window);
  115. return menu;
  116. }
  117. private JMenuItem getMenuItem(final TextFrame window) {
  118. final Action action = windowActionFactory.getWindowAction(window);
  119. final JMenuItem menu = new JMenuItem(action);
  120. windowSelectionFontChanger.getWindowSelectionFontChanger(menu, window);
  121. return menu;
  122. }
  123. private void addChildWindow(final TextFrame parent, final TextFrame child) {
  124. if (!(menuItems.get(parent) instanceof JMenu)) {
  125. remove(menuItems.get(parent));
  126. final AbstractButton item = getMenu(parent);
  127. menuItems.put(parent, item);
  128. add(item);
  129. }
  130. final AbstractButton item = getMenuItem(child);
  131. menuItems.put(child, item);
  132. menuItems.get(parent).add(item);
  133. }
  134. private void addTopLevelWindow(final TextFrame window) {
  135. final AbstractButton item = getMenuItem(window);
  136. menuItems.put(window, item);
  137. add(item);
  138. }
  139. private void deleteTopLevelWindow(final Window window) {
  140. remove(menuItems.get(window));
  141. }
  142. private void deleteChildWindow(final Window parent, final Window child) {
  143. menuItems.get(parent).remove(menuItems.get(child));
  144. }
  145. }