Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CtrlTabWindowManager.java 7.2KB

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