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ů.

TreeFrameManager.java 15KB

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