summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/TreeColumnAssociation.java
blob: 26f2eda79bf3bd37c7eae2720d1be8e3b0eb33b4 (plain)
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
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2002 Intersect Software Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing;

import javax.swing.JTree;
import javax.swing.table.TableColumn;

import net.wotonomy.foundation.NSArray;
import net.wotonomy.ui.EODisplayGroup;
import net.wotonomy.ui.swing.components.TreeTableCellRenderer;

/**
 * TreeColumnAssociation is a TableColumnAssocation that works like a
 * TreeAssociation, allowing any table to display hierarchical data in a tabular
 * format. This class is mainly a convenience for connecting a TreeAssociation
 * to a JTree to a TreeTableCellRenderer to a TableColumn.<br>
 * <br>
 * 
 * Like TableColumnAssociation, you must call setTable() to specify the JTable
 * to be used. (The corresponding table association will direct all column
 * header sorting to the root node of the tree association.)
 *
 * You may also optionally call setTree() to specify a customized JTree to be
 * used. If not specified, a slightly customized JTree will be used (see
 * createTree() and configureColumn()).
 * 
 * TreeColumnAssociation supports the following bindings, just as
 * TableColumnAssociation does:
 * <ul>
 * <li>value: a property convertable to a string for display in the cells of the
 * table column. This binding is equivalent to the titles binding of
 * TreeAssociation.</li>
 *
 * <li>editable: a property convertable to a boolean that determines the
 * editability of the corresponding cells in the column.</li>
 * </ul>
 *
 * TreeColumnAssociation additionally supports the following bindings, just as
 * TreeAssociation does, except that the value binding is used instead of the
 * titles binding.
 * <ul>
 * <li>children: a property of a node value that returns zero, one or many
 * objects, each of which will correspond to a child node for the corresponding
 * node in the tree. If this aspect is not bound, the tree behaves like a
 * list.</li>
 *
 * <li>isLeaf: a property of a node value that returns a value convertable to a
 * boolean value. If the isLeaf aspect is not bound, the tree will force nodes
 * to load their children to determine whether they are leaf nodes (in effect
 * loading the grandchildren for any expanded node).</li>
 * </ul>
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 904 $
 */
public class TreeColumnAssociation extends TableColumnAssociation {
	static final NSArray aspects = new NSArray(
			new Object[] { ValueAspect, EditableAspect, ChildrenAspect, IsLeafAspect });
	static final NSArray aspectSignatures = new NSArray(new Object[] { AttributeToOneAspectSignature });
	static final NSArray objectKeysTaken = new NSArray(new Object[] { "table" });

	EODisplayGroup childrenDisplayGroup, leafDisplayGroup;
	String childrenKey, leafKey;

	TreeModelAssociation treeAssociation;
	JTree tree;

	/**
	 * Constructor specifying the object to be controlled by this association.
	 * Throws an exception if the object is not a TableColumn.
	 */
	public TreeColumnAssociation(Object anObject) {
		super(anObject);
	}

	/**
	 * Returns a List of aspect signatures whose contents correspond with the
	 * aspects list. Each element is a string whose characters represent a
	 * capability of the corresponding aspect.
	 * <ul>
	 * <li>"A" attribute: the aspect can be bound to an attribute.</li>
	 * <li>"1" to-one: the aspect can be bound to a property that returns a single
	 * object.</li>
	 * <li>"M" to-one: the aspect can be bound to a property that returns multiple
	 * objects.</li>
	 * </ul>
	 * An empty signature "" means that the aspect can bind without needing a key.
	 * This implementation returns "A1M" for each element in the aspects array.
	 */
	public static NSArray aspectSignatures() {
		return aspectSignatures;
	}

	/**
	 * Returns a List that describes the aspects supported by this class. Each
	 * element in the list is the string name of the aspect. This implementation
	 * returns an empty list.
	 */
	public static NSArray aspects() {
		return aspects;
	}

	/**
	 * Binds the specified aspect of this association to the specified key on the
	 * specified display group.
	 */
	public void bindAspect(String anAspect, EODisplayGroup aDisplayGroup, String aKey) {
		if (ChildrenAspect.equals(anAspect)) {
			childrenDisplayGroup = aDisplayGroup;
			childrenKey = aKey;
		}
		if (IsLeafAspect.equals(anAspect)) {
			leafDisplayGroup = aDisplayGroup;
			leafKey = aKey;
		}
		super.bindAspect(anAspect, aDisplayGroup, aKey);
	}

	/**
	 * Overridden to call createTree if necessary, call configureColumn, call
	 * createTreeAssociation if necessary, and then call to super.
	 */
	public void establishConnection() {
		if (tree == null) {
			tree = createTree();
		}

		configureColumn((TableColumn) object());

		if (treeAssociation == null) {
			treeAssociation = createTreeAssociation(tree);
		}

		treeAssociation.bindAspect(TitlesAspect, valueDisplayGroup, valueKey);
		if (childrenKey != null) {
			treeAssociation.bindAspect(ChildrenAspect, childrenDisplayGroup, childrenKey);
		}
		if (leafKey != null) {
			treeAssociation.bindAspect(IsLeafAspect, leafDisplayGroup, leafKey);
		}

		// ensure table association's source is tree asssociation's child group
		getTableAssociation().bindAspect(SourceAspect, treeAssociation.childrenDisplayGroup, "");

		treeAssociation.establishConnection();

		table.setRowHeight(tree.getRowHeight());

		super.establishConnection();

		// cause sort ordering to apply to root node of tree
		if (childrenKey != null) {
			getTableAssociation().sortTarget = (EODisplayGroup) treeAssociation.getRoot();
		}
	}

	/**
	 * Breaks the connection between this association and its object. Override to
	 * stop listening for events from the object.
	 */
	public void breakConnection() {
		super.breakConnection();
		treeAssociation.breakConnection();

		// restore original source display group
		getTableAssociation().sortTarget = null;
	}

	/**
	 * Called by establishConnection if setTree was not called. This implementation
	 * returns a stock JTree. Override to provide your own customized JTree. Note
	 * that TreeTableCellRenderer will further customize this tree when
	 * configureColumn is called.
	 */
	protected JTree createTree() {
		return new JTree();
	}

	/**
	 * Called by establishConnection to create a tree association only if no tree
	 * association has already been created. This implementation returns a stock
	 * TreeAssociation. Override to return your own customized TreeAssociation.
	 */
	protected TreeModelAssociation createTreeAssociation(JTree aTree) {
		return new TreeAssociation(aTree);
	}

	/**
	 * Called by establishConnection to configure the column with a
	 * TreeTableCellRenderer using the current JTree. Override to further customize
	 * the column, or customize your column yourself after the call to
	 * establishConnection.
	 */
	protected void configureColumn(TableColumn aColumn) {
		aColumn.setCellRenderer(new TreeTableCellRenderer(tree));
	}

	/**
	 * Gets the JTree currently used for the column renderer. If not specified,
	 * returns null.
	 */
	public JTree getTree() {
		return tree;
	}

	/**
	 * Gets the TreeModelAssociation currently used for the tree. If not tree is not
	 * specified, returns null.
	 */
	public TreeModelAssociation getTreeModelAssociation() {
		if (tree == null)
			return null;
		if (!(tree.getModel() instanceof TreeModelAssociation))
			return null;
		return (TreeModelAssociation) tree.getModel();
	}

	/**
	 * Sets the JTree to be used for the column renderer. If not specified,
	 * createTree() will be called to create a JTree.
	 */
	public void setTree(JTree aTree) {
		tree = aTree;
	}

}

/*
 * $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven
 * dependencies.
 *
 * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.9 2003/08/06 23:07:52 chochos general code cleanup (mostly,
 * removing unused imports)
 *
 * Revision 1.8 2002/05/03 21:31:50 mpowers Actually works better without
 * selectionPaintedImmediately.
 *
 * Revision 1.7 2002/04/12 21:05:58 mpowers Now distinguishing changes in titles
 * group even better.
 *
 * Revision 1.6 2002/03/08 23:18:48 mpowers Added accessor for tree association.
 *
 * Revision 1.5 2002/03/07 23:04:36 mpowers Refining TreeColumnAssociation.
 *
 * Revision 1.4 2002/03/06 13:04:16 mpowers Implemented cascading qualifiers in
 * tree nodes.
 *
 * Revision 1.3 2002/03/05 23:18:28 mpowers Added documentation. Added
 * isSelectionPaintedImmediate and isSelectionTracking attributes to
 * TableAssociation. Added getTableAssociation to TableColumnAssociation.
 *
 * Revision 1.2 2002/03/04 22:48:22 mpowers Now working with table association
 * to sort the root node.
 *
 * Revision 1.1 2002/03/01 23:42:09 mpowers Implemented TreeColumnAssociation,
 * and updated documentation.
 *
 *
 */