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

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