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 Icon icon = UIManager.getIcon("FileChooser.homeFolderIcon"); 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 ); } } }