Skip to main content
summaryrefslogtreecommitdiffstats
blob: 967a02783e4ecef217a825269251d64bf1bb2338 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*******************************************************************************
 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.model.value.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.iterators.EnumerationIterator;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.common.utility.internal.model.value.swing.TreeModelAdapter;
import org.eclipse.jpt.common.utility.internal.swing.Displayable;
import org.eclipse.jpt.common.utility.model.value.TreeNodeValueModel;
import org.eclipse.jpt.common.utility.model.value.WritablePropertyValueModel;
import org.eclipse.jpt.common.utility.tests.internal.model.value.swing.TreeModelAdapterTests.SortedTestNode;
import org.eclipse.jpt.common.utility.tests.internal.model.value.swing.TreeModelAdapterTests.TestModel;
import org.eclipse.jpt.common.utility.tests.internal.model.value.swing.TreeModelAdapterTests.TestNode;
import org.eclipse.jpt.common.utility.tests.internal.model.value.swing.TreeModelAdapterTests.UnsortedTestNode;

/**
 * an example UI for testing the TreeModelAdapter
 */
@SuppressWarnings("nls")
public class TreeModelAdapterUITest {

	// hold the tree so we can restore its expansion state
	private JTree tree;
	private WritablePropertyValueModel<TreeNodeValueModel<Object>> rootNodeHolder;
	private boolean sorted;
	private TreeModel treeModel;
	private TreeSelectionModel treeSelectionModel;
	private TextField nameTextField;

	public static void main(String[] args) throws Exception {
		new TreeModelAdapterUITest().exec();
	}

	private TreeModelAdapterUITest() {
		super();
	}

	private void exec() throws Exception {
		this.rootNodeHolder = this.buildRootNodeHolder();
		this.sorted = this.rootNodeHolder.getValue() instanceof SortedTestNode;
		this.treeModel = this.buildTreeModel();
		this.treeSelectionModel = this.buildTreeSelectionModel();
		this.nameTextField = new TextField();
		this.openWindow();
	}

	private WritablePropertyValueModel<TreeNodeValueModel<Object>> buildRootNodeHolder() {
		return new SimplePropertyValueModel<TreeNodeValueModel<Object>>(this.buildSortedRootNode());
	}

	private TestNode buildSortedRootNode() {
		return new SortedTestNode(this.buildRoot());
	}

	private TestNode buildUnsortedRootNode() {
		return new UnsortedTestNode(this.buildRoot());
	}

	private TestModel buildRoot() {
		TestModel root = new TestModel("root");

		TestModel node_1 = root.addChild("node 1");
		/*Node node_1_1 = */node_1.addChild("node 1.1");

		TestModel node_2 = root.addChild("node 2");
		/*Node node_2_1 = */node_2.addChild("node 2.1");
		TestModel node_2_2 = node_2.addChild("node 2.2");
		/*Node node_2_2_1 = */node_2_2.addChild("node 2.2.1");
		/*Node node_2_2_2 = */node_2_2.addChild("node 2.2.2");
		/*Node node_2_3 = */node_2.addChild("node 2.3");
		/*Node node_2_4 = */node_2.addChild("node 2.4");
		/*Node node_2_5 = */node_2.addChild("node 2.5");

		TestModel node_3 = root.addChild("node 3");
		TestModel node_3_1 = node_3.addChild("node 3.1");
		TestModel node_3_1_1 = node_3_1.addChild("node 3.1.1");
		/*Node node_3_1_1_1 = */node_3_1_1.addChild("node 3.1.1.1");

		/*Node node_4 = */root.addChild("node 4");

		return root;
	}

	private TreeModel buildTreeModel() {
		return new TreeModelAdapter<Object>(this.rootNodeHolder);
	}

	private TreeSelectionModel buildTreeSelectionModel() {
		TreeSelectionModel tsm = new DefaultTreeSelectionModel();
		tsm.addTreeSelectionListener(this.buildTreeSelectionListener());
		tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		return tsm;
	}

	private TreeSelectionListener buildTreeSelectionListener() {
		return new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				TreeModelAdapterUITest.this.treeSelectionChanged(e);
			}
		};
	}

	void treeSelectionChanged(@SuppressWarnings("unused") TreeSelectionEvent e) {
		TestModel selectedTestModel = this.selectedTestModel();
		if (selectedTestModel != null) {
			this.nameTextField.setText(selectedTestModel.getName());
		}
	}

	private void openWindow() {
		JFrame window = new JFrame(this.getClass().getName());
		window.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		window.addWindowListener(this.buildWindowListener());
		window.getContentPane().add(this.buildMainPanel(), "Center");
		window.setLocation(300, 300);
		window.setSize(400, 400);
		window.setVisible(true);
	}

	private WindowListener buildWindowListener() {
		return new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				e.getWindow().setVisible(false);
				System.exit(0);
			}
		};
	}

	private Component buildMainPanel() {
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.add(this.buildTreePane(), BorderLayout.CENTER);
		mainPanel.add(this.buildControlPanel(), BorderLayout.SOUTH);
		return mainPanel;
	}

	private Component buildTreePane() {
		return new JScrollPane(this.buildTree());
	}

	private JTree buildTree() {
		this.tree = new JTree(this.treeModel) {
			@Override
			public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
				return ((Displayable) value).displayString();
			}
		};
		this.tree.setSelectionModel(this.treeSelectionModel);
		this.tree.setRootVisible(true);
		this.tree.setShowsRootHandles(true);
		this.tree.setRowHeight(20);
		this.tree.setDoubleBuffered(true);
		return this.tree;
	}

	private Component buildControlPanel() {
		JPanel controlPanel = new JPanel(new GridLayout(0, 1));
		controlPanel.add(this.buildAddRenameNodePanel());
		controlPanel.add(this.buildMiscPanel());
		return controlPanel;
	}

	private Component buildAddRenameNodePanel() {
		JPanel addRenameNodePanel = new JPanel(new BorderLayout());
		addRenameNodePanel.add(this.buildAddButton(), BorderLayout.WEST);
		addRenameNodePanel.add(this.nameTextField, BorderLayout.CENTER);
		addRenameNodePanel.add(this.buildRenameButton(), BorderLayout.EAST);
		return addRenameNodePanel;
	}

	private Component buildMiscPanel() {
		JPanel miscPanel = new JPanel(new GridLayout(1, 0));
		miscPanel.add(this.buildClearChildrenButton());
		miscPanel.add(this.buildRemoveButton());
		miscPanel.add(this.buildResetButton());
		return miscPanel;
	}

	private String getName() {
		return this.nameTextField.getText();
	}

	// ********** queries **********
	private TestNode selectedNode() {
		if (this.treeSelectionModel.isSelectionEmpty()) {
			return null;
		}
		return (TestNode) this.treeSelectionModel.getSelectionPath().getLastPathComponent();
	}

	private TestModel selectedTestModel() {
		if (this.treeSelectionModel.isSelectionEmpty()) {
			return null;
		}
		return this.selectedNode().getValue();
	}

	private TestNode rootNode() {
		return (TestNode) this.treeModel.getRoot();
	}

	private TestModel root() {
		return this.rootNode().getValue();
	}

	private Collection<TreePath> expandedPaths() {
		Enumeration<TreePath> stream = this.tree.getExpandedDescendants(new TreePath(this.rootNode()));
		if (stream == null) {
			return Collections.emptyList();
		}
		return CollectionTools.list(new EnumerationIterator<TreePath>(stream));
	}

	// ********** behavior **********
	private void setSelectedNode(TestNode selectedNode) {
		this.treeSelectionModel.setSelectionPath(new TreePath(selectedNode.path()));
	}

	private void expandPaths(Collection<TreePath> paths) {
		for (TreePath path : paths) {
			this.tree.expandPath(path);
		}
	}

	// ********** add **********
	private JButton buildAddButton() {
		return new JButton(this.buildAddAction());
	}

	private Action buildAddAction() {
		Action action = new AbstractAction("add") {
			public void actionPerformed(ActionEvent event) {
				TreeModelAdapterUITest.this.addNode();
			}
		};
		action.setEnabled(true);
		return action;
	}

	/**
	 * adding causes the tree to be sorted and nodes to be
	 * removed and re-added; so we have to fiddle with the expansion state
	 */
	void addNode() {
		TestModel selectedTestModel = this.selectedTestModel();
		if (selectedTestModel != null) {
			String name = this.getName();
			// save the expansion state and restore it after the add
			Collection<TreePath> paths = this.expandedPaths();

			selectedTestModel.addChild(name);

			this.expandPaths(paths);
			this.setSelectedNode(this.selectedNode().childNamed(name));
		}
	}

	// ********** remove **********
	private JButton buildRemoveButton() {
		return new JButton(this.buildRemoveAction());
	}

	private Action buildRemoveAction() {
		Action action = new AbstractAction("remove") {
			public void actionPerformed(ActionEvent event) {
				TreeModelAdapterUITest.this.removeNode();
			}
		};
		action.setEnabled(true);
		return action;
	}

	/**
	 * we need to figure out which node to select after
	 * the selected node is deleted
	 */
	void removeNode() {
		TestModel selectedTestModel = this.selectedTestModel();
		// do not allow the root to be removed
		if ((selectedTestModel != null) && (selectedTestModel != this.root())) {
			// save the parent and index, so we can select another, nearby, node
			// once the selected node is removed
			TestNode parentNode = (TestNode) this.selectedNode().parent();
			int childIndex = parentNode.indexOfChild(this.selectedNode());

			selectedTestModel.getParent().removeChild(selectedTestModel);

			int childrenSize = parentNode.childrenSize();
			if (childIndex < childrenSize) {
				// select the child that moved up and replaced the just-deleted child
				this.setSelectedNode((TestNode) parentNode.child(childIndex));
			} else {
				if (childrenSize == 0) {
					// if there are no more children, select the parent
					this.setSelectedNode(parentNode);
				} else {
					// if the child at the bottom of the list was deleted, select the next child up
					this.setSelectedNode((TestNode) parentNode.child(childIndex - 1));
				}
			}
		}
	}

	// ********** rename **********
	private JButton buildRenameButton() {
		return new JButton(this.buildRenameAction());
	}

	private Action buildRenameAction() {
		Action action = new AbstractAction("rename") {
			public void actionPerformed(ActionEvent event) {
				TreeModelAdapterUITest.this.renameNode();
			}
		};
		action.setEnabled(true);
		return action;
	}

	/**
	 * renaming causes the tree to be sorted and nodes to be
	 * removed and re-added; so we have to fiddle with the expansion state
	 */
	void renameNode() {
		TestModel selectedTestModel = this.selectedTestModel();
		if (selectedTestModel != null) {
			// save the node and re-select it after the rename
			TestNode selectedNode = this.selectedNode();
			// save the expansion state and restore it after the rename
			Collection<TreePath> paths = this.expandedPaths();

			selectedTestModel.setName(this.getName());

			this.expandPaths(paths);
			this.setSelectedNode(selectedNode);
		}
	}

	// ********** clear children **********
	private JButton buildClearChildrenButton() {
		return new JButton(this.buildClearChildrenAction());
	}

	private Action buildClearChildrenAction() {
		Action action = new AbstractAction("clear children") {
			public void actionPerformed(ActionEvent event) {
				TreeModelAdapterUITest.this.clearChildren();
			}
		};
		action.setEnabled(true);
		return action;
	}

	/**
	 * nothing special, we just want to test #fireCollectionChanged(String)
	 */
	void clearChildren() {
		TestModel selectedTestModel = this.selectedTestModel();
		if (selectedTestModel != null) {
			selectedTestModel.clearChildren();
		}
	}

	// ********** reset **********
	private JButton buildResetButton() {
		return new JButton(this.buildResetAction());
	}

	private Action buildResetAction() {
		Action action = new AbstractAction("reset") {
			public void actionPerformed(ActionEvent event) {
				TreeModelAdapterUITest.this.reset();
			}
		};
		action.setEnabled(true);
		return action;
	}

	/**
	 * test the adapter's root node holder;
	 * toggle between sorted and unsorted lists
	 */
	void reset() {
		this.sorted = ! this.sorted;
		if (this.sorted) {
			this.rootNodeHolder.setValue(this.buildSortedRootNode());
		} else {
			this.rootNodeHolder.setValue(this.buildUnsortedRootNode());
		}
		this.tree.expandPath(new TreePath(this.rootNode()));
	}

}

Back to the top