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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 treePanel.tree.setEditable( true );
94
95
96
97 titlesGroup.fetch();
98
99
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
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
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
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
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
194 }
195 } );
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
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
265 d.addWindowListener( new WindowAdapter()
266 {
267
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
281 new ObjectInspector( item );
282 }
283 }
284
285 }