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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2006-2015 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.UnreadStatusChangedEvent;
  40. import com.dmdirc.events.UserErrorEvent;
  41. import com.dmdirc.interfaces.WindowModel;
  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.addons.ui_swing.components.IconManager;
  48. import com.dmdirc.ui.WindowManager;
  49. import com.dmdirc.ui.messages.ColourManager;
  50. import java.awt.Rectangle;
  51. import java.awt.event.MouseEvent;
  52. import java.io.Serializable;
  53. import java.util.Collection;
  54. import java.util.HashMap;
  55. import java.util.Map;
  56. import javax.inject.Inject;
  57. import javax.swing.JComponent;
  58. import javax.swing.JScrollPane;
  59. import javax.swing.JTree;
  60. import javax.swing.ScrollPaneConstants;
  61. import javax.swing.SwingUtilities;
  62. import javax.swing.tree.DefaultMutableTreeNode;
  63. import javax.swing.tree.DefaultTreeModel;
  64. import javax.swing.tree.MutableTreeNode;
  65. import javax.swing.tree.TreeNode;
  66. import javax.swing.tree.TreePath;
  67. import net.miginfocom.swing.MigLayout;
  68. import net.engio.mbassy.listener.Handler;
  69. import net.engio.mbassy.listener.Invoke;
  70. /**
  71. * Manages open windows in the application in a tree style view.
  72. */
  73. public class TreeFrameManager implements FrameManager, Serializable, ConfigChangeListener {
  74. /** Serial version UID. */
  75. private static final long serialVersionUID = 5;
  76. /** node storage, used for adding and deleting nodes correctly. */
  77. private final Map<Window, TreeViewNode> nodes;
  78. /** Configuration manager. */
  79. private final AggregateConfigProvider config;
  80. /** Colour manager. */
  81. private final ColourManager colourManager;
  82. /** Factory to use to retrieve swing windows. */
  83. private final SwingWindowFactory windowFactory;
  84. /** Window manage. */
  85. private final WindowManager windowManager;
  86. /** Active frame manager. */
  87. private final ActiveFrameManager activeFrameManager;
  88. /** The event bus to post errors to. */
  89. private final DMDircMBassador eventBus;
  90. /** Swing event bus. */
  91. private final SwingEventBus swingEventBus;
  92. /** Icon manager. */
  93. private final IconManager iconManager;
  94. /** display tree. */
  95. private Tree tree;
  96. /** data model. */
  97. private TreeViewModel model;
  98. /** Tree scroller. */
  99. private TreeScroller scroller;
  100. @Inject
  101. public TreeFrameManager(final WindowManager windowManager,
  102. @GlobalConfig final AggregateConfigProvider globalConfig,
  103. @GlobalConfig final ColourManager colourManager,
  104. final ActiveFrameManager activeFrameManager,
  105. final SwingWindowFactory windowFactory,
  106. @PluginDomain(SwingController.class) final String domain,
  107. final DMDircMBassador eventBus,
  108. final SwingEventBus swingEventBus,
  109. final IconManager iconManager) {
  110. this.windowFactory = windowFactory;
  111. this.windowManager = windowManager;
  112. this.nodes = new HashMap<>();
  113. this.config = globalConfig;
  114. this.colourManager = colourManager;
  115. this.activeFrameManager = activeFrameManager;
  116. this.eventBus = eventBus;
  117. this.swingEventBus = swingEventBus;
  118. this.iconManager = iconManager;
  119. UIUtilities.invokeLater(() -> {
  120. model = new TreeViewModel(config, new TreeViewNode(null, null));
  121. tree = new Tree(this, model, swingEventBus, globalConfig, domain);
  122. tree.setCellRenderer(
  123. new TreeViewTreeCellRenderer(config, colourManager, this));
  124. tree.setVisible(true);
  125. config.addChangeListener("treeview", this);
  126. config.addChangeListener("ui", "sortrootwindows", this);
  127. config.addChangeListener("ui", "sortchildwindows", this);
  128. config.addChangeListener("ui", "backgroundcolour", this);
  129. config.addChangeListener("ui", "foregroundcolour", this);
  130. });
  131. }
  132. @Override
  133. public boolean canPositionVertically() {
  134. return true;
  135. }
  136. @Override
  137. public boolean canPositionHorizontally() {
  138. return false;
  139. }
  140. @Override
  141. public void setParent(final JComponent parent) {
  142. SwingUtilities.invokeLater(() -> {
  143. final JScrollPane scrollPane = new JScrollPane(tree);
  144. scrollPane.setAutoscrolls(true);
  145. scrollPane.setHorizontalScrollBarPolicy(
  146. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  147. parent.setVisible(false);
  148. parent.setLayout(new MigLayout("ins 0, fill"));
  149. parent.add(scrollPane, "grow");
  150. parent.setFocusable(false);
  151. parent.setVisible(true);
  152. setColours();
  153. eventBus.subscribe(this);
  154. swingEventBus.subscribe(this);
  155. redoTreeView();
  156. });
  157. }
  158. @Handler
  159. public void doAddWindow(final SwingWindowAddedEvent event) {
  160. final TextFrame parent = event.getParentWindow().orElse(null);
  161. final TextFrame window = event.getChildWindow();
  162. if (nodes.containsKey(window)) {
  163. return;
  164. }
  165. if (parent == null) {
  166. addWindow(model.getRootNode(), window);
  167. } else {
  168. addWindow(nodes.get(parent), window);
  169. }
  170. }
  171. @Handler
  172. public void doDeleteWindow(final SwingWindowDeletedEvent event) {
  173. final TextFrame window = event.getChildWindow();
  174. UIUtilities.invokeAndWait(() -> {
  175. if (nodes.get(window) == null) {
  176. return;
  177. }
  178. final DefaultMutableTreeNode node = nodes.get(window);
  179. if (node.getLevel() == 0) {
  180. eventBus.publishAsync(
  181. new UserErrorEvent(ErrorLevel.MEDIUM, new IllegalArgumentException(),
  182. "delServer triggered for root node" + node, ""));
  183. } else {
  184. model.removeNodeFromParent(nodes.get(window));
  185. }
  186. synchronized (nodes) {
  187. eventBus.unsubscribe(nodes.get(window).getLabel());
  188. nodes.remove(window);
  189. }
  190. });
  191. }
  192. /**
  193. * Adds a window to the frame container.
  194. *
  195. * @param parent Parent node
  196. * @param window Window to add
  197. */
  198. public void addWindow(final MutableTreeNode parent, final TextFrame window) {
  199. UIUtilities.invokeAndWait(() -> {
  200. final NodeLabel label = new NodeLabel(window, iconManager);
  201. eventBus.subscribe(label);
  202. swingEventBus.subscribe(label);
  203. final TreeViewNode node = new TreeViewNode(label, window);
  204. synchronized (nodes) {
  205. nodes.put(window, node);
  206. }
  207. if (parent == null) {
  208. model.insertNodeInto(node, model.getRootNode());
  209. } else {
  210. model.insertNodeInto(node, parent);
  211. }
  212. tree.expandPath(new TreePath(node.getPath()).getParentPath());
  213. final Rectangle view = tree.getRowBounds(tree.getRowForPath(new TreePath(node.
  214. getPath())));
  215. if (view != null) {
  216. tree.scrollRectToVisible(new Rectangle(0, (int) view.getY(), 0, 0));
  217. }
  218. node.getLabel().unreadStatusChanged(new UnreadStatusChangedEvent(
  219. window.getContainer(), window.getContainer().getUnreadStatusManager(),
  220. window.getContainer().getUnreadStatusManager().getNotificationColour(),
  221. window.getContainer().getUnreadStatusManager().getUnreadLines()));
  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 (WindowModel window : windowManager.getRootWindows()) {
  282. addWindow(null, windowFactory.getSwingWindow((FrameContainer) window));
  283. final Collection<WindowModel> childWindows = window.getChildren();
  284. for (WindowModel childWindow : childWindows) {
  285. addWindow(nodes.get(windowFactory.getSwingWindow((FrameContainer) window)),
  286. windowFactory.getSwingWindow((FrameContainer) 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. tree.repaint();
  305. }
  306. });
  307. }
  308. }
  309. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  310. public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
  311. synchronized (nodes) {
  312. final TreeViewNode node = nodes.get(windowFactory.getSwingWindow(event.getSource()));
  313. if (node != null) {
  314. final NodeLabel label = node.getLabel();
  315. if (label != null) {
  316. label.unreadStatusChanged(event);
  317. tree.repaint();
  318. }
  319. }
  320. }
  321. }
  322. }