View Javadoc

1   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      public TreeController( EODataSource aDataSource )
40      {
41          titlesGroup = new EODisplayGroup();
42          group = new EODisplayGroup();
43          detailGroup = new EODisplayGroup();
44          titlesGroup.setDataSource( aDataSource );
45          editingContext = aDataSource.editingContext();
46          init();
47      }
48      
49      public TreeController( EOEditingContext aContext, 
50          EODisplayGroup aTitlesGroup, EODisplayGroup aChildGroup )
51      {
52          titlesGroup = aTitlesGroup;
53          group = aChildGroup;
54          detailGroup = new EODisplayGroup();
55          editingContext = aContext;
56          init();
57      }
58      
59      public void init()
60      {
61  		final TreePanel treePanel = new TreePanel();
62          treePanel.panel.setBorder( 
63              BorderFactory.createCompoundBorder(
64                  BorderFactory.createRaisedBevelBorder(),
65                  BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) ) );
66          treePanel.setBorder( 
67              BorderFactory.createEmptyBorder( 0, 0, 0, 0 ) );
68          ButtonPanel okPanel = new ButtonPanel( 
69              new String[] { "Refresh", "Commit" } );
70          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          treePanel.tree.setEditable( true );
94  
95          // set up display groups
96  		
97          titlesGroup.fetch();
98  
99  		// text associations
100 		EOAssociation ta;
101         
102 		ta = new TreeAssociation( treePanel.tree, "People" );
103 		ta.bindAspect( EOAssociation.TitlesAspect, titlesGroup, "fullName" );
104 		ta.bindAspect( EOAssociation.ChildrenAspect, group, "childList" );
105 		ta.bindAspect( EOAssociation.IsLeafAspect, titlesGroup, "childCount" );
106 //		ta.bindAspect( EOAssociation.IsLeafAspect, null, "false" );
107 		ta.establishConnection();
108 		treePanel.tree.setEditable( true );
109         
110         ta = new TextAssociation( treePanel.editPanel.firstNameField );
111         ta.bindAspect( EOAssociation.ValueAspect, group, "firstName" );
112 		ta.establishConnection();
113         
114         ta = new TextAssociation( treePanel.editPanel.middleNameField );
115         ta.bindAspect( EOAssociation.ValueAspect, group, "middleName" );
116 		ta.establishConnection();
117         
118         ta = new TextAssociation( treePanel.editPanel.lastNameField );
119         ta.bindAspect( EOAssociation.ValueAspect, group, "lastName" );
120 		ta.establishConnection();
121 
122 		ta = new RadioPanelAssociation( treePanel.editPanel.yearRadioPanel );
123 		ta.bindAspect( EOAssociation.ValueAspect, group, "createDate.year" );
124 		
125 		EODisplayGroup yearTitles = new EODisplayGroup();
126 		yearTitles.setObjectArray( new NSArray( 
127 			new Object[] { "1999", "2000", "2001" } ) );
128 		ta.bindAspect( EOAssociation.TitlesAspect, yearTitles, "" );
129 	
130 		EODisplayGroup yearObjects = new EODisplayGroup();
131 		yearObjects.setObjectArray( new NSArray( 
132 			new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) );
133 		ta.bindAspect( EOAssociation.ObjectsAspect, yearObjects, "" );
134 		
135 		ta.establishConnection();
136 
137 		// detail group
138 		
139 		ta = new MasterDetailAssociation( detailGroup );
140 		ta.bindAspect( EOAssociation.ParentAspect, group, "childList" );
141 		ta.establishConnection();
142 		
143 		ta = new ListAssociation( treePanel.editPanel.list );
144 		ta.bindAspect( EOAssociation.TitlesAspect, detailGroup, "fullName" );
145 		ta.establishConnection();
146 		
147 		// display group action associations
148 
149 		AbstractButton addButton = (AbstractButton)
150 			treePanel.editPanel.addPanel.getButton( "Add" );
151 		addButton.addActionListener( new ActionListener()
152 		{
153 			public void actionPerformed( ActionEvent evt )
154 			{
155                 Object testObject = new TestObject();
156                 editingContext.insertObject( testObject );
157 				detailGroup.insertObjectAtIndex( testObject, 0 );	
158 			}
159 		} );
160 
161 		AbstractButton removeButton = (AbstractButton)
162 			treePanel.editPanel.addPanel.getButton( "Remove" );
163 		removeButton.addActionListener( new ActionListener()
164 		{
165 			public void actionPerformed( ActionEvent evt )
166 			{
167                 if ( detailGroup.selectedObject() != null )
168     				detailGroup.deleteSelection();	
169 			}
170 		} );
171 
172         // ok / cancel buttons
173         
174         AbstractButton button;
175         
176 		button = (AbstractButton)
177 			okPanel.getButton( "Commit" );
178 		button.addActionListener( new ActionListener()
179 		{
180 			public void actionPerformed( ActionEvent evt )
181 			{
182 				editingContext.saveChanges();	
183 			}
184 		} );
185 		
186 		button = (AbstractButton)
187 			okPanel.getButton( "Refresh" );
188 		button.addActionListener( new ActionListener()
189 		{
190 			public void actionPerformed( ActionEvent evt )
191 			{
192 				editingContext.revert();	
193 //				editingContext.invalidateAllObjects();	
194 			}
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 		treePanel.editPanel.list.addMouseListener( new MouseAdapter() 
241 		{
242 			public void mouseClicked(MouseEvent e)
243 			{
244 				if ( e.getClickCount() == 2 )
245 				{
246 					Object item = detailGroup.selectedObject();
247 					if ( item != null )
248 					{
249 						new InspectorController( item );
250 					}		
251 				}
252 			}
253 		});
254 
255 		// launch
256 		
257 		JDialog d = new JDialog();
258 		d.getContentPane().add( treePanel );
259 		d.setTitle( "Tree Panel" );
260 		d.pack();
261         WindowUtilities.cascade( d );
262 		d.show();
263 
264         // workaround for memory issues on jdk1.2.2
265 		d.addWindowListener( new WindowAdapter()
266 		{
267 			// exit on close
268 			public void windowClosing(WindowEvent e)
269 			{	
270 				((JDialog)e.getWindow()).getContentPane().removeAll();
271 			}
272 		});
273 	}
274     
275     public void actionPerformed( ActionEvent evt )
276     {
277         Object item = group.selectedObject();
278         if ( item != null )
279         {
280 //            new InspectorController( item );
281             new ObjectInspector( item );
282         }		
283     }
284     
285 }