Source code: javatools/swing/tree/AbstractTreeExpansionDispatcher.java
1 /*
2 * AbstractTreeExpansionDispatcher.java
3 *
4 * Created on 10 novembre 2002, 12.15
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing.tree;
26
27 import javatools.util.JQueue;
28 import javatools.swing.StatusLabelSync;
29
30 /** It is an "event dispatcher" for tree expansions, i.e. it manages in a separate
31 * thread tree expansions.
32 * You have only to inherit this method:
33 * <CODE>doDispatchOne</CODE>: what you have to do when you are expanding a node.
34 * Once created your own class, use this sequence of commands:
35 * <CODE>AbstractTreeExpansioDispatcher disp = new YourClass();
36 * disp.setStatusLabelSync(myStatusLabelSync);
37 * disp.start();
38 * disp.expand(myTree, mySelectionPath);
39 * disp.stopAll();
40 * </CODE>
41 * @author Antonio Petrelli
42 * @version 0.1.7
43 */
44 public abstract class AbstractTreeExpansionDispatcher extends java.lang.Thread {
45
46 /** Creates a new instance of AbstractTreeExpansionDispatcher */
47 public AbstractTreeExpansionDispatcher() {
48 nodeQueue = new JQueue();
49 stoppedExternally = false;
50 processing = false;
51 }
52
53 /** Sets the status label sync to use.
54 * @param sync The sync to use.
55 */
56 public void setStatusLabelSync(StatusLabelSync sync) {
57 statusSync = sync;
58 }
59
60 /** Thread's run method.
61 */
62 public void run() {
63 doDispatch();
64 }
65
66 /** Stops the thread.
67 */
68 public synchronized void stopAll() {
69 stoppedExternally = true;
70 notifyAll();
71 }
72
73 /** Expands a node.
74 * @param tree The tree that will be expanded.
75 * @param path The complete path of the node to be expanded.
76 */
77 public void expand(javax.swing.JTree tree, javax.swing.tree.TreePath path) {
78 Object[] tempObject;
79
80 tempObject = new Object[2];
81 tempObject[0] = tree;
82 tempObject[1] = path;
83 nodeQueue.put(tempObject);
84 if (!processing)
85 restartDispatch();
86 }
87
88 /** The status label sync to be used to display messages.
89 */
90 protected StatusLabelSync statusSync;
91
92 /** Inherit it to make your own operations.
93 * @param tree The tree that will be expanded.
94 * @param path The complete selection path.
95 */
96 protected abstract void doDispatchOne(javax.swing.JTree tree, javax.swing.tree.TreePath path);
97
98 private synchronized void doDispatch() {
99 javax.swing.tree.TreePath path;
100 javax.swing.JTree tree;
101 Object [] tempObject;
102
103 while (!stoppedExternally) {
104 if (nodeQueue.empty() && !stoppedExternally) {
105 try {
106 wait();
107 }
108 catch (InterruptedException e) {
109 System.out.println(e.getMessage());
110 }
111 }
112
113 if (stoppedExternally)
114 return;
115
116 while (!nodeQueue.empty()) {
117 tempObject = (Object[]) nodeQueue.get();
118 tree = (javax.swing.JTree) tempObject[0];
119 path = (javax.swing.tree.TreePath) tempObject[1];
120 //node.removeAllChildren();
121 doDispatchOne(tree, path);
122 }
123 processing = false;
124 }
125 }
126
127 private synchronized void restartDispatch() {
128 processing = true;
129 notifyAll();
130 }
131
132 private JQueue nodeQueue;
133 private boolean stoppedExternally;
134 private boolean processing;
135 }