summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.test/src/main/java/net/wotonomy/test/TestController.java
blob: c91739c4cd4ae7166744015831275cf5863cdc6e (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
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
package net.wotonomy.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.TableColumn;

import net.wotonomy.control.ChildDataSource;
import net.wotonomy.control.EOEditingContext;
import net.wotonomy.control.EOGlobalID;
import net.wotonomy.control.EOKeyValueQualifier;
import net.wotonomy.control.EOQualifier;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSNotification;
import net.wotonomy.ui.EOAssociation;
import net.wotonomy.ui.EODisplayGroup;
import net.wotonomy.ui.swing.ButtonAssociation;
import net.wotonomy.ui.swing.ComboBoxAssociation;
import net.wotonomy.ui.swing.DisplayGroupActionAssociation;
import net.wotonomy.ui.swing.SliderAssociation;
import net.wotonomy.ui.swing.TableColumnAssociation;
import net.wotonomy.ui.swing.TextAssociation;
import net.wotonomy.ui.swing.TreeColumnAssociation;
import net.wotonomy.ui.swing.components.AlternatingRowCellRenderer;
import net.wotonomy.ui.swing.components.FormattedCellRenderer;
import net.wotonomy.ui.swing.components.IconCellRenderer;
import net.wotonomy.ui.swing.components.KeyableCellEditor;

/**
 * Controller for the TestPanel.
 */
public class TestController implements ActionListener {
	EODisplayGroup displayGroup;
	TestPanel panel;

	public TestController(TestPanel aPanel) {
		panel = aPanel;

		// setup display group

		displayGroup = new EODisplayGroup();
		displayGroup.setSortOrderings(new NSArray(new Object[] { "firstName", "middleName", "lastName" }));

		// fetch the data
//        displayGroup.setUsesOptimisticRefresh( true );
		displayGroup.setDataSource(new TestDataSource());
//        displayGroup.setSelectsFirstObjectAfterFetch( true );
		displayGroup.fetch();
		displayGroup.selectNext();

		displayGroup.setDelegate(this);

		// set up associations

		EOAssociation assoc;

		// table association

		TableColumn column;

		column = new TableColumn();
		column.setHeaderValue("First");
		IconCellRenderer iconRenderer = new IconCellRenderer() {
			private static final long serialVersionUID = 6287856165157518684L;
			private Icon icon = UIManager.getIcon("FileChooser.homeFolderIcon");

			@Override
			public Icon getIconForContext(JComponent container, Object value, int row, int col, boolean isSelected,
					boolean hasFocus, boolean isExpanded, boolean isLeaf) {
				return icon;
			}
		};
		iconRenderer.addActionListener(this);
		column.setCellRenderer(new AlternatingRowCellRenderer(iconRenderer));
//      column.setCellEditor( iconRenderer );
//        assoc = new TableColumnAssociation( column );
		assoc = new TreeColumnAssociation(column);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "firstName");
//new net.wotonomy.ui.swing.DisplayGroupInspector( displayGroup );
//displayGroup = new EODisplayGroup();        
//        assoc.bindAspect( EOAssociation.ChildrenAspect, displayGroup, "childList" );
		assoc.bindAspect(EOAssociation.EditableAspect, null, "true");
		assoc.bindAspect(EOAssociation.IsLeafAspect, null, "childCount");
		((TableColumnAssociation) assoc).setTable(panel.table);
		assoc.establishConnection();
		((TreeColumnAssociation) assoc).getTreeModelAssociation().setInsertingAfter(false);
		((TreeColumnAssociation) assoc).getTreeModelAssociation().setInsertingChild(false);

//		column.setCellRenderer( new AlternatingRowCellRenderer( column.getCellRenderer() ) );

		/*
		 * // test the standalone mode of the icon cell renderer panel.add(
		 * iconRenderer, java.awt.BorderLayout.SOUTH ); iconRenderer.setText(
		 * "Hello World!" ); iconRenderer.setIcon(
		 * UIManager.getIcon("FileChooser.homeFolderIcon") );
		 */

		column = new TableColumn();
		column.setHeaderValue("Middle");
		column.setCellRenderer(new AlternatingRowCellRenderer());
		assoc = new TableColumnAssociation(column);
		((TableColumnAssociation) assoc).setSortCaseSensitive(true);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "middleName");
		((TableColumnAssociation) assoc).setTable(panel.table);
		assoc.establishConnection();

		column = new TableColumn();
		column.setHeaderValue("Last");
		column.setCellRenderer(new AlternatingRowCellRenderer());
		column.setCellEditor(new KeyableCellEditor());
		assoc = new TableColumnAssociation(column);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "lastName");
		assoc.bindAspect(EOAssociation.EditableAspect, null, "true");
		((TableColumnAssociation) assoc).setTable(panel.table);
		assoc.establishConnection();

		column = new TableColumn();
		column.setHeaderValue("Created");
		FormattedCellRenderer renderer = new FormattedCellRenderer();
		renderer.setFormat(DateFormat.getDateInstance());
		column.setCellRenderer(new AlternatingRowCellRenderer(renderer));
		assoc = new TableColumnAssociation(column);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "createDate");
		((TableColumnAssociation) assoc).setTable(panel.table);
		assoc.establishConnection();

		column = new TableColumn();
		column.setHeaderValue("Special");
		column.setCellRenderer(new AlternatingRowCellRenderer(panel.table.getDefaultRenderer(Boolean.class)));
		assoc = new TableColumnAssociation(column);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "special");
		assoc.bindAspect(EOAssociation.EditableAspect, null, "true");
		((TableColumnAssociation) assoc).setTable(panel.table);
		assoc.establishConnection();

		// text associations

		assoc = new TextAssociation(panel.firstNameField);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "firstName");
//EODisplayGroup controllerDisplayGroup = new EODisplayGroup();
//controllerDisplayGroup.setObjectArray( new NSArray( this ) );
//controllerDisplayGroup.selectNext();
//assoc.bindAspect( EOAssociation.ValueAspect, controllerDisplayGroup, "filter" );
		assoc.establishConnection();

		assoc = new TextAssociation(panel.middleNameField);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "middleName");
		assoc.establishConnection();

		assoc = new TextAssociation(panel.lastNameField);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "lastName");
		assoc.bindAspect(EOAssociation.LabelAspect, displayGroup, "special");
		assoc.establishConnection();

		assoc = new ButtonAssociation(panel.checkbox);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "special");
//        assoc.bindAspect( EOAssociation.EnabledAspect, displayGroup, "special" );
//        assoc.bindAspect( EOAssociation.VisibleAspect, displayGroup, "special" );
		assoc.establishConnection();

		// combo associations

		assoc = new ComboBoxAssociation(panel.dateBox);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "createDate.date");
		// no titles aspect: uses existing combobox options
		assoc.establishConnection();

		assoc = new ComboBoxAssociation(panel.monthBox);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "createDate.month");
		EODisplayGroup monthTitlesGroup = new EODisplayGroup();
		monthTitlesGroup.setObjectArray(new NSArray(new Object[] { "January", "February", "March", "April", "May",
				"June", "July", "August", "September", "October", "November", "December" }));
		assoc.bindAspect(EOAssociation.TitlesAspect, monthTitlesGroup, "");

		EODisplayGroup monthObjectsGroup = new EODisplayGroup();
		monthObjectsGroup.setObjectArray(new NSArray(new Object[] { new Integer(0), new Integer(1), new Integer(2),
				new Integer(3), new Integer(4), new Integer(5), new Integer(6), new Integer(7), new Integer(8),
				new Integer(9), new Integer(10), new Integer(11) }));
		assoc.bindAspect(EOAssociation.ObjectsAspect, monthObjectsGroup, "");

		assoc.establishConnection();

		assoc = new ComboBoxAssociation(panel.yearBox);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "createDate.year");

		EODisplayGroup yearTitlesGroup = new EODisplayGroup();
		yearTitlesGroup.setObjectArray(new NSArray(new Object[] { "1999", "2000", "2001" }));
		assoc.bindAspect(EOAssociation.TitlesAspect, yearTitlesGroup, "");

		EODisplayGroup yearObjectsGroup = new EODisplayGroup();
		yearObjectsGroup
				.setObjectArray(new NSArray(new Object[] { new Integer(99), new Integer(100), new Integer(101) }));
		assoc.bindAspect(EOAssociation.ObjectsAspect, yearObjectsGroup, "");

		assoc.establishConnection();

		assoc = new SliderAssociation(panel.slider);
		assoc.bindAspect(EOAssociation.ValueAspect, displayGroup, "createDate.date");
		assoc.bindAspect(EOAssociation.VisibleAspect, displayGroup, "special");
		assoc.establishConnection();

		assoc = new TextAssociation(panel.infoPanel.getLabelForKey("Day of Month"));
		assoc.bindAspect(EOAssociation.VisibleAspect, displayGroup, "special");
		assoc.establishConnection();

		// display group action associations
		AbstractButton button;

		button = (AbstractButton) panel.savePanel.getButton("Refresh All");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) { // panel.lastNameField.setText(
															// panel.lastNameField.getText().trim() );

				// test all three ways

				displayGroup.dataSource().editingContext().invalidateAllObjects();
//				displayGroup.dataSource().editingContext().parentObjectStore().invalidateAllObjects();	
//				displayGroup.dataSource().editingContext().revert(); 
			}
		});

		button = (AbstractButton) panel.savePanel.getButton("Commit");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					displayGroup.dataSource().editingContext().saveChanges();
				} catch (RuntimeException exc) {
					JOptionPane.showMessageDialog((java.awt.Component) evt.getSource(), exc.getMessage());
					exc.printStackTrace();
				}
			}
		});

		button = (AbstractButton) panel.buttonPanel.getButton("Add");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				displayGroup.insertNewObjectAtIndex(0);
			}
		});

		assoc = new DisplayGroupActionAssociation(panel.buttonPanel.getButton("Remove"));
		assoc.bindAspect(EOAssociation.ActionAspect, displayGroup, "deleteSelection");
		assoc.establishConnection();
		/*
		 * assoc = new DisplayGroupActionAssociation(
		 * panel.infoPanel.getButtonPanel().getButton( "Refresh" ) ); assoc.bindAspect(
		 * EOAssociation.ActionAspect, displayGroup, "updateDisplayedObjects" );
		 * assoc.establishConnection();
		 * 
		 * assoc = new DisplayGroupActionAssociation(
		 * panel.infoPanel.getButtonPanel().getButton( "Commit" ) ); assoc.bindAspect(
		 * EOAssociation.ActionAspect, displayGroup, "updateDisplayedObjects" );
		 * assoc.establishConnection();
		 */
		// add MouseListener for table
		panel.table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Object o = displayGroup.selectedObject();
					if (o != null) {
//						new InspectorController( o );
						new EditController(new ChildDataSource(displayGroup.dataSource(), o));
					}
				}
			}
		});

		// add ActionListener for tree button
		((JButton) panel.buttonPanel.getButton("Tree View")).addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				EOEditingContext parentContext = Test.editingContext;
				EOEditingContext childContext = new EOEditingContext(parentContext);

				// transpose objects to ids to faults
				List ids = new LinkedList();
				Iterator i = displayGroup.selectedObjects().iterator();
				while (i.hasNext()) {
					ids.add(parentContext.globalIDForObject(i.next()));
				}
				List objects = new LinkedList();
				i = ids.iterator();
				while (i.hasNext()) {
					objects.add(childContext.faultForGlobalID((EOGlobalID) i.next(), childContext));
				}

				EODisplayGroup treeGroup = new EODisplayGroup();
				treeGroup.setSortOrderings(new NSArray("lastName"));
				treeGroup.setObjectArray(objects);

				EODisplayGroup childGroup = new EODisplayGroup();

				// childGroup.setDelegate( new DebuggingDelegate() );
				// new TreeInspectorController( treeGroup, childGroup );
				// new BindingController( treeGroup, childGroup );

				new TreeController(childContext, treeGroup, childGroup);

				// NOTE: ChildDataSource is fundamentally broken
//				new TreeController( new ChildDataSource(
//                    displayGroup.dataSource(), displayGroup.selectedObjects() ) );	
			}
		});

		/*
		 * NSNotificationCenter.defaultCenter().addObserver( this, new NSSelector(
		 * "hitMe", new Class[] { NSNotification.class } ), null, null );
		 */
	}

	private String filter;

	public String getFilter() {
		return filter;
	}

	public void setFilter(String aFilter) {
		filter = aFilter;

		EOQualifier qualifier = null;
		if (!"".equals(aFilter)) {
			qualifier = new EOKeyValueQualifier("firstName", EOQualifier.QualifierOperatorContains, filter);
		}
		displayGroup.setQualifier(qualifier);
		displayGroup.updateDisplayedObjects();
	}

	public void hitMe(NSNotification aNote) {
		System.out.println(aNote);
	}

	public void actionPerformed(ActionEvent evt) {
		Object o = displayGroup.selectedObject();
		if (o != null) {
//            new ObjectInspector( o );
			new InspectorController(o);
		}
	}

}