source: trunk/gli/src/org/greenstone/gatherer/util/TreeModelTest.java@ 5170

Last change on this file since 5170 was 4686, checked in by jmt12, 21 years ago

Replaced System.err.println with Gatherer.println

  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1package org.greenstone.gatherer.util;
2import java.awt.*;
3import java.awt.event.*;
4import java.io.*;
5import java.util.*;
6import javax.swing.*;
7import javax.swing.tree.*;
8import org.greenstone.gatherer.util.DefaultSynchronizedTreeNode;
9import org.greenstone.gatherer.util.SynchronizedTreeModel;
10import org.greenstone.gatherer.util.SynchronizedTreeNode;
11
12public class TreeModelTest
13 extends JFrame {
14
15 /** Testing - test frame size. */
16 static final private Dimension SIZE = new Dimension(400,400);
17
18 /** Testing - should a debug file be written. */
19 static private boolean debug = false;
20
21 /** Testing - debug output stream. */
22 static private FileOutputStream output = null;
23
24 /** Testing - write a message to the debug file. */
25 static void debug(String message) {
26 if(debug) {
27 try {
28 if(output == null) {
29 output = new FileOutputStream(new File("debug.txt"));
30 }
31 output.write(message.getBytes());
32 output.flush();
33
34 }
35 catch (Exception error) {
36 error.printStackTrace();
37 }
38 }
39 }
40
41 /** Testing - usage message. */
42 static void printUsage() {
43 System.out.println("Usage: java Test3 [-safe] [-debug]");
44 System.out.println(" safe - use SynchronizedTreeModel");
45 System.out.println(" debug - write debug messages to debug.txt file\n");
46 System.out.println("Actions:");
47 System.out.println(" 'Add Node' - Adds a new node to the selected node. Since this happens on the AWTEvent Thread it should always be safe.");
48 System.out.println(" 'Remove Node' - Removes the selected node. Since this happens on the AWTEvent Thread it should always be safe.");
49 System.out.println(" 'Preset Structure' - Generates a small preset structure on a seperate (dangerous) thread.");
50 System.out.println(" 'Dangerous' - The ultimate test. Hammers the model with incessant random mutations from a separate thread.");
51 System.exit(0);
52 }
53 /** Testing - creates a frame containing a JTree backed by either a normal DefaultTreeModel or a SynchronizedTreeModel depending on arguments. There are also several actions you can perform by clicking on buttons. */
54 static public void main(String[] args) {
55 boolean safe = false;
56 if(args.length <= 2) {
57 for(int i = 0; i < args.length; i++) {
58 String arg = args[i];
59 if(arg.equalsIgnoreCase("-safe")) {
60 safe = true;
61 }
62 else if(arg.equalsIgnoreCase("-debug")) {
63 debug = true;
64 }
65 else {
66 printUsage();
67 }
68 }
69 }
70 else {
71 printUsage();
72 }
73 new TreeModelTest(safe);
74 }
75
76 /** Testing - a dangerous thread which hammers the mutable tree model. */
77 private DangerousTask task;
78
79 /** Testing - a dangerous thread which builds a preset structure. */
80 private DangerousTask preset_task;
81
82 /** Testing - the tree model, which may be a SynchronizedTreeModel. */
83 private DefaultTreeModel model;
84
85 /** Testing - a button for toggling the dangerous thread on and off. */
86 private JToggleButton dangerous_button;
87
88 /** Testing - the tree used during testing. */
89 private JTree tree;
90
91 TreeModelTest(boolean safe) {
92 super("Testing");
93 setDefaultCloseOperation(EXIT_ON_CLOSE);
94 setSize(SIZE);
95 JPanel content_panel = (JPanel) getContentPane();
96 // Creation
97
98 task = new DangerousTask(false);
99 preset_task = new DangerousTask(true);
100
101 if(safe) {
102 model = new SynchronizedTreeModel(new DefaultSynchronizedTreeNode("Root"));
103 }
104 else {
105 model = new DefaultTreeModel(new DefaultMutableTreeNode("Root"));
106 }
107
108 tree = new JTree(model);
109
110 JPanel button_panel = new JPanel();
111 JButton add_button = new JButton("Add Node");
112 dangerous_button = new JToggleButton("Dangerous!");
113 dangerous_button.setSelected(false);
114 JButton remove_button = new JButton("Remove Node");
115 JButton preset_button = new JButton("Preset Structure");
116
117 // Connection
118 add_button.addActionListener(new AddListener());
119 dangerous_button.addActionListener(new ActionListener() {
120 public void actionPerformed(ActionEvent event) {
121 if(dangerous_button.isSelected()) {
122 task.start();
123 }
124 else {
125 task.exit = true;
126 }
127 }
128 });
129 remove_button.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent event) {
131 ///ystem.err.println("*** Start Remove ***");
132 // Get the selected node
133 TreePath selected_path = tree.getSelectionPath();
134 if(selected_path != null) {
135 DefaultMutableTreeNode old_child = (DefaultMutableTreeNode) selected_path.getLastPathComponent();
136 if(!old_child.isRoot()) {
137 // Remove node.
138 model.removeNodeFromParent(old_child);
139 }
140 }
141 ///ystem.err.println("*** End Remove ***\n");
142 }
143 });
144 preset_button.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent event) {
146 preset_task.start();
147 }
148 });
149
150 // Layout
151 button_panel.setLayout(new GridLayout(2,2));
152 button_panel.add(add_button);
153 button_panel.add(remove_button);
154 button_panel.add(preset_button);
155 button_panel.add(dangerous_button);
156
157 content_panel.setLayout(new BorderLayout());
158 content_panel.add(new JScrollPane(tree), BorderLayout.CENTER);
159 content_panel.add(button_panel, BorderLayout.SOUTH);
160
161 // Display
162 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
163 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
164 show();
165 }
166
167 private class AddListener
168 implements ActionListener {
169 public void actionPerformed(ActionEvent event) {
170 ///ystem.err.println("*** Start Add ***");
171 // Get the selected node
172 TreePath selected_path = tree.getSelectionPath();
173 if(selected_path != null) {
174 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected_path.getLastPathComponent();
175 // Create a new node
176 MutableTreeNode new_child = new DefaultSynchronizedTreeNode( String.valueOf(System.currentTimeMillis()));
177 // Insert node.
178 model.insertNodeInto(new_child, parent, parent.getChildCount());
179 }
180 ///ystem.err.println("*** End Add ***\n");
181 }
182 }
183
184 private class DangerousTask
185 extends Thread {
186
187 private boolean exit = false;
188 private boolean preset = false;
189
190 DangerousTask(boolean preset) {
191 this.preset = preset;
192 }
193
194 public void run() {
195 if(preset) {
196 DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
197 MutableTreeNode alpha = new DefaultSynchronizedTreeNode("Alpha");
198 MutableTreeNode beta = new DefaultSynchronizedTreeNode("Beta");
199 MutableTreeNode delta = new DefaultSynchronizedTreeNode("Delta");
200 MutableTreeNode gamma = new DefaultSynchronizedTreeNode("Gamma");
201 MutableTreeNode epsilon = new DefaultSynchronizedTreeNode("Epsilon");
202 MutableTreeNode rho = new DefaultSynchronizedTreeNode("Rho");
203 MutableTreeNode pi = new DefaultSynchronizedTreeNode("Pi");
204 MutableTreeNode xhi = new DefaultSynchronizedTreeNode("Xhi");
205 MutableTreeNode kappa = new DefaultSynchronizedTreeNode("Kappa");
206 MutableTreeNode lambda = new DefaultSynchronizedTreeNode("Lambda");
207 MutableTreeNode omega = new DefaultSynchronizedTreeNode("Omega");MutableTreeNode tau = new DefaultSynchronizedTreeNode("Tau");
208
209 model.insertNodeInto(alpha, root, 0);
210 model.insertNodeInto(omega, root, 1);
211 model.insertNodeInto(pi, root, 1);
212
213 model.insertNodeInto(beta, alpha, 0);
214 model.insertNodeInto(delta, beta, 0);
215 model.insertNodeInto(epsilon, delta, 0);
216 model.insertNodeInto(gamma, delta, 0);
217 model.insertNodeInto(rho, pi, 0);
218 model.insertNodeInto(xhi, alpha, 0);
219 model.insertNodeInto(kappa, root, 1);
220
221
222 debug("Root has: " + model.getChildCount(model.getRoot()));
223 }
224 else {
225 exit = false;
226 while(!exit) {
227 // Find a random node
228 DefaultMutableTreeNode current = (DefaultMutableTreeNode) model.getRoot();
229 while(Math.random() > 0.3 && model.getChildCount(current) > 0) {
230 int index = (int)(Math.random() * (model.getChildCount(current) - 1));
231 current = (DefaultMutableTreeNode) model.getChild(current, index);
232 }
233 // Either add or remove a random node.
234 if(Math.random() > 0.9) {
235 ///ystem.err.println("<< Expand >>");
236 TreePath path = new TreePath(model.getPathToRoot(current));
237 tree.expandPath(path);
238 }
239 else if(Math.random() > 0.4 || current.isRoot()) {
240 if(model.getChildCount(current) < 25) {
241 ///ystem.err.println("<<< Add >>>");
242 // Add a new node
243 MutableTreeNode new_child = new DefaultSynchronizedTreeNode(String.valueOf(System.currentTimeMillis()));
244 // Insert node.
245 model.insertNodeInto(new_child, current, model. getChildCount(current));
246 ///ystem.err.println("<<< >>>\n");
247 }
248 }
249 else {
250 ///ystem.err.println("<<< Remove >>>");
251 // Remove node.
252 model.removeNodeFromParent(current);
253 ///ystem.err.println("<<< >>>\n");
254
255 }
256 // Wait some random minute amount of time
257 try {
258 synchronized(this) {
259 wait((int)(5 * Math.random()) + 1);
260 }
261 } catch (Exception exception) {
262 exception.printStackTrace();
263 }
264 }
265 }
266 }
267 }
268}
Note: See TracBrowser for help on using the repository browser.