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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.SwingWindowAddedEvent;
  33. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  34. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  35. import com.dmdirc.addons.ui_swing.framemanager.FrameManager;
  36. import com.dmdirc.addons.ui_swing.injection.SwingEventBus;
  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.WindowManager;
  48. import com.dmdirc.ui.messages.ColourManager;
  49. import com.dmdirc.util.colours.Colour;
  50. import com.google.common.base.Optional;
  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<FrameContainer, 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 DMDircMBassador swingEventBus;
  93. /** display tree. */
  94. private Tree tree;
  95. /** data model. */
  96. private TreeViewModel model;
  97. /** Tree scroller. */
  98. private TreeScroller scroller;
  99. /**
  100. * Creates a new instance of the TreeFrameManager.
  101. *
  102. * @param windowManager The window manager to use to read window state.
  103. * @param globalConfig The provider to read config settings from.
  104. * @param colourManager The colour manager to use to retrieve colours.
  105. * @param activeFrameManager The active window manager
  106. * @param windowFactory The factory to use to retrieve swing windows.
  107. * @param domain The domain to read settings from.
  108. * @param eventBus The event bus to post errors to
  109. * @param swingEventBus The swing event bus
  110. */
  111. @Inject
  112. public TreeFrameManager(final WindowManager windowManager,
  113. @GlobalConfig final AggregateConfigProvider globalConfig,
  114. @GlobalConfig final ColourManager colourManager, final ActiveFrameManager
  115. activeFrameManager,
  116. final SwingWindowFactory windowFactory,
  117. @PluginDomain(SwingController.class) final String domain,
  118. final DMDircMBassador eventBus, @SwingEventBus final DMDircMBassador swingEventBus) {
  119. this.windowFactory = windowFactory;
  120. this.windowManager = windowManager;
  121. this.nodes = new HashMap<>();
  122. this.config = globalConfig;
  123. this.colourManager = colourManager;
  124. this.activeFrameManager = activeFrameManager;
  125. this.eventBus = eventBus;
  126. this.swingEventBus = swingEventBus;
  127. UIUtilities.invokeLater(new Runnable() {
  128. @Override
  129. public void run() {
  130. model = new TreeViewModel(config, new TreeViewNode(null, null));
  131. tree = new Tree(TreeFrameManager.this, model, activeFrameManager, globalConfig,
  132. windowFactory, domain);
  133. tree.setCellRenderer(
  134. new TreeViewTreeCellRenderer(config, colourManager, TreeFrameManager.this));
  135. tree.setVisible(true);
  136. config.addChangeListener("treeview", TreeFrameManager.this);
  137. config.addChangeListener("ui", "sortrootwindows", TreeFrameManager.this);
  138. config.addChangeListener("ui", "sortchildwindows", TreeFrameManager.this);
  139. config.addChangeListener("ui", "backgroundcolour", TreeFrameManager.this);
  140. config.addChangeListener("ui", "foregroundcolour", TreeFrameManager.this);
  141. }
  142. });
  143. }
  144. @Override
  145. public boolean canPositionVertically() {
  146. return true;
  147. }
  148. @Override
  149. public boolean canPositionHorizontally() {
  150. return false;
  151. }
  152. @Override
  153. public void setParent(final JComponent parent) {
  154. SwingUtilities.invokeLater(new Runnable() {
  155. @Override
  156. public void run() {
  157. final JScrollPane scrollPane = new JScrollPane(tree);
  158. scrollPane.setAutoscrolls(true);
  159. scrollPane.setHorizontalScrollBarPolicy(
  160. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  161. parent.setVisible(false);
  162. parent.setLayout(new MigLayout("ins 0, fill"));
  163. parent.add(scrollPane, "grow");
  164. parent.setFocusable(false);
  165. parent.setVisible(true);
  166. setColours();
  167. eventBus.subscribe(TreeFrameManager.this);
  168. swingEventBus.subscribe(TreeFrameManager.this);
  169. redoTreeView();
  170. }
  171. });
  172. }
  173. @Handler
  174. public void doAddWindow(final SwingWindowAddedEvent event) {
  175. final TextFrame parent = event.getParentWindow().orNull();
  176. final TextFrame window = event.getChildWindow();
  177. if (nodes.containsKey(window.getContainer())) {
  178. return;
  179. }
  180. if (parent == null) {
  181. addWindow(model.getRootNode(), window.getContainer());
  182. } else {
  183. addWindow(nodes.get(parent.getContainer()), window.getContainer());
  184. }
  185. }
  186. @Handler
  187. public void doDeleteWindow(final SwingWindowDeletedEvent event) {
  188. final TextFrame window = event.getChildWindow();
  189. UIUtilities.invokeAndWait(new Runnable() {
  190. @Override
  191. public void run() {
  192. if (nodes.get(window.getContainer()) == null) {
  193. return;
  194. }
  195. final DefaultMutableTreeNode node = nodes.get(window.getContainer());
  196. if (node.getLevel() == 0) {
  197. eventBus.publishAsync(
  198. new UserErrorEvent(ErrorLevel.MEDIUM, new IllegalArgumentException(),
  199. "delServer triggered for root node" + node, ""));
  200. } else {
  201. model.removeNodeFromParent(nodes.get(window.getContainer()));
  202. }
  203. synchronized (nodes) {
  204. eventBus.unsubscribe(nodes.get(window.getContainer()).getLabel());
  205. nodes.remove(window.getContainer());
  206. }
  207. }
  208. });
  209. }
  210. /**
  211. * Adds a window to the frame container.
  212. *
  213. * @param parent Parent node
  214. * @param window Window to add
  215. */
  216. public void addWindow(final MutableTreeNode parent, final FrameContainer window) {
  217. UIUtilities.invokeAndWait(new Runnable() {
  218. @Override
  219. public void run() {
  220. final NodeLabel label = new NodeLabel(window);
  221. eventBus.subscribe(label);
  222. swingEventBus.subscribe(label);
  223. final TreeViewNode node = new TreeViewNode(label, window);
  224. synchronized (nodes) {
  225. nodes.put(window, node);
  226. }
  227. if (parent == null) {
  228. model.insertNodeInto(node, model.getRootNode());
  229. } else {
  230. model.insertNodeInto(node, parent);
  231. }
  232. tree.expandPath(new TreePath(node.getPath()).getParentPath());
  233. final Rectangle view = tree.getRowBounds(tree.getRowForPath(new TreePath(node.
  234. getPath())));
  235. if (view != null) {
  236. tree.scrollRectToVisible(new Rectangle(0, (int) view.getY(), 0, 0));
  237. }
  238. // TODO: Should this colour be configurable?
  239. node.getLabel().notificationSet(new NotificationSetEvent(window,
  240. window.getNotification().or(Colour.BLACK)));
  241. node.getLabel().iconChanged(new FrameIconChangedEvent(window, window.getIcon()));
  242. }
  243. });
  244. }
  245. /**
  246. * Returns the tree for this frame manager.
  247. *
  248. * @return Tree for the manager
  249. */
  250. public JTree getTree() {
  251. return tree;
  252. }
  253. /**
  254. * Checks for and sets a rollover node.
  255. *
  256. * @param event event to check
  257. */
  258. protected void checkRollover(final MouseEvent event) {
  259. NodeLabel node = null;
  260. if (event != null && tree.getNodeForLocation(event.getX(), event.getY()) != null) {
  261. node = tree.getNodeForLocation(event.getX(), event.getY()).getLabel();
  262. }
  263. synchronized (nodes) {
  264. for (TreeViewNode treeNode : nodes.values()) {
  265. final NodeLabel label = treeNode.getLabel();
  266. label.setRollover(label == node);
  267. }
  268. }
  269. tree.repaint();
  270. }
  271. /** Sets treeview colours. */
  272. private void setColours() {
  273. tree.setBackground(UIUtilities.convertColour(colourManager.getColourFromString(
  274. config.getOptionString("treeview", "backgroundcolour", "ui",
  275. "backgroundcolour"), null)));
  276. tree.setForeground(UIUtilities.convertColour(colourManager.getColourFromString(
  277. config.getOptionString("treeview", "foregroundcolour", "ui",
  278. "foregroundcolour"), null)));
  279. tree.repaint();
  280. }
  281. @Override
  282. public void configChanged(final String domain, final String key) {
  283. if ("sortrootwindows".equals(key) || "sortchildwindows".equals(key)) {
  284. redoTreeView();
  285. } else {
  286. setColours();
  287. }
  288. }
  289. /**
  290. * Starts the tree from scratch taking into account new sort orders.
  291. */
  292. private void redoTreeView() {
  293. UIUtilities.invokeLater(new Runnable() {
  294. @Override
  295. public void run() {
  296. ((DefaultTreeModel) tree.getModel()).setRoot(null);
  297. ((DefaultTreeModel) tree.getModel()).setRoot(new TreeViewNode(null, null));
  298. if (scroller != null) {
  299. scroller.unregister();
  300. }
  301. scroller = new TreeTreeScroller(activeFrameManager, windowFactory, tree);
  302. for (FrameContainer window : windowManager.getRootWindows()) {
  303. addWindow(null, window);
  304. final Collection<FrameContainer> childWindows = window.getChildren();
  305. for (FrameContainer childWindow : childWindows) {
  306. addWindow(nodes.get(window), childWindow);
  307. }
  308. }
  309. if (activeFrameManager.getActiveFrame() != null) {
  310. selectionChanged(new SwingWindowSelectedEvent(Optional.fromNullable((Window)
  311. activeFrameManager.getActiveFrame())));
  312. }
  313. }
  314. });
  315. }
  316. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  317. public void selectionChanged(final SwingWindowSelectedEvent event) {
  318. if (event.getWindow().isPresent()) {
  319. UIUtilities.invokeLater(new Runnable() {
  320. @Override
  321. public void run() {
  322. final TreeNode[] treePath = ((DefaultTreeModel) tree.getModel())
  323. .getPathToRoot(nodes.get(event.getWindow().get().getContainer()));
  324. if (treePath != null && treePath.length > 0) {
  325. final TreePath path = new TreePath(treePath);
  326. tree.setTreePath(path);
  327. tree.scrollPathToVisible(path);
  328. }
  329. }
  330. });
  331. }
  332. }
  333. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  334. public void notificationSet(final NotificationSetEvent event) {
  335. synchronized (nodes) {
  336. final TreeViewNode node = nodes.get(event.getWindow());
  337. if (event.getWindow() != null && node != null) {
  338. final NodeLabel label = node.getLabel();
  339. if (label != null) {
  340. label.notificationSet(event);
  341. tree.repaint();
  342. }
  343. }
  344. }
  345. }
  346. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  347. public void notificationCleared(final NotificationClearedEvent event) {
  348. synchronized (nodes) {
  349. final TreeViewNode node = nodes.get(event.getWindow());
  350. if (event.getWindow() != null && node != null) {
  351. final NodeLabel label = node.getLabel();
  352. if (label != null) {
  353. label.notificationCleared();
  354. tree.repaint();
  355. }
  356. }
  357. }
  358. }
  359. }