Coverage Report - net.wotonomy.test.TreeController
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeController
0% 
0% 
1.4
 
 1  0
 package net.wotonomy.test;
 2  
 
 3  
 import java.awt.BorderLayout;
 4  
 import java.awt.event.ActionEvent;
 5  
 import java.awt.event.ActionListener;
 6  
 import java.awt.event.MouseAdapter;
 7  
 import java.awt.event.MouseEvent;
 8  
 import java.awt.event.WindowAdapter;
 9  
 import java.awt.event.WindowEvent;
 10  
 
 11  
 import javax.swing.AbstractButton;
 12  
 import javax.swing.BorderFactory;
 13  
 import javax.swing.JDialog;
 14  
 
 15  
 import net.wotonomy.control.EODataSource;
 16  
 import net.wotonomy.control.EOEditingContext;
 17  
 import net.wotonomy.foundation.NSArray;
 18  
 import net.wotonomy.ui.EOAssociation;
 19  
 import net.wotonomy.ui.EODisplayGroup;
 20  
 import net.wotonomy.ui.MasterDetailAssociation;
 21  
 import net.wotonomy.ui.swing.ListAssociation;
 22  
 import net.wotonomy.ui.swing.RadioPanelAssociation;
 23  
 import net.wotonomy.ui.swing.TextAssociation;
 24  
 import net.wotonomy.ui.swing.TreeAssociation;
 25  
 import net.wotonomy.ui.swing.components.ButtonPanel;
 26  
 import net.wotonomy.ui.swing.util.ObjectInspector;
 27  
 import net.wotonomy.ui.swing.util.WindowUtilities;
 28  
 
 29  
 /**
 30  
 * A simple editor panel with a few textfields.
 31  
 */
 32  
 public class TreeController implements ActionListener
 33  
 {    
 34  
     final EODisplayGroup group;
 35  
     final EODisplayGroup titlesGroup;
 36  
     final EODisplayGroup detailGroup;
 37  
     final EOEditingContext editingContext;
 38  
     
 39  0
     public TreeController( EODataSource aDataSource )
 40  0
     {
 41  0
         titlesGroup = new EODisplayGroup();
 42  0
         group = new EODisplayGroup();
 43  0
         detailGroup = new EODisplayGroup();
 44  0
         titlesGroup.setDataSource( aDataSource );
 45  0
         editingContext = aDataSource.editingContext();
 46  0
         init();
 47  0
     }
 48  
     
 49  0
     public TreeController( EOEditingContext aContext, 
 50  
         EODisplayGroup aTitlesGroup, EODisplayGroup aChildGroup )
 51  0
     {
 52  0
         titlesGroup = aTitlesGroup;
 53  0
         group = aChildGroup;
 54  0
         detailGroup = new EODisplayGroup();
 55  0
         editingContext = aContext;
 56  0
         init();
 57  0
     }
 58  
     
 59  
     public void init()
 60  
     {
 61  0
                 final TreePanel treePanel = new TreePanel();
 62  0
         treePanel.panel.setBorder( 
 63  0
             BorderFactory.createCompoundBorder(
 64  0
                 BorderFactory.createRaisedBevelBorder(),
 65  0
                 BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) ) );
 66  0
         treePanel.setBorder( 
 67  0
             BorderFactory.createEmptyBorder( 0, 0, 0, 0 ) );
 68  0
         ButtonPanel okPanel = new ButtonPanel( 
 69  0
             new String[] { "Refresh", "Commit" } );
 70  0
         treePanel.add( okPanel, BorderLayout.SOUTH );
 71  
 /*        
 72  
 
 73  
         // set up renderer
 74  
                 IconCellRenderer iconRenderer = new IconCellRenderer()
 75  
                 {
 76  
             private Icon icon = UIManager.getIcon("FileChooser.homeFolderIcon");
 77  
             public Icon getIconForContext(
 78  
                 JComponent container, Object value, 
 79  
                 int row, int col,
 80  
                 boolean isSelected, boolean hasFocus, 
 81  
                 boolean isExpanded, boolean isLeaf )
 82  
             {
 83  
                 return icon;
 84  
             }
 85  
         };
 86  
                 treePanel.tree.setCellRenderer( iconRenderer );
 87  
 
 88  
         // enable icon clicking
 89  
                 treePanel.tree.setCellEditor( iconRenderer );
 90  
                 iconRenderer.addActionListener( this );
 91  
 */
 92  
 
 93  0
         treePanel.tree.setEditable( true );
 94  
 
 95  
         // set up display groups
 96  
                 
 97  0
         titlesGroup.fetch();
 98  
 
 99  
                 // text associations
 100  
                 EOAssociation ta;
 101  
         
 102  0
                 ta = new TreeAssociation( treePanel.tree, "People" );
 103  0
                 ta.bindAspect( EOAssociation.TitlesAspect, titlesGroup, "fullName" );
 104  0
                 ta.bindAspect( EOAssociation.ChildrenAspect, group, "childList" );
 105  0
                 ta.bindAspect( EOAssociation.IsLeafAspect, titlesGroup, "childCount" );
 106  
 //                ta.bindAspect( EOAssociation.IsLeafAspect, null, "false" );
 107  0
                 ta.establishConnection();
 108  0
                 treePanel.tree.setEditable( true );
 109  
         
 110  0
         ta = new TextAssociation( treePanel.editPanel.firstNameField );
 111  0
         ta.bindAspect( EOAssociation.ValueAspect, group, "firstName" );
 112  0
                 ta.establishConnection();
 113  
         
 114  0
         ta = new TextAssociation( treePanel.editPanel.middleNameField );
 115  0
         ta.bindAspect( EOAssociation.ValueAspect, group, "middleName" );
 116  0
                 ta.establishConnection();
 117  
         
 118  0
         ta = new TextAssociation( treePanel.editPanel.lastNameField );
 119  0
         ta.bindAspect( EOAssociation.ValueAspect, group, "lastName" );
 120  0
                 ta.establishConnection();
 121  
 
 122  0
                 ta = new RadioPanelAssociation( treePanel.editPanel.yearRadioPanel );
 123  0
                 ta.bindAspect( EOAssociation.ValueAspect, group, "createDate.year" );
 124  
                 
 125  0
                 EODisplayGroup yearTitles = new EODisplayGroup();
 126  0
                 yearTitles.setObjectArray( new NSArray( 
 127  0
                         new Object[] { "1999", "2000", "2001" } ) );
 128  0
                 ta.bindAspect( EOAssociation.TitlesAspect, yearTitles, "" );
 129  
         
 130  0
                 EODisplayGroup yearObjects = new EODisplayGroup();
 131  0
                 yearObjects.setObjectArray( new NSArray( 
 132  0
                         new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) );
 133  0
                 ta.bindAspect( EOAssociation.ObjectsAspect, yearObjects, "" );
 134  
                 
 135  0
                 ta.establishConnection();
 136  
 
 137  
                 // detail group
 138  
                 
 139  0
                 ta = new MasterDetailAssociation( detailGroup );
 140  0
                 ta.bindAspect( EOAssociation.ParentAspect, group, "childList" );
 141  0
                 ta.establishConnection();
 142  
                 
 143  0
                 ta = new ListAssociation( treePanel.editPanel.list );
 144  0
                 ta.bindAspect( EOAssociation.TitlesAspect, detailGroup, "fullName" );
 145  0
                 ta.establishConnection();
 146  
                 
 147  
                 // display group action associations
 148  
 
 149  0
                 AbstractButton addButton = (AbstractButton)
 150  0
                         treePanel.editPanel.addPanel.getButton( "Add" );
 151  0
                 addButton.addActionListener( new ActionListener()
 152  
                 {
 153  0
                         public void actionPerformed( ActionEvent evt )
 154  
                         {
 155  0
                 Object testObject = new TestObject();
 156  0
                 editingContext.insertObject( testObject );
 157  0
                                 detailGroup.insertObjectAtIndex( testObject, 0 );        
 158  0
                         }
 159  
                 } );
 160  
 
 161  0
                 AbstractButton removeButton = (AbstractButton)
 162  0
                         treePanel.editPanel.addPanel.getButton( "Remove" );
 163  0
                 removeButton.addActionListener( new ActionListener()
 164  
                 {
 165  0
                         public void actionPerformed( ActionEvent evt )
 166  
                         {
 167  0
                 if ( detailGroup.selectedObject() != null )
 168  0
                                     detailGroup.deleteSelection();        
 169  0
                         }
 170  
                 } );
 171  
 
 172  
         // ok / cancel buttons
 173  
         
 174  
         AbstractButton button;
 175  
         
 176  0
                 button = (AbstractButton)
 177  0
                         okPanel.getButton( "Commit" );
 178  0
                 button.addActionListener( new ActionListener()
 179  
                 {
 180  0
                         public void actionPerformed( ActionEvent evt )
 181  
                         {
 182  0
                                 editingContext.saveChanges();        
 183  0
                         }
 184  
                 } );
 185  
                 
 186  0
                 button = (AbstractButton)
 187  0
                         okPanel.getButton( "Refresh" );
 188  0
                 button.addActionListener( new ActionListener()
 189  
                 {
 190  0
                         public void actionPerformed( ActionEvent evt )
 191  
                         {
 192  0
                                 editingContext.revert();        
 193  
 //                                editingContext.invalidateAllObjects();        
 194  0
                         }
 195  
                 } );
 196  
                 
 197  
         
 198  
 /*
 199  
                 AbstractButton refreshButton = (AbstractButton)
 200  
                         treePanel.editPanel.addPanel.getButton( "Refresh" );
 201  
                 refreshButton.addActionListener( new ActionListener()
 202  
                 {
 203  
             int count = 0;
 204  
                         public void actionPerformed( ActionEvent evt )
 205  
                         {                
 206  
                 EODisplayGroup displayGroup = (EODisplayGroup)
 207  
                    treePanel.tree.getSelectionPath().getLastPathComponent();
 208  
 //                displayGroup.insertObjectAtIndex( new TestObject(), 0 );
 209  
 //                displayGroup.deleteObjectAtIndex( 0 );
 210  
                List sortOrderings = new LinkedList();
 211  
                if ( count++ % 2 == 0 )
 212  
                {
 213  
                    sortOrderings.add( new EOSortOrdering( 
 214  
                         "lastName", EOSortOrdering.CompareAscending ) );
 215  
                }
 216  
                else
 217  
                {
 218  
                    sortOrderings.add( new EOSortOrdering( 
 219  
                         "lastName", EOSortOrdering.CompareDescending ) );
 220  
                }
 221  
                displayGroup.setSortOrderings( sortOrderings );
 222  
                displayGroup.updateDisplayedObjects();
 223  
                         }
 224  
                 } );
 225  
 */        
 226  
 /*                
 227  
                 ta = new DisplayGroupActionAssociation( 
 228  
             treePanel.editPanel.addPanel.getButton( "Remove" ) );
 229  
                 ta.bindAspect( EOAssociation.ActionAspect, detailGroup, "deleteSelection" );
 230  
                 ta.establishConnection();
 231  
         
 232  
                 ta = new DisplayGroupActionAssociation( 
 233  
             treePanel.editPanel.addPanel.getButton( "Refresh" ) );
 234  
                 ta.bindAspect( EOAssociation.ActionAspect, group, "updateDisplayedObjects" );
 235  
                 ta.establishConnection();
 236  
 */
 237  
 
 238  
                 // add mouse listener for list
 239  
                 
 240  0
                 treePanel.editPanel.list.addMouseListener( new MouseAdapter() 
 241  
                 {
 242  0
                         public void mouseClicked(MouseEvent e)
 243  
                         {
 244  0
                                 if ( e.getClickCount() == 2 )
 245  
                                 {
 246  0
                                         Object item = detailGroup.selectedObject();
 247  0
                                         if ( item != null )
 248  
                                         {
 249  0
                                                 new InspectorController( item );
 250  
                                         }                
 251  
                                 }
 252  0
                         }
 253  
                 });
 254  
 
 255  
                 // launch
 256  
                 
 257  0
                 JDialog d = new JDialog();
 258  0
                 d.getContentPane().add( treePanel );
 259  0
                 d.setTitle( "Tree Panel" );
 260  0
                 d.pack();
 261  0
         WindowUtilities.cascade( d );
 262  0
                 d.show();
 263  
 
 264  
         // workaround for memory issues on jdk1.2.2
 265  0
                 d.addWindowListener( new WindowAdapter()
 266  
                 {
 267  
                         // exit on close
 268  0
                         public void windowClosing(WindowEvent e)
 269  
                         {        
 270  0
                                 ((JDialog)e.getWindow()).getContentPane().removeAll();
 271  0
                         }
 272  
                 });
 273  0
         }
 274  
     
 275  
     public void actionPerformed( ActionEvent evt )
 276  
     {
 277  0
         Object item = group.selectedObject();
 278  0
         if ( item != null )
 279  
         {
 280  
 //            new InspectorController( item );
 281  0
             new ObjectInspector( item );
 282  
         }                
 283  0
     }
 284  
     
 285  
 }