001: package org.osbl.client.wings.form.editor;
002:
003: import org.conform.wings.*;
004: import org.conform.wings.editor.ComponentInvalidator;
005: import org.conform.*;
006: import org.osbl.client.wings.form.*;
007: import org.osbl.client.wings.*;
008: import org.osbl.client.wings.shell.Client;
009: import org.osbl.client.wings.shell.Window;
010: import org.osbl.client.action.*;
011: import org.wings.*;
012: import org.wings.table.STableColumnModel;
013: import org.wingx.XTable;
014: import org.wingx.XScrollPane;
015: import org.wingx.table.XTableClickListener;
016:
017: import javax.swing.table.TableModel;
018: import javax.swing.*;
019: import javax.swing.event.TableModelListener;
020: import javax.swing.event.TableModelEvent;
021: import java.util.*;
022: import java.awt.event.ActionEvent;
023: import java.awt.Insets;
024: import java.awt.GridBagConstraints;
025: import java.beans.PropertyChangeListener;
026: import java.beans.PropertyChangeEvent;
027:
028: public class OneToManyCompositionEditor implements Editor {
029: public SComponent createComponent(PropertyMeta propertyMeta) {
030: TablePanel tablePanel = new TablePanel(propertyMeta);
031: configureComponent(propertyMeta, tablePanel, false);
032: return tablePanel;
033: }
034:
035: public void configureComponent(PropertyMeta propertyMeta,
036: SComponent component, boolean erroneous) {
037: TablePanel tablePanel = (TablePanel) component;
038: tablePanel.setEnabled(propertyMeta.isWritable());
039: tablePanel.setVisible(propertyMeta.isReadable());
040: }
041:
042: public void setPropertyData(SComponent component,
043: PropertyData propertyData) {
044: BeanData beanData = propertyData.getBeanData();
045: if (beanData != null)
046: beanData.addPropertyChangeListener(propertyData
047: .getPropertyMeta().getName(),
048: new ComponentInvalidator(component));
049:
050: if (component instanceof TablePanel) {
051: TablePanel tablePanel = (TablePanel) component;
052: tablePanel.setPropertyData(propertyData);
053:
054: if (beanData != null)
055: beanData.addPropertyChangeListener(propertyData
056: .getPropertyMeta().getName(), tablePanel);
057: }
058: }
059:
060: public PropertyData getPropertyData(SComponent component) {
061: if (component instanceof TablePanel) {
062: TablePanel tablePanel = (TablePanel) component;
063: return tablePanel.getPropertyData();
064: }
065:
066: return null;
067: }
068:
069: public static class TablePanel extends SContainer implements
070: PropertyChangeListener {
071: protected XTable table;
072: protected PropertyData propertyData;
073: private EditorWindow window;
074: private List<? extends EditorAction> actions;
075:
076: public TablePanel(PropertyMeta propertyMeta) {
077: super (new SGridBagLayout());
078:
079: table = new XTable();
080:
081: actions = (List<? extends EditorAction>) propertyMeta
082: .getAttribute(Editor.ACTIONS);
083: if (actions == null)
084: actions = createActions();
085:
086: int selectionMode = SListSelectionModel.NO_SELECTION;
087: for (EditorAction action : actions) {
088: action.setComponent(this );
089: if (action.getValue(EditorAction.SELECTION_MODE) != null)
090: selectionMode = Math
091: .max(
092: selectionMode,
093: (Integer) action
094: .getValue(EditorAction.SELECTION_MODE));
095: }
096: table.setSelectionMode(selectionMode);
097:
098: initialize();
099: }
100:
101: protected List<? extends EditorAction> createActions() {
102: return Arrays.asList(new ManageAction(), new AddAction());
103: }
104:
105: protected void initialize() {
106: GridBagConstraints constraints = new GridBagConstraints();
107: XScrollPane scrollPane = new XScrollPane(table, 6);
108: constraints.gridwidth = actions.size() + 1;
109: add(scrollPane, constraints);
110:
111: constraints.insets = new Insets(10, 0, 10, 10);
112: constraints.weightx = .1;
113: constraints.gridy = 2;
114: constraints.gridwidth = 1;
115:
116: for (Action action : actions)
117: add(new XButton(action), constraints);
118:
119: constraints.weightx = .8;
120: constraints.gridwidth = GridBagConstraints.REMAINDER;
121:
122: add(new SLabel(), constraints);
123: }
124:
125: public PropertyData getPropertyData() {
126: return propertyData;
127: }
128:
129: public void setPropertyData(PropertyData propertyData) {
130: this .propertyData = propertyData;
131: for (EditorAction action : actions)
132: action.setPropertyData(propertyData);
133:
134: CompositionObjectEditor objectEditor = getObjectEditor();
135: setTableModel(objectEditor.getTableModel());
136: setTableColumnModel(objectEditor.getTableColumnModel());
137:
138: getTableModel().addTableModelListener(
139: new TableModelListener() {
140: public void tableChanged(TableModelEvent e) {
141: BeanData beanData = TablePanel.this .propertyData
142: .getBeanData();
143: final Object value = TablePanel.this .propertyData
144: .getValue();
145: TablePanel.this .propertyData.revalidate();
146: PropertyChangeListener[] propertyChangeListeners = beanData
147: .getPropertyChangeListeners(TablePanel.this .propertyData
148: .getPropertyMeta()
149: .getName());
150: for (PropertyChangeListener propertyChangeListener : propertyChangeListeners) {
151: propertyChangeListener
152: .propertyChange(new PropertyChangeEvent(
153: beanData,
154: TablePanel.this .propertyData
155: .getPropertyMeta()
156: .getName(),
157: value, value));
158: }
159: }
160: });
161: }
162:
163: public XTable getTable() {
164: return table;
165: }
166:
167: public TableModel getTableModel() {
168: return table.getModel();
169: }
170:
171: public void setTableModel(TableModel model) {
172: table.setModel(model);
173: }
174:
175: public STableColumnModel getTableColumnModel() {
176: return table.getColumnModel();
177: }
178:
179: public void setTableColumnModel(STableColumnModel columnModel) {
180: table.setColumnModel(columnModel);
181: table.addClickListener(linkIndex(columnModel),
182: new XTableClickListener() {
183: public void clickOccured(int row, int col) {
184: EditorWindow window = getEditorWindow();
185: ObjectEditor editor = window.getEditor();
186: if (getTableModel() instanceof GenericObjectTableModel) {
187: GenericObjectTableModel genericObjectTableModel = (GenericObjectTableModel) getTableModel();
188: editor
189: .setObject(genericObjectTableModel
190: .getRow(row));
191: } else if (getTableModel() instanceof ListTableModel) {
192: ListTableModel listTableModel = (ListTableModel) getTableModel();
193: editor.setObject(listTableModel
194: .getList().get(row));
195: }
196:
197: editor.showForm();
198: Window parent = Window.currentWindow(table);
199: Client.getInstance().pushWindow(parent,
200: window);
201: }
202: });
203: }
204:
205: private EditorWindow getEditorWindow() {
206: EditorWindow window = (EditorWindow) getClientProperty("editorWindow");
207: if (window == null) {
208: ObjectEditor editor = getObjectEditor();
209: window = new EditorWindow(editor);
210: window.addPopAction(new PopAction());
211: putClientProperty("editorWindow", window);
212: }
213: return window;
214: }
215:
216: private CompositionObjectEditor getObjectEditor() {
217: CompositionObjectEditor objectEditor = (CompositionObjectEditor) propertyData
218: .getPropertyMeta().getAttribute("objectEditor");
219: if (objectEditor == null)
220: objectEditor = (CompositionObjectEditor) Editors
221: .get(propertyData.getPropertyMeta().getType());
222: return objectEditor;
223: }
224:
225: private int linkIndex(STableColumnModel columnModel) {
226: int columnCount = columnModel.getColumnCount();
227: for (int i = 0; i < columnCount; i++) {
228: if ("key".equals(columnModel.getColumn(i)
229: .getIdentifier()))
230: return i;
231: }
232: return 0;
233: }
234:
235: public void setEnabled(boolean enabled) {
236: table.setEnabled(enabled);
237: for (Action action : actions)
238: action.setEnabled(enabled);
239:
240: }
241:
242: public void propertyChange(PropertyChangeEvent evt) {
243: if (getTableModel() instanceof ListTableModel) {
244: final Object value = propertyData.getValue();
245: if (value != null)
246: ((ListTableModel) getTableModel())
247: .setList((List) value);
248: }
249: }
250:
251: }
252:
253: public static class AddAction extends AbstractEditorAction {
254: public AddAction() {
255: putValue(Action.ACTION_COMMAND_KEY, ".buttons.add");
256: }
257:
258: public void actionPerformed(ActionEvent e) {
259: EditorWindow window = getEditorWindow();
260: ObjectEditor editor = window.getEditor();
261: editor.setObject(null);
262: editor.showForm();
263: Window parent = Window.currentWindow(getComponent());
264: Client.getInstance().pushWindow(parent, window);
265: }
266:
267: private EditorWindow getEditorWindow() {
268: EditorWindow window = (EditorWindow) getComponent()
269: .getClientProperty("editorWindow");
270: if (window == null) {
271: ObjectEditor editor = getObjectEditor();
272: window = new EditorWindow(editor);
273: window.addPopAction(new PopAction());
274: getComponent()
275: .putClientProperty("editorWindow", window);
276: }
277: return window;
278: }
279:
280: private CompositionObjectEditor getObjectEditor() {
281: CompositionObjectEditor objectEditor = (CompositionObjectEditor) getPropertyMeta()
282: .getAttribute("objectEditor");
283: if (objectEditor == null)
284: objectEditor = (CompositionObjectEditor) Editors
285: .get(getPropertyMeta().getType());
286: return objectEditor;
287: }
288: }
289:
290: public static class ManageAction extends AbstractEditorAction {
291: public ManageAction() {
292: putValue(Action.ACTION_COMMAND_KEY, ".buttons.manage");
293: }
294:
295: public void actionPerformed(ActionEvent e) {
296: EditorWindow window = getEditorWindow();
297: ObjectEditor editor = window.getEditor();
298: editor.setObject(null);
299: editor.showList();
300: Window parent = Window.currentWindow(getComponent());
301: Client.getInstance().pushWindow(parent, window);
302: }
303:
304: private EditorWindow getEditorWindow() {
305: EditorWindow window = (EditorWindow) getComponent()
306: .getClientProperty("editorWindow");
307: if (window == null) {
308: ObjectEditor editor = getObjectEditor();
309: window = new EditorWindow(editor);
310: window.addPopAction(new PopAction());
311: getComponent()
312: .putClientProperty("editorWindow", window);
313: }
314: return window;
315: }
316:
317: private CompositionObjectEditor getObjectEditor() {
318: CompositionObjectEditor objectEditor = (CompositionObjectEditor) getPropertyMeta()
319: .getAttribute("objectEditor");
320: if (objectEditor == null)
321: objectEditor = (CompositionObjectEditor) Editors
322: .get(getPropertyMeta().getType());
323: return objectEditor;
324: }
325: }
326:
327: private static class PopAction extends AbstractObjectAction {
328: public PopAction() {
329: putValue(Action.ACTION_COMMAND_KEY, ".buttons.back");
330: }
331:
332: public void actionPerformed(ObjectActionEvent event) {
333: }
334:
335: public void setEnabled(boolean enabled) {
336: }
337: }
338: }
|