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.

CtrlTabWindowManager.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.ctrltab;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  26. import com.dmdirc.addons.ui_swing.MainFrame;
  27. import com.dmdirc.addons.ui_swing.UIUtilities;
  28. import com.dmdirc.addons.ui_swing.actions.NextFrameAction;
  29. import com.dmdirc.addons.ui_swing.actions.PreviousFrameAction;
  30. import com.dmdirc.addons.ui_swing.components.TreeScroller;
  31. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  32. import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
  33. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  34. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  35. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  36. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  37. import com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewModel;
  38. import com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewNode;
  39. import com.dmdirc.events.UserErrorEvent;
  40. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  41. import com.dmdirc.logger.ErrorLevel;
  42. import java.awt.event.InputEvent;
  43. import java.awt.event.KeyEvent;
  44. import java.util.HashMap;
  45. import java.util.Map;
  46. import java.util.Optional;
  47. import javax.inject.Inject;
  48. import javax.inject.Singleton;
  49. import javax.swing.InputMap;
  50. import javax.swing.JComponent;
  51. import javax.swing.KeyStroke;
  52. import javax.swing.tree.DefaultTreeSelectionModel;
  53. import javax.swing.tree.TreeNode;
  54. import javax.swing.tree.TreePath;
  55. import javax.swing.tree.TreeSelectionModel;
  56. import net.engio.mbassy.listener.Handler;
  57. /**
  58. * A Window manager to handle ctrl[+shift]+tab switching between windows.
  59. */
  60. @Singleton
  61. public class CtrlTabWindowManager {
  62. /** Node storage, used for adding and deleting nodes correctly. */
  63. private final Map<TextFrame, TreeViewNode> nodes;
  64. /** Data model. */
  65. private final TreeViewModel model;
  66. /** Tree Scroller. */
  67. private final TreeScroller treeScroller;
  68. /** Selection model for the tree scroller. */
  69. private final TreeSelectionModel selectionModel;
  70. /** The event bus to post errors to. */
  71. private final DMDircMBassador eventBus;
  72. @Inject
  73. public CtrlTabWindowManager(
  74. @GlobalConfig final AggregateConfigProvider globalConfig,
  75. final MainFrame mainFrame,
  76. final DMDircMBassador eventBus,
  77. final SwingEventBus swingEventBus) {
  78. this.eventBus = eventBus;
  79. nodes = new HashMap<>();
  80. model = new TreeViewModel(globalConfig, new TreeViewNode(null, null));
  81. selectionModel = new DefaultTreeSelectionModel();
  82. treeScroller = new TreeScroller(model, selectionModel, false) {
  83. @Override
  84. protected void setPath(final TreePath path) {
  85. super.setPath(path);
  86. swingEventBus.publishAsync(new SwingActiveWindowChangeRequestEvent(Optional
  87. .ofNullable(((TreeViewNode) path.getLastPathComponent()).getWindow())));
  88. }
  89. };
  90. swingEventBus.subscribe(this);
  91. if (mainFrame.getRootPane().getActionMap() != null) {
  92. mainFrame.getRootPane().getActionMap()
  93. .put("prevFrameAction", new PreviousFrameAction(treeScroller));
  94. mainFrame.getRootPane().getActionMap()
  95. .put("nextFrameAction", new NextFrameAction(treeScroller));
  96. }
  97. final InputMap ancestorFocusedMap = mainFrame.getRootPane().getInputMap(
  98. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  99. if (ancestorFocusedMap != null) {
  100. ancestorFocusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
  101. InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK), "prevFrameAction");
  102. ancestorFocusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
  103. InputEvent.CTRL_DOWN_MASK),
  104. "nextFrameAction");
  105. }
  106. }
  107. @Handler
  108. public void windowAdded(final SwingWindowAddedEvent event) {
  109. final Optional<TextFrame> parent = event.getParentWindow();
  110. final TextFrame window = event.getChildWindow();
  111. final TreeViewNode parentNode;
  112. if (parent.isPresent()) {
  113. parentNode = nodes.get(parent.get());
  114. } else {
  115. parentNode = model.getRootNode();
  116. }
  117. UIUtilities.invokeAndWait(() -> {
  118. final TreeViewNode node = new TreeViewNode(null, window);
  119. synchronized (nodes) {
  120. nodes.put(window, node);
  121. }
  122. node.setUserObject(window);
  123. model.insertNodeInto(node, parentNode);
  124. });
  125. }
  126. @Handler
  127. public void windowDeleted(final SwingWindowDeletedEvent event) {
  128. final TextFrame window = event.getChildWindow();
  129. UIUtilities.invokeAndWait(() -> {
  130. if (nodes.get(window) == null) {
  131. return;
  132. }
  133. final TreeViewNode node = nodes.get(window);
  134. if (node.getLevel() == 0) {
  135. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM,
  136. new IllegalArgumentException(),
  137. "delServer triggered for root node" + node, ""));
  138. } else {
  139. model.removeNodeFromParent(nodes.get(window));
  140. }
  141. nodes.remove(window);
  142. });
  143. }
  144. /** Scrolls up. */
  145. public void scrollUp() {
  146. treeScroller.changeFocus(true);
  147. }
  148. /** Scrolls down. */
  149. public void scrollDown() {
  150. treeScroller.changeFocus(false);
  151. }
  152. @Handler(invocation = EdtHandlerInvocation.class)
  153. public void selectionChanged(final SwingWindowSelectedEvent event) {
  154. if (event.getWindow().isPresent()) {
  155. final TreeNode[] path = model.getPathToRoot(nodes.get(event.getWindow().get()));
  156. if (path != null && path.length > 0) {
  157. selectionModel.setSelectionPath(new TreePath(path));
  158. }
  159. } else {
  160. selectionModel.setSelectionPath(null);
  161. }
  162. }
  163. }