View Javadoc

1   package net.wotonomy.test;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import java.awt.event.MouseAdapter;
6   import java.awt.event.MouseEvent;
7   import java.text.DateFormat;
8   import java.util.Iterator;
9   import java.util.LinkedList;
10  import java.util.List;
11  
12  import javax.swing.AbstractButton;
13  import javax.swing.Icon;
14  import javax.swing.JButton;
15  import javax.swing.JComponent;
16  import javax.swing.JOptionPane;
17  import javax.swing.UIManager;
18  import javax.swing.table.TableColumn;
19  
20  import net.wotonomy.control.ChildDataSource;
21  import net.wotonomy.control.EOEditingContext;
22  import net.wotonomy.control.EOGlobalID;
23  import net.wotonomy.control.EOKeyValueQualifier;
24  import net.wotonomy.control.EOQualifier;
25  import net.wotonomy.foundation.NSArray;
26  import net.wotonomy.foundation.NSNotification;
27  import net.wotonomy.ui.EOAssociation;
28  import net.wotonomy.ui.EODisplayGroup;
29  import net.wotonomy.ui.swing.ButtonAssociation;
30  import net.wotonomy.ui.swing.ComboBoxAssociation;
31  import net.wotonomy.ui.swing.DisplayGroupActionAssociation;
32  import net.wotonomy.ui.swing.SliderAssociation;
33  import net.wotonomy.ui.swing.TableColumnAssociation;
34  import net.wotonomy.ui.swing.TextAssociation;
35  import net.wotonomy.ui.swing.TreeColumnAssociation;
36  import net.wotonomy.ui.swing.components.AlternatingRowCellRenderer;
37  import net.wotonomy.ui.swing.components.FormattedCellRenderer;
38  import net.wotonomy.ui.swing.components.IconCellRenderer;
39  import net.wotonomy.ui.swing.components.KeyableCellEditor;
40  
41  /***
42  * Controller for the TestPanel.
43  */
44  public class TestController implements ActionListener
45  {    
46  	EODisplayGroup displayGroup;
47  	TestPanel panel;
48  
49      public TestController( TestPanel aPanel )
50      {
51  		panel = aPanel;
52  		
53          // setup display group
54  
55          displayGroup = new EODisplayGroup();	
56  		displayGroup.setSortOrderings( new NSArray( new Object[]
57  			{ "firstName", "middleName", "lastName" } ) );
58  
59  		// fetch the data
60  //        displayGroup.setUsesOptimisticRefresh( true );
61          displayGroup.setDataSource( new TestDataSource() );
62  //        displayGroup.setSelectsFirstObjectAfterFetch( true );
63          displayGroup.fetch();
64          displayGroup.selectNext();
65  
66  displayGroup.setDelegate( this );
67  
68  		// set up associations
69  
70          EOAssociation assoc;
71  		
72  		// table association
73  		
74  		TableColumn column;
75  		
76  		column = new TableColumn();
77  		column.setHeaderValue( "First" );
78  		IconCellRenderer iconRenderer = new IconCellRenderer()
79  		{
80              private Icon icon = UIManager.getIcon("FileChooser.homeFolderIcon");
81              public Icon getIconForContext(
82                  JComponent container, Object value, 
83                  int row, int col,
84                  boolean isSelected, boolean hasFocus, 
85                  boolean isExpanded, boolean isLeaf )
86              {
87                  return icon;
88              }
89          };
90  		iconRenderer.addActionListener( this );
91  		column.setCellRenderer( new AlternatingRowCellRenderer( iconRenderer ) );
92  //      column.setCellEditor( iconRenderer );
93  //        assoc = new TableColumnAssociation( column );
94          assoc = new TreeColumnAssociation( column );
95          assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "firstName" );
96  //new net.wotonomy.ui.swing.DisplayGroupInspector( displayGroup );
97  //displayGroup = new EODisplayGroup();        
98  //        assoc.bindAspect( EOAssociation.ChildrenAspect, displayGroup, "childList" );
99          assoc.bindAspect( EOAssociation.EditableAspect, null, "true" );
100         assoc.bindAspect( EOAssociation.IsLeafAspect, null, "childCount" );
101 		((TableColumnAssociation)assoc).setTable( panel.table );
102 		assoc.establishConnection();
103 		((TreeColumnAssociation)assoc).getTreeModelAssociation().setInsertingAfter( false );
104 		((TreeColumnAssociation)assoc).getTreeModelAssociation().setInsertingChild( false );
105 
106 //		column.setCellRenderer( new AlternatingRowCellRenderer( column.getCellRenderer() ) );
107 
108 /*
109         // test the standalone mode of the icon cell renderer
110         panel.add( iconRenderer, java.awt.BorderLayout.SOUTH );
111         iconRenderer.setText( "Hello World!" );
112         iconRenderer.setIcon( UIManager.getIcon("FileChooser.homeFolderIcon") );
113 */        
114         
115 		column = new TableColumn();
116 		column.setHeaderValue( "Middle" );
117 		column.setCellRenderer( new AlternatingRowCellRenderer() );
118         assoc = new TableColumnAssociation( column );
119         ((TableColumnAssociation)assoc).setSortCaseSensitive( true );
120         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "middleName" );
121 		((TableColumnAssociation)assoc).setTable( panel.table );
122 		assoc.establishConnection();
123         
124 		column = new TableColumn();
125 		column.setHeaderValue( "Last" );
126 		column.setCellRenderer( new AlternatingRowCellRenderer() );
127         column.setCellEditor( new KeyableCellEditor() );
128         assoc = new TableColumnAssociation( column );
129         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "lastName" );
130         assoc.bindAspect( EOAssociation.EditableAspect, null, "true" );
131 		((TableColumnAssociation)assoc).setTable( panel.table );
132 		assoc.establishConnection();
133         
134 		column = new TableColumn();
135 		column.setHeaderValue( "Created" );
136 		FormattedCellRenderer renderer = new FormattedCellRenderer();
137 		renderer.setFormat( DateFormat.getDateInstance() );
138 		column.setCellRenderer( new AlternatingRowCellRenderer( renderer ) );
139         assoc = new TableColumnAssociation( column );
140         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "createDate" );
141 		((TableColumnAssociation)assoc).setTable( panel.table );
142 		assoc.establishConnection();
143      
144 		column = new TableColumn();
145 		column.setHeaderValue( "Special" );
146 		column.setCellRenderer( new AlternatingRowCellRenderer(
147             panel.table.getDefaultRenderer( Boolean.class ) ) );
148         assoc = new TableColumnAssociation( column );
149         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "special" );
150         assoc.bindAspect( EOAssociation.EditableAspect, null, "true" );
151 		((TableColumnAssociation)assoc).setTable( panel.table );
152 		assoc.establishConnection();
153         
154         // text associations
155 
156         assoc = new TextAssociation( panel.firstNameField );
157         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "firstName" );
158 //EODisplayGroup controllerDisplayGroup = new EODisplayGroup();
159 //controllerDisplayGroup.setObjectArray( new NSArray( this ) );
160 //controllerDisplayGroup.selectNext();
161 //assoc.bindAspect( EOAssociation.ValueAspect, controllerDisplayGroup, "filter" );
162 		assoc.establishConnection();
163 
164         assoc = new TextAssociation( panel.middleNameField );
165         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "middleName" );
166 		assoc.establishConnection();
167         
168         assoc = new TextAssociation( panel.lastNameField );
169         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "lastName" );
170         assoc.bindAspect( EOAssociation.LabelAspect, displayGroup, "special" );
171 		assoc.establishConnection();
172 		
173         assoc = new ButtonAssociation( panel.checkbox );
174         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "special" );
175 //        assoc.bindAspect( EOAssociation.EnabledAspect, displayGroup, "special" );
176 //        assoc.bindAspect( EOAssociation.VisibleAspect, displayGroup, "special" );
177 		assoc.establishConnection();
178 		
179 		// combo associations
180 		
181         assoc = new ComboBoxAssociation( panel.dateBox );
182         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "createDate.date" );
183         // no titles aspect: uses existing combobox options
184 		assoc.establishConnection();
185         
186         assoc = new ComboBoxAssociation( panel.monthBox );
187         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "createDate.month" );
188 		EODisplayGroup monthTitlesGroup = new EODisplayGroup();
189 		monthTitlesGroup.setObjectArray( new NSArray( 
190 			new Object[] { "January", "February", "March", "April", "May", "June",  
191 				"July", "August", "September", "October", "November", "December" } ) );
192 		assoc.bindAspect( EOAssociation.TitlesAspect, monthTitlesGroup, "" );
193 
194 		EODisplayGroup monthObjectsGroup = new EODisplayGroup();
195 		monthObjectsGroup.setObjectArray( new NSArray( 
196 			new Object[] { new Integer( 0 ),
197 				new Integer( 1 ), new Integer( 2 ), new Integer( 3 ), 
198 				new Integer( 4 ), new Integer( 5 ), new Integer( 6 ), 
199 				new Integer( 7 ), new Integer( 8 ), new Integer( 9 ), 
200 				new Integer( 10 ), new Integer( 11 ) } ) );
201 		assoc.bindAspect( EOAssociation.ObjectsAspect, monthObjectsGroup, "" );
202 		
203 		assoc.establishConnection();
204         		
205 
206 		assoc = new ComboBoxAssociation( panel.yearBox );
207         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "createDate.year" );
208 		
209 		EODisplayGroup yearTitlesGroup = new EODisplayGroup();
210 		yearTitlesGroup.setObjectArray( new NSArray( 
211 			new Object[] { "1999", "2000", "2001" } ) );
212 		assoc.bindAspect( EOAssociation.TitlesAspect, yearTitlesGroup, "" );
213 
214 		EODisplayGroup yearObjectsGroup = new EODisplayGroup();
215 		yearObjectsGroup.setObjectArray( new NSArray( 
216 			new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) );
217 		assoc.bindAspect( EOAssociation.ObjectsAspect, yearObjectsGroup, "" );
218 		
219 		assoc.establishConnection();
220 		
221         assoc = new SliderAssociation( panel.slider );
222         assoc.bindAspect( EOAssociation.ValueAspect, displayGroup, "createDate.date" );
223         assoc.bindAspect( EOAssociation.VisibleAspect, displayGroup, "special" );
224 		assoc.establishConnection();
225         
226         assoc = new TextAssociation( panel.infoPanel.getLabelForKey( "Day of Month" ) );
227         assoc.bindAspect( EOAssociation.VisibleAspect, displayGroup, "special" );
228 		assoc.establishConnection();
229 		
230 		// display group action associations
231         AbstractButton button;
232 
233 		button = (AbstractButton)
234 			panel.savePanel.getButton( "Refresh All" );
235 		button.addActionListener( new ActionListener()
236 		{
237 			public void actionPerformed( ActionEvent evt )
238 			{ // panel.lastNameField.setText( panel.lastNameField.getText().trim() );
239 
240                 // test all three ways
241                 
242 				displayGroup.dataSource().editingContext().invalidateAllObjects();	
243 //				displayGroup.dataSource().editingContext().parentObjectStore().invalidateAllObjects();	
244 //				displayGroup.dataSource().editingContext().revert(); 
245 			}
246 		} );
247 		
248 		button = (AbstractButton)
249 			panel.savePanel.getButton( "Commit" );
250 		button.addActionListener( new ActionListener()
251 		{
252 			public void actionPerformed( ActionEvent evt )
253 			{
254                 try
255                 {
256                     displayGroup.dataSource().editingContext().saveChanges();	
257                 }
258                 catch ( RuntimeException exc )
259                 {
260                     JOptionPane.showMessageDialog( 
261                         (java.awt.Component)evt.getSource(), exc.getMessage() );   
262                     exc.printStackTrace();
263                 }
264 			}
265 		} );
266 		
267 		button = (AbstractButton)
268 			panel.buttonPanel.getButton( "Add" );
269 		button.addActionListener( new ActionListener()
270 		{
271 			public void actionPerformed( ActionEvent evt )
272 			{
273 				displayGroup.insertNewObjectAtIndex( 0 );	
274 			}
275 		} );
276 		
277 		assoc = new DisplayGroupActionAssociation( 
278             panel.buttonPanel.getButton( "Remove" ) );
279 		assoc.bindAspect( EOAssociation.ActionAspect, displayGroup, "deleteSelection" );
280 		assoc.establishConnection();
281 /*
282 		assoc = new DisplayGroupActionAssociation( 
283             panel.infoPanel.getButtonPanel().getButton( "Refresh" ) );
284 		assoc.bindAspect( EOAssociation.ActionAspect, displayGroup, "updateDisplayedObjects" );
285 		assoc.establishConnection();
286 
287 		assoc = new DisplayGroupActionAssociation( 
288             panel.infoPanel.getButtonPanel().getButton( "Commit" ) );
289 		assoc.bindAspect( EOAssociation.ActionAspect, displayGroup, "updateDisplayedObjects" );
290 		assoc.establishConnection();
291 */
292 		// add MouseListener for table
293 		panel.table.addMouseListener( new MouseAdapter() 
294 		{
295 			public void mouseClicked(MouseEvent e)
296 			{
297 				if ( e.getClickCount() == 2 )
298 				{
299 					Object o = displayGroup.selectedObject();
300 					if ( o != null )
301 					{
302 //						new InspectorController( o );
303 						new EditController( 
304                             new ChildDataSource( 
305                                 displayGroup.dataSource(), o ) );
306 					}		
307 				}
308 			}
309 		});
310 		
311 		// add ActionListener for tree button
312 		((JButton) panel.buttonPanel.getButton( 
313 			"Tree View" ) ).addActionListener( new ActionListener()
314 		{
315 			public void actionPerformed( ActionEvent evt )
316 			{
317                 EOEditingContext parentContext = Test.editingContext;
318                 EOEditingContext childContext = new EOEditingContext( parentContext );
319                 
320                 // transpose objects to ids to faults
321                 List ids = new LinkedList();
322                 Iterator i = displayGroup.selectedObjects().iterator();
323                 while ( i.hasNext() )
324                 {
325                     ids.add( parentContext.globalIDForObject( i.next() ) );
326                 }
327                 List objects = new LinkedList();
328                 i = ids.iterator();
329                 while( i.hasNext() )
330                 {
331                     objects.add( childContext.faultForGlobalID( (EOGlobalID) i.next(), childContext ) );
332                 }
333                 
334 				EODisplayGroup treeGroup = new EODisplayGroup();
335                 treeGroup.setSortOrderings( new NSArray( "lastName" ) );
336 				treeGroup.setObjectArray( objects );
337 
338                 EODisplayGroup childGroup = new EODisplayGroup();
339                 
340                 //childGroup.setDelegate( new DebuggingDelegate() );
341 				//new TreeInspectorController( treeGroup, childGroup );	
342 				//new BindingController( treeGroup, childGroup );	
343 
344 				new TreeController( childContext, treeGroup, childGroup );	
345 
346                 //NOTE: ChildDataSource is fundamentally broken                    
347 //				new TreeController( new ChildDataSource(
348 //                    displayGroup.dataSource(), displayGroup.selectedObjects() ) );	
349 			}
350 		});
351 
352 /*
353         NSNotificationCenter.defaultCenter().addObserver(
354             this, 
355             new NSSelector( "hitMe", new Class[] { NSNotification.class } ), 
356             null, null );        
357 */
358 	}
359     
360     private String filter;
361     public String getFilter()
362     {
363         return filter;
364     }
365     
366     public void setFilter( String aFilter )
367     {
368         filter = aFilter;
369         
370         EOQualifier qualifier = null;
371         if ( ! "".equals( aFilter ) )
372         {
373             qualifier = new EOKeyValueQualifier(
374                 "firstName", EOQualifier.QualifierOperatorContains, filter );
375         }
376         displayGroup.setQualifier( qualifier );
377         displayGroup.updateDisplayedObjects();
378     }
379 	
380     public void hitMe( NSNotification aNote )
381     {
382         System.out.println( aNote );   
383     }
384 
385 	public void actionPerformed( ActionEvent evt )
386 	{
387         Object o = displayGroup.selectedObject();
388         if ( o != null )
389         {
390 //            new ObjectInspector( o );
391             new InspectorController( o );
392         }		
393     }
394     
395 }