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.

TreeFrameManager.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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.tree;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  27. import com.dmdirc.addons.ui_swing.SwingController;
  28. import com.dmdirc.addons.ui_swing.SwingWindowFactory;
  29. import com.dmdirc.addons.ui_swing.UIUtilities;
  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.FrameManager;
  37. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  38. import com.dmdirc.events.FrameIconChangedEvent;
  39. import com.dmdirc.events.NotificationClearedEvent;
  40. import com.dmdirc.events.NotificationSetEvent;
  41. import com.dmdirc.events.UserErrorEvent;
  42. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  43. import com.dmdirc.interfaces.config.ConfigChangeListener;
  44. import com.dmdirc.interfaces.ui.Window;
  45. import com.dmdirc.logger.ErrorLevel;
  46. import com.dmdirc.plugins.PluginDomain;
  47. import com.dmdirc.ui.IconManager;
  48. import com.dmdirc.ui.WindowManager;
  49. import com.dmdirc.ui.messages.ColourManager;
  50. import com.dmdirc.util.colours.Colour;
  51. import java.awt.Rectangle;
  52. import java.awt.event.MouseEvent;
  53. import java.io.Serializable;
  54. import java.util.Collection;
  55. import java.util.HashMap;
  56. import java.util.Map;
  57. import javax.inject.Inject;
  58. import javax.swing.JComponent;
  59. import javax.swing.JScrollPane;
  60. import javax.swing.JTree;
  61. import javax.swing.ScrollPaneConstants;
  62. import javax.swing.SwingUtilities;
  63. import javax.swing.tree.DefaultMutableTreeNode;
  64. import javax.swing.tree.DefaultTreeModel;
  65. import javax.swing.tree.MutableTreeNode;
  66. import javax.swing.tree.TreeNode;
  67. import javax.swing.tree.TreePath;
  68. import net.miginfocom.swing.MigLayout;
  69. import net.engio.mbassy.listener.Handler;
  70. import net.engio.mbassy.listener.Invoke;
  71. /**
  72. * Manages open windows in the application in a tree style view.
  73. */
  74. public class TreeFrameManager implements FrameManager, Serializable, ConfigChangeListener {
  75. /** Serial version UID. */
  76. private static final long serialVersionUID = 5;
  77. /** node storage, used for adding and deleting nodes correctly. */
  78. private final Map<Window, TreeViewNode> nodes;
  79. /** Configuration manager. */
  80. private final AggregateConfigProvider config;
  81. /** Colour manager. */
  82. private final ColourManager colourManager;
  83. /** Factory to use to retrieve swing windows. */
  84. private final SwingWindowFactory windowFactory;
  85. /** Window manage. */
  86. private final WindowManager windowManager;
  87. /** Active frame manager. */
  88. private final ActiveFrameManager activeFrameManager;
  89. /** The event bus to post errors to. */
  90. private final DMDircMBassador eventBus;
  91. /** Swing event bus. */
  92. private final SwingEventBus swingEventBus;
  93. /** Icon manager. */
  94. private final IconManager iconManager;
  95. /** display tree. */
  96. private Tree tree;
  97. /** data model. */
  98. private TreeViewModel model;
  99. /** Tree scroller. */
  100. private TreeScroller scroller;
  101. @Inject
  102. public TreeFrameManager(final WindowManager windowManager,
  103. @GlobalConfig final AggregateConfigProvider globalConfig,
  104. @GlobalConfig final ColourManager colourManager,
  105. final ActiveFrameManager activeFrameManager,
  106. final SwingWindowFactory windowFactory,
  107. @PluginDomain(SwingController.class) final String domain,
  108. final DMDircMBassador eventBus,
  109. final SwingEventBus swingEventBus,
  110. @GlobalConfig final IconManager iconManager) {
  111. this.windowFactory = windowFactory;
  112. this.windowManager = windowManager;
  113. this.nodes = new HashMap<>();
  114. this.config = globalConfig;
  115. this.colourManager = colourManager;
  116. this.activeFrameManager = activeFrameManager;
  117. this.eventBus = eventBus;
  118. this.swingEventBus = swingEventBus;
  119. this.iconManager = iconManager;
  120. UIUtilities.invokeLater(() -> {
  121. model = new TreeViewModel(config, new TreeViewNode(null, null));
  122. tree = new Tree(this, model, swingEventBus, globalConfig, domain);
  123. tree.setCellRenderer(
  124. new TreeViewTreeCellRenderer(config, colourManager, this));
  125. tree.setVisible(true);
  126. config.addChangeListener("treeview", this);
  127. config.addChangeListener("ui", "sortrootwindows", this);
  128. config.addChangeListener("ui", "sortchildwindows", this);
  129. config.addChangeListener("ui", "backgroundcolour", this);
  130. config.addChangeListener("ui", "foregroundcolour", this);
  131. });
  132. }
  133. @Override
  134. public boolean canPositionVertically() {
  135. return true;
  136. }
  137. @Override
  138. public boolean canPositionHorizontally() {
  139. return false;
  140. }
  141. @Override
  142. public void setParent(final JComponent parent) {
  143. SwingUtilities.invokeLater(() -> {
  144. final JScrollPane scrollPane = new JScrollPane(tree);
  145. scrollPane.setAutoscrolls(true);
  146. scrollPane.setHorizontalScrollBarPolicy(
  147. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  148. parent.setVisible(false);
  149. parent.setLayout(new MigLayout("ins 0, fill"));
  150. parent.add(scrollPane, "grow");
  151. parent.setFocusable(false);
  152. parent.setVisible(true);
  153. setColours();
  154. eventBus.subscribe(this);
  155. swingEventBus.subscribe(this);
  156. redoTreeView();
  157. });
  158. }
  159. @Handler
  160. public void doAddWindow(final SwingWindowAddedEvent event) {
  161. final TextFrame parent = event.getParentWindow().orElse(null);
  162. final TextFrame window = event.getChildWindow();
  163. if (nodes.containsKey(window)) {
  164. return;
  165. }
  166. if (parent == null) {
  167. addWindow(model.getRootNode(), window);
  168. } else {
  169. addWindow(nodes.get(parent), window);
  170. }
  171. }
  172. @Handler
  173. public void doDeleteWindow(final SwingWindowDeletedEvent event) {
  174. final TextFrame window = event.getChildWindow();
  175. UIUtilities.invokeAndWait(() -> {
  176. if (nodes.get(window) == null) {
  177. return;
  178. }
  179. final DefaultMutableTreeNode node = nodes.get(window);
  180. if (node.getLevel() == 0) {
  181. eventBus.publishAsync(
  182. new UserErrorEvent(ErrorLevel.MEDIUM, new IllegalArgumentException(),
  183. "delServer triggered for root node" + node, ""));
  184. } else {
  185. model.removeNodeFromParent(nodes.get(window));
  186. }
  187. synchronized (nodes) {
  188. eventBus.unsubscribe(nodes.get(window).getLabel());
  189. nodes.remove(window);
  190. }
  191. });
  192. }
  193. /**
  194. * Adds a window to the frame container.
  195. *
  196. * @param parent Parent node
  197. * @param window Window to add
  198. */
  199. public void addWindow(final MutableTreeNode parent, final TextFrame window) {
  200. UIUtilities.invokeAndWait(() -> {
  201. final NodeLabel label = new NodeLabel(window, iconManager);
  202. eventBus.subscribe(label);
  203. swingEventBus.subscribe(label);
  204. final TreeViewNode node = new TreeViewNode(label, window);
  205. synchronized (nodes) {
  206. nodes.put(window, node);
  207. }
  208. if (parent == null) {
  209. model.insertNodeInto(node, model.getRootNode());
  210. } else {
  211. model.insertNodeInto(node, parent);
  212. }
  213. tree.expandPath(new TreePath(node.getPath()).getParentPath());
  214. final Rectangle view = tree.getRowBounds(tree.getRowForPath(new TreePath(node.
  215. getPath())));
  216. if (view != null) {
  217. tree.scrollRectToVisible(new Rectangle(0, (int) view.getY(), 0, 0));
  218. }
  219. // TODO: Should this colour be configurable?
  220. node.getLabel().notificationSet(new NotificationSetEvent(window.getContainer(),
  221. window.getContainer().getNotification().orElse(Colour.BLACK)));
  222. node.getLabel().iconChanged(new FrameIconChangedEvent(window.getContainer(),
  223. window.getContainer().getIcon()));
  224. });
  225. }
  226. /**
  227. * Returns the tree for this frame manager.
  228. *
  229. * @return Tree for the manager
  230. */
  231. public JTree getTree() {
  232. return tree;
  233. }
  234. /**
  235. * Checks for and sets a rollover node.
  236. *
  237. * @param event event to check
  238. */
  239. protected void checkRollover(final MouseEvent event) {
  240. NodeLabel node = null;
  241. if (event != null && tree.getNodeForLocation(event.getX(), event.getY()) != null) {
  242. node = tree.getNodeForLocation(event.getX(), event.getY()).getLabel();
  243. }
  244. synchronized (nodes) {
  245. for (TreeViewNode treeNode : nodes.values()) {
  246. final NodeLabel label = treeNode.getLabel();
  247. label.setRollover(label == node);
  248. }
  249. }
  250. tree.repaint();
  251. }
  252. /** Sets treeview colours. */
  253. private void setColours() {
  254. tree.setBackground(UIUtilities.convertColour(colourManager.getColourFromString(
  255. config.getOptionString("treeview", "backgroundcolour", "ui",
  256. "backgroundcolour"), null)));
  257. tree.setForeground(UIUtilities.convertColour(colourManager.getColourFromString(
  258. config.getOptionString("treeview", "foregroundcolour", "ui",
  259. "foregroundcolour"), null)));
  260. tree.repaint();
  261. }
  262. @Override
  263. public void configChanged(final String domain, final String key) {
  264. if ("sortrootwindows".equals(key) || "sortchildwindows".equals(key)) {
  265. redoTreeView();
  266. } else {
  267. setColours();
  268. }
  269. }
  270. /**
  271. * Starts the tree from scratch taking into account new sort orders.
  272. */
  273. private void redoTreeView() {
  274. UIUtilities.invokeLater(() -> {
  275. ((DefaultTreeModel) tree.getModel()).setRoot(null);
  276. ((DefaultTreeModel) tree.getModel()).setRoot(new TreeViewNode(null, null));
  277. if (scroller != null) {
  278. scroller.unregister();
  279. }
  280. scroller = new TreeTreeScroller(swingEventBus, tree);
  281. for (FrameContainer window : windowManager.getRootWindows()) {
  282. addWindow(null, windowFactory.getSwingWindow(window));
  283. final Collection<FrameContainer> childWindows = window.getChildren();
  284. for (FrameContainer childWindow : childWindows) {
  285. addWindow(nodes.get(windowFactory.getSwingWindow(window)),
  286. windowFactory.getSwingWindow(childWindow));
  287. }
  288. }
  289. if (activeFrameManager.getActiveFrame() != null) {
  290. selectionChanged(new SwingWindowSelectedEvent(activeFrameManager.getActiveFrame()));
  291. }
  292. });
  293. }
  294. @Handler(invocation = EdtHandlerInvocation.class)
  295. public void selectionChanged(final SwingWindowSelectedEvent event) {
  296. if (event.getWindow().isPresent()) {
  297. UIUtilities.invokeLater(() -> {
  298. final TreeNode[] treePath = ((DefaultTreeModel) tree.getModel())
  299. .getPathToRoot(nodes.get(event.getWindow().get()));
  300. if (treePath != null && treePath.length > 0) {
  301. final TreePath path = new TreePath(treePath);
  302. tree.setTreePath(path);
  303. tree.scrollPathToVisible(path);
  304. }
  305. });
  306. }
  307. }
  308. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  309. public void notificationSet(final NotificationSetEvent event) {
  310. synchronized (nodes) {
  311. final TreeViewNode node = nodes.get(windowFactory.getSwingWindow(event.getWindow()));
  312. if (event.getWindow() != null && node != null) {
  313. final NodeLabel label = node.getLabel();
  314. if (label != null) {
  315. label.notificationSet(event);
  316. tree.repaint();
  317. }
  318. }
  319. }
  320. }
  321. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  322. public void notificationCleared(final NotificationClearedEvent event) {
  323. synchronized (nodes) {
  324. final TreeViewNode node = nodes.get(windowFactory.getSwingWindow(event.getWindow()));
  325. if (event.getWindow() != null && node != null) {
  326. final NodeLabel label = node.getLabel();
  327. if (label != null) {
  328. label.notificationCleared();
  329. tree.repaint();
  330. }
  331. }
  332. }
  333. }
  334. }