summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.test/src/main/java/net/wotonomy/test/EditController.java
blob: 6a36db3a383d768d0b3201ec17b745455b1b7000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package net.wotonomy.test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;

import net.wotonomy.control.ChildDataSource;
import net.wotonomy.control.EODataSource;
import net.wotonomy.control.PropertyDataSource;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.ui.EOAssociation;
import net.wotonomy.ui.EODisplayGroup;
import net.wotonomy.ui.MasterDetailAssociation;
import net.wotonomy.ui.swing.DisplayGroupActionAssociation;
import net.wotonomy.ui.swing.ListAssociation;
import net.wotonomy.ui.swing.RadioPanelAssociation;
import net.wotonomy.ui.swing.TextAssociation;
import net.wotonomy.ui.swing.components.ButtonPanel;
import net.wotonomy.ui.swing.util.WindowUtilities;

/**
 * A simple editor panel with a few textfields.
 */
public class EditController {
	EODisplayGroup group;
	JDialog dialog;

	public EditController(EODataSource aDataSource) {
		EditPanel editPanel = new EditPanel();
		editPanel.infoPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(),
				BorderFactory.createEmptyBorder(10, 10, 10, 10)));
		editPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		ButtonPanel okPanel = new ButtonPanel(new String[] { "Revert", "Refault", "Refresh", "Commit" });
		editPanel.add(okPanel, BorderLayout.SOUTH);

		group = new EODisplayGroup();
		group.setDataSource(aDataSource);
		group.fetch();
		group.selectNext();

		// text associations

		EOAssociation ta;

		ta = new TextAssociation(editPanel.firstNameField);
		ta.bindAspect(EOAssociation.ValueAspect, group, "firstName");
		ta.establishConnection();

		ta = new TextAssociation(editPanel.middleNameField);
		ta.bindAspect(EOAssociation.ValueAspect, group, "middleName");
		ta.establishConnection();

		ta = new TextAssociation(editPanel.lastNameField);
		ta.bindAspect(EOAssociation.ValueAspect, group, "lastName");
		ta.establishConnection();

		// radio panels

		ta = new RadioPanelAssociation(editPanel.yearRadioPanel);
		ta.bindAspect(EOAssociation.ValueAspect, group, "createDate.year");

		EODisplayGroup yearTitles = new EODisplayGroup();
		yearTitles.setObjectArray(new NSArray(new Object[] { "1999", "2000", "2001" }));
		ta.bindAspect(EOAssociation.TitlesAspect, yearTitles, "");

		EODisplayGroup yearObjects = new EODisplayGroup();
		yearObjects.setObjectArray(new NSArray(new Object[] { new Integer(99), new Integer(100), new Integer(101) }));
		ta.bindAspect(EOAssociation.ObjectsAspect, yearObjects, "");

		ta.establishConnection();

		// detail group

		final EODisplayGroup detailGroup = new EODisplayGroup();
		detailGroup.setDataSource(new PropertyDataSource(aDataSource.editingContext(), TestObject.class));

		ta = new MasterDetailAssociation(detailGroup);
		ta.bindAspect(EOAssociation.ParentAspect, group, "childList");
		ta.establishConnection();

		ta = new ListAssociation(editPanel.list);
		ta.bindAspect(EOAssociation.TitlesAspect, detailGroup, "fullName");
		ta.establishConnection();

		// display group action associations

		AbstractButton button = (AbstractButton) editPanel.addPanel.getButton("Add");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				detailGroup.insertNewObjectAtIndex(0);
			}
		});

		ta = new DisplayGroupActionAssociation(editPanel.addPanel.getButton("Remove"));
		ta.bindAspect(EOAssociation.ActionAspect, detailGroup, "deleteSelection");
		ta.establishConnection();

		// ok / cancel buttons

		button = (AbstractButton) okPanel.getButton("Commit");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				group.dataSource().editingContext().saveChanges();
			}
		});

		button = (AbstractButton) okPanel.getButton("Refresh");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				group.dataSource().editingContext().invalidateAllObjects();
			}
		});

		button = (AbstractButton) okPanel.getButton("Refault");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				/*
				 * Object o = group.displayedObjects().objectAtIndex( 0 );
				 * group.dataSource().editingContext().refaultObject( o,
				 * group.dataSource().editingContext().globalIDForObject( o ),
				 * group.dataSource().editingContext() );
				 */
				group.dataSource().editingContext().revert();
				group.dataSource().editingContext().refaultObjects();
			}
		});

		button = (AbstractButton) okPanel.getButton("Revert");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				group.dataSource().editingContext().revert();
			}
		});

		// add mouse listener for list

		editPanel.list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Object item = detailGroup.selectedObject();
					if (item != null) {
//                        new InspectorController( item );

						new EditController(new ChildDataSource(group.dataSource(), item));
					}
				}
			}
		});

		// launch

		dialog = new JDialog();
		// add WindowListener for frame
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		dialog.getContentPane().add(editPanel);
		dialog.setTitle("Edit Panel");
		dialog.pack();
//		dialog.setSize( 300, dialog.getSize().height );
		WindowUtilities.cascade(dialog);
		dialog.show();

		// workaround for memory issues on jdk1.2.2
		dialog.addWindowListener(new WindowAdapter() {
			// exit on close
			public void windowClosing(WindowEvent e) {
				((JDialog) e.getWindow()).getContentPane().removeAll();
			}
		});
	}

}