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  import javax.swing.JFrame;
15  
16  import net.wotonomy.control.ChildDataSource;
17  import net.wotonomy.control.EODataSource;
18  import net.wotonomy.control.PropertyDataSource;
19  import net.wotonomy.foundation.NSArray;
20  import net.wotonomy.ui.EOAssociation;
21  import net.wotonomy.ui.EODisplayGroup;
22  import net.wotonomy.ui.MasterDetailAssociation;
23  import net.wotonomy.ui.swing.DisplayGroupActionAssociation;
24  import net.wotonomy.ui.swing.ListAssociation;
25  import net.wotonomy.ui.swing.RadioPanelAssociation;
26  import net.wotonomy.ui.swing.TextAssociation;
27  import net.wotonomy.ui.swing.components.ButtonPanel;
28  import net.wotonomy.ui.swing.util.WindowUtilities;
29  
30  /***
31  * A simple editor panel with a few textfields.
32  */
33  public class EditController 
34  {    
35      EODisplayGroup group;
36      JDialog dialog;
37      
38      public EditController( EODataSource aDataSource )
39      {
40  		EditPanel editPanel = new EditPanel();
41          editPanel.infoPanel.setBorder( 
42              BorderFactory.createCompoundBorder(
43                  BorderFactory.createRaisedBevelBorder(),
44                  BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) ) );
45          editPanel.setBorder( 
46              BorderFactory.createEmptyBorder( 0, 0, 0, 0 ) );
47          ButtonPanel okPanel = new ButtonPanel( 
48              new String[] { "Revert", "Refault", "Refresh", "Commit" } );
49          editPanel.add( okPanel, BorderLayout.SOUTH );
50  		
51  		group = new EODisplayGroup();
52  		group.setDataSource( aDataSource );
53          group.fetch();
54  		group.selectNext();
55  		
56  		// text associations
57  		
58  		EOAssociation ta;
59  		
60  		ta = new TextAssociation( editPanel.firstNameField );
61  		ta.bindAspect( EOAssociation.ValueAspect, group, "firstName" );
62  		ta.establishConnection();
63  		
64  		ta = new TextAssociation( editPanel.middleNameField );
65  		ta.bindAspect( EOAssociation.ValueAspect, group, "middleName" );
66  		ta.establishConnection();
67  		
68  		ta = new TextAssociation( editPanel.lastNameField );
69  		ta.bindAspect( EOAssociation.ValueAspect, group, "lastName" );
70  		ta.establishConnection();
71  		
72  		// radio panels
73  		
74  		ta = new RadioPanelAssociation( editPanel.yearRadioPanel );
75  		ta.bindAspect( EOAssociation.ValueAspect, group, "createDate.year" );
76  		
77  		EODisplayGroup yearTitles = new EODisplayGroup();
78  		yearTitles.setObjectArray( new NSArray( 
79  			new Object[] { "1999", "2000", "2001" } ) );
80  		ta.bindAspect( EOAssociation.TitlesAspect, yearTitles, "" );
81  	
82  		EODisplayGroup yearObjects = new EODisplayGroup();
83  		yearObjects.setObjectArray( new NSArray( 
84  			new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) );
85  		ta.bindAspect( EOAssociation.ObjectsAspect, yearObjects, "" );
86  		
87  		ta.establishConnection();
88  		
89  		// detail group
90  		
91  		final EODisplayGroup detailGroup = new EODisplayGroup();
92          detailGroup.setDataSource( new PropertyDataSource( 
93              aDataSource.editingContext(), TestObject.class ) );
94  		
95  		ta = new MasterDetailAssociation( detailGroup );
96  		ta.bindAspect( EOAssociation.ParentAspect, group, "childList" );
97  		ta.establishConnection();
98  		
99  		ta = new ListAssociation( editPanel.list );
100 		ta.bindAspect( EOAssociation.TitlesAspect, detailGroup, "fullName" );
101 		ta.establishConnection();
102 		
103 		// display group action associations
104 
105 		AbstractButton button = (AbstractButton)
106 			editPanel.addPanel.getButton( "Add" );
107 		button.addActionListener( new ActionListener()
108 		{
109 			public void actionPerformed( ActionEvent evt )
110 			{
111 				detailGroup.insertNewObjectAtIndex( 0 );	
112 			}
113 		} );
114 		
115 		ta = new DisplayGroupActionAssociation( 
116             editPanel.addPanel.getButton( "Remove" ) );
117 		ta.bindAspect( EOAssociation.ActionAspect, detailGroup, "deleteSelection" );
118 		ta.establishConnection();
119 
120         // ok / cancel buttons
121         
122 		button = (AbstractButton)
123 			okPanel.getButton( "Commit" );
124 		button.addActionListener( new ActionListener()
125 		{
126 			public void actionPerformed( ActionEvent evt )
127 			{
128 				group.dataSource().editingContext().saveChanges();	
129 			}
130 		} );
131 		
132 		button = (AbstractButton)
133 			okPanel.getButton( "Refresh" );
134 		button.addActionListener( new ActionListener()
135 		{
136 			public void actionPerformed( ActionEvent evt )
137 			{
138 				group.dataSource().editingContext().invalidateAllObjects();	
139 			}
140 		} );
141 		
142 		button = (AbstractButton)
143 			okPanel.getButton( "Refault" );
144 		button.addActionListener( new ActionListener()
145 		{
146 			public void actionPerformed( ActionEvent evt )
147 			{
148 /*                
149                 Object o = group.displayedObjects().objectAtIndex( 0 );
150 				group.dataSource().editingContext().refaultObject(
151                     o, 
152                     group.dataSource().editingContext().globalIDForObject( o ),
153                     group.dataSource().editingContext() );
154 */
155                 group.dataSource().editingContext().revert();                    
156                 group.dataSource().editingContext().refaultObjects();                    
157 			}
158 		} );
159 		
160 		button = (AbstractButton)
161 			okPanel.getButton( "Revert" );
162 		button.addActionListener( new ActionListener()
163 		{
164 			public void actionPerformed( ActionEvent evt )
165 			{
166 				group.dataSource().editingContext().revert();	
167 			}
168 		} );
169 		
170 		// add mouse listener for list
171 		
172 		editPanel.list.addMouseListener( new MouseAdapter() 
173 		{
174 			public void mouseClicked(MouseEvent e)
175 			{
176 				if ( e.getClickCount() == 2 )
177 				{
178 					Object item = detailGroup.selectedObject();
179 					if ( item != null )
180 					{
181 //                        new InspectorController( item );
182                         
183 						new EditController( 
184                             new ChildDataSource( 
185                                 group.dataSource(), item ) );
186 					}		
187 				}
188 			}
189 		});
190 		
191 		// launch
192 								
193 		dialog = new JDialog();
194 		// add WindowListener for frame
195         dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
196 		dialog.getContentPane().add( editPanel );
197 		dialog.setTitle( "Edit Panel" );
198 		dialog.pack();
199 //		dialog.setSize( 300, dialog.getSize().height );
200         WindowUtilities.cascade( dialog );
201 		dialog.show();
202 
203         // workaround for memory issues on jdk1.2.2
204 		dialog.addWindowListener( new WindowAdapter()
205 		{
206 			// exit on close
207 			public void windowClosing(WindowEvent e)
208 			{	
209 				((JDialog)e.getWindow()).getContentPane().removeAll();
210 			}
211 		});
212 	}
213     
214 }