Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.SwingController;
  26. import com.dmdirc.addons.ui_swing.SwingWindowFactory;
  27. import com.dmdirc.addons.ui_swing.UIUtilities;
  28. import com.dmdirc.addons.ui_swing.components.IconManager;
  29. import com.dmdirc.addons.ui_swing.components.TreeScroller;
  30. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  31. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  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.interfaces.ActiveFrameManager;
  37. import com.dmdirc.events.FrameIconChangedEvent;
  38. import com.dmdirc.events.UnreadStatusChangedEvent;
  39. import com.dmdirc.interfaces.EventBus;
  40. import com.dmdirc.interfaces.WindowModel;
  41. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  42. import com.dmdirc.interfaces.config.ConfigChangeListener;
  43. import com.dmdirc.plugins.PluginDomain;
  44. import com.dmdirc.ui.WindowManager;
  45. import com.dmdirc.ui.messages.ColourManager;
  46. import com.dmdirc.util.LogUtils;
  47. import java.awt.Rectangle;
  48. import java.awt.event.MouseEvent;
  49. import java.io.Serializable;
  50. import java.util.Collection;
  51. import java.util.HashMap;
  52. import java.util.Map;
  53. import javax.inject.Inject;
  54. import javax.swing.JComponent;
  55. import javax.swing.JScrollPane;
  56. import javax.swing.JTree;
  57. import javax.swing.ScrollPaneConstants;
  58. import javax.swing.SwingUtilities;
  59. import javax.swing.tree.DefaultMutableTreeNode;
  60. import javax.swing.tree.DefaultTreeModel;
  61. import javax.swing.tree.MutableTreeNode;
  62. import javax.swing.tree.TreeNode;
  63. import javax.swing.tree.TreePath;
  64. import net.engio.mbassy.listener.Handler;
  65. import net.engio.mbassy.listener.Invoke;
  66. import net.miginfocom.swing.MigLayout;
  67. import org.slf4j.Logger;
  68. import org.slf4j.LoggerFactory;
  69. /**
  70. * Manages open windows in the application in a tree style view.
  71. */
  72. public class TreeFrameManager implements FrameManager, Serializable, ConfigChangeListener {
  73. private static final Logger LOG = LoggerFactory.getLogger(TreeFrameManager.class);
  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<TextFrame, 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 EventBus 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 EventBus 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. LOG.warn(LogUtils.USER_ERROR, "delServer triggered for root node {}",
  181. node, new IllegalArgumentException());
  182. } else {
  183. model.removeNodeFromParent(nodes.get(window));
  184. }
  185. synchronized (nodes) {
  186. eventBus.unsubscribe(nodes.get(window).getLabel());
  187. nodes.remove(window);
  188. }
  189. });
  190. }
  191. /**
  192. * Adds a window to the frame container.
  193. *
  194. * @param parent Parent node
  195. * @param window Window to add
  196. */
  197. public void addWindow(final MutableTreeNode parent, final TextFrame window) {
  198. UIUtilities.invokeAndWait(() -> {
  199. final NodeLabel label = new NodeLabel(window, iconManager);
  200. eventBus.subscribe(label);
  201. swingEventBus.subscribe(label);
  202. final TreeViewNode node = new TreeViewNode(label, window);
  203. synchronized (nodes) {
  204. nodes.put(window, node);
  205. }
  206. if (parent == null) {
  207. model.insertNodeInto(node, model.getRootNode());
  208. } else {
  209. model.insertNodeInto(node, parent);
  210. }
  211. tree.expandPath(new TreePath(node.getPath()).getParentPath());
  212. final Rectangle view = tree.getRowBounds(tree.getRowForPath(new TreePath(node.
  213. getPath())));
  214. if (view != null) {
  215. tree.scrollRectToVisible(new Rectangle(0, (int) view.getY(), 0, 0));
  216. }
  217. node.getLabel().unreadStatusChanged(new UnreadStatusChangedEvent(
  218. window.getContainer(),
  219. 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(window));
  283. final Collection<WindowModel> childWindows = windowManager.getChildren(window);
  284. for (WindowModel 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. 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. }