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.awt.event.WindowAdapter;
8   import java.awt.event.WindowEvent;
9   
10  import javax.swing.AbstractButton;
11  import javax.swing.JDialog;
12  
13  import net.wotonomy.foundation.NSArray;
14  import net.wotonomy.ui.EOAssociation;
15  import net.wotonomy.ui.EODisplayGroup;
16  import net.wotonomy.ui.MasterDetailAssociation;
17  import net.wotonomy.ui.swing.ListAssociation;
18  import net.wotonomy.ui.swing.RadioPanelAssociation;
19  import net.wotonomy.ui.swing.TextAssociation;
20  import net.wotonomy.ui.swing.TreeAssociation;
21  import net.wotonomy.ui.swing.util.WindowUtilities;
22  
23  /***
24  * A simple editor panel with a few textfields.
25  */
26  public class TreeInspectorController 
27  {    
28      public TreeInspectorController( EODisplayGroup titlesGroup, EODisplayGroup childrenGroup )
29      {
30          final EODisplayGroup group = childrenGroup;
31  //        final EODisplayGroup group = titlesGroup;
32  		final TreePanel treePanel = new TreePanel();
33  		
34  		// text associations
35  		
36  		EOAssociation ta;
37  		
38  		ta = new TreeAssociation( treePanel.tree, "People" );
39  		ta.bindAspect( EOAssociation.TitlesAspect, titlesGroup, "lastName" );
40  		ta.bindAspect( EOAssociation.ChildrenAspect, group, "childList" );
41  		ta.bindAspect( EOAssociation.IsLeafAspect, titlesGroup, "childCount" );
42  //		ta.bindAspect( EOAssociation.IsLeafAspect, null, "false" );
43  		ta.establishConnection();
44  		treePanel.tree.setEditable( true );
45  		
46          ta = new TextAssociation( treePanel.editPanel.firstNameField );
47          ta.bindAspect( EOAssociation.ValueAspect, group, "firstName" );
48  		ta.establishConnection();
49          
50          ta = new TextAssociation( treePanel.editPanel.middleNameField );
51          ta.bindAspect( EOAssociation.ValueAspect, group, "middleName" );
52  		ta.establishConnection();
53          
54          ta = new TextAssociation( treePanel.editPanel.lastNameField );
55          ta.bindAspect( EOAssociation.ValueAspect, group, "lastName" );
56  		ta.establishConnection();
57  
58  		ta = new RadioPanelAssociation( treePanel.editPanel.yearRadioPanel );
59  		ta.bindAspect( EOAssociation.ValueAspect, group, "createDate.year" );
60  		
61  		EODisplayGroup yearTitles = new EODisplayGroup();
62  		yearTitles.setObjectArray( new NSArray( 
63  			new Object[] { "1999", "2000", "2001" } ) );
64  		ta.bindAspect( EOAssociation.TitlesAspect, yearTitles, "" );
65  	
66  		EODisplayGroup yearObjects = new EODisplayGroup();
67  		yearObjects.setObjectArray( new NSArray( 
68  			new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) );
69  		ta.bindAspect( EOAssociation.ObjectsAspect, yearObjects, "" );
70  		
71  		ta.establishConnection();
72  
73  		// detail group
74  		
75  		final EODisplayGroup detailGroup = new EODisplayGroup();
76  		
77  		ta = new MasterDetailAssociation( detailGroup );
78  		ta.bindAspect( EOAssociation.ParentAspect, group, "childList" );
79  		ta.establishConnection();
80  		
81  		ta = new ListAssociation( treePanel.editPanel.list );
82  		ta.bindAspect( EOAssociation.TitlesAspect, detailGroup, "fullName" );
83  		ta.establishConnection();
84  		
85  		// display group action associations
86  
87  		AbstractButton addButton = (AbstractButton)
88  			treePanel.editPanel.addPanel.getButton( "Add" );
89  		addButton.addActionListener( new ActionListener()
90  		{
91  			public void actionPerformed( ActionEvent evt )
92  			{
93  				detailGroup.insertObjectAtIndex( new TestObject(), 0 );	
94  			}
95  		} );
96  
97  		AbstractButton removeButton = (AbstractButton)
98  			treePanel.editPanel.addPanel.getButton( "Remove" );
99  		removeButton.addActionListener( new ActionListener()
100 		{
101 			public void actionPerformed( ActionEvent evt )
102 			{
103                 if ( detailGroup.selectedObject() != null )
104     				detailGroup.deleteSelection();	
105 			}
106 		} );
107 /*
108 		AbstractButton refreshButton = (AbstractButton)
109 			treePanel.editPanel.addPanel.getButton( "Refresh" );
110 		refreshButton.addActionListener( new ActionListener()
111 		{
112             int count = 0;
113 			public void actionPerformed( ActionEvent evt )
114 			{                
115                 EODisplayGroup displayGroup = (EODisplayGroup)
116                    treePanel.tree.getSelectionPath().getLastPathComponent();
117 //                displayGroup.insertObjectAtIndex( new TestObject(), 0 );
118 //                displayGroup.deleteObjectAtIndex( 0 );
119                List sortOrderings = new LinkedList();
120                if ( count++ % 2 == 0 )
121                {
122                    sortOrderings.add( new EOSortOrdering( 
123                         "lastName", EOSortOrdering.CompareAscending ) );
124                }
125                else
126                {
127                    sortOrderings.add( new EOSortOrdering( 
128                         "lastName", EOSortOrdering.CompareDescending ) );
129                }
130                displayGroup.setSortOrderings( sortOrderings );
131                displayGroup.updateDisplayedObjects();
132 			}
133 		} );
134 */        
135 /*		
136 		ta = new DisplayGroupActionAssociation( 
137             treePanel.editPanel.addPanel.getButton( "Remove" ) );
138 		ta.bindAspect( EOAssociation.ActionAspect, detailGroup, "deleteSelection" );
139 		ta.establishConnection();
140         
141 		ta = new DisplayGroupActionAssociation( 
142             treePanel.editPanel.addPanel.getButton( "Refresh" ) );
143 		ta.bindAspect( EOAssociation.ActionAspect, group, "updateDisplayedObjects" );
144 		ta.establishConnection();
145 */
146 
147 		// add mouse listener for list
148 		
149 		treePanel.editPanel.list.addMouseListener( new MouseAdapter() 
150 		{
151 			public void mouseClicked(MouseEvent e)
152 			{
153 				if ( e.getClickCount() == 2 )
154 				{
155 					Object item = detailGroup.selectedObject();
156 					if ( item != null )
157 					{
158 						new InspectorController( item );
159 					}		
160 				}
161 			}
162 		});
163 
164 		// launch
165 		
166 		JDialog d = new JDialog();
167 		d.getContentPane().add( treePanel );
168 		d.setTitle( "Tree Panel" );
169 		d.pack();
170         WindowUtilities.cascade( d );
171 		d.show();
172 
173         // workaround for memory issues on jdk1.2.2
174 		d.addWindowListener( new WindowAdapter()
175 		{
176 			// exit on close
177 			public void windowClosing(WindowEvent e)
178 			{	
179 				((JDialog)e.getWindow()).getContentPane().removeAll();
180 			}
181 		});
182 	}
183     
184 }