001: package org.osbl.client.wings.form;
002:
003: import org.osbl.client.action.*;
004: import org.osbl.client.wings.*;
005: import org.osbl.client.wings.shell.*;
006:
007: import javax.swing.*;
008: import java.util.*;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011:
012: /**
013: * A generic base class, that implements basic ObjectViewer functionality.
014: * It encapsulates an ObjectForm.
015: */
016: public abstract class GenericObjectViewer implements ObjectViewer {
017: protected Environment environment = new DelegateEnvironment(true);
018:
019: protected ObjectList objectList;
020: protected ObjectForm objectForm;
021: private boolean editing;
022: private Object object;
023:
024: protected ShowAction showAction = new ShowAction(base());
025: protected PreviousAction previousAction = new PreviousAction(base());
026: protected NextAction nextAction = new NextAction(base());
027: protected ListAction listAction = new ListAction(base());
028: protected SelectObjectAction selectObjectAction = new SelectObjectAction(
029: base());
030: protected DeselectObjectAction deselectObjectAction = new DeselectObjectAction(
031: base());
032: protected GenericObjectForm.DelegateObjectAction formSelectAction = new GenericObjectForm.DelegateObjectAction(
033: selectObjectAction);
034: protected GenericObjectList.DelegateObjectAction listSelectAction = new GenericObjectList.DelegateObjectAction(
035: selectObjectAction);
036: protected GenericObjectForm.DelegateObjectAction formDeselectAction = new GenericObjectForm.DelegateObjectAction(
037: deselectObjectAction);
038: protected GenericObjectList.DelegateObjectAction listDeselectAction = new GenericObjectList.DelegateObjectAction(
039: deselectObjectAction);
040:
041: private List<ObjectAction> objectActions = new LinkedList<ObjectAction>();
042: private Window window;
043:
044: public GenericObjectViewer() {
045: initializeViewer();
046: environment.setActive(true);
047: }
048:
049: public Environment getEnvironment() {
050: return environment;
051: }
052:
053: public void showList() {
054: editing = false;
055:
056: getList();
057:
058: if (objectForm != null)
059: objectForm.getEnvironment().setActive(false);
060: objectList.getEnvironment().setActive(true);
061:
062: objectList.refresh();
063: }
064:
065: public void showForm() {
066: editing = true;
067:
068: getForm();
069:
070: if (objectList != null)
071: objectList.getEnvironment().setActive(false);
072: objectForm.getEnvironment().setActive(true);
073:
074: objectForm.setObject(object);
075: }
076:
077: /**
078: * Initializations like installing global object actions go here.
079: * Override this method for your own initializations.
080: */
081: protected void initializeViewer() {
082: }
083:
084: protected ObjectForm getForm() {
085: if (objectForm == null) {
086: objectForm = initializeForm();
087: objectForm.setViewMode(ObjectForm.VIEW);
088: formControls(objectForm.getEnvironment());
089: objectForm.getEnvironment().setDelegate(environment);
090: formSelectAction.setForm(objectForm);
091: formDeselectAction.setForm(objectForm);
092: }
093: return objectForm;
094: }
095:
096: protected void formControls(Environment environment) {
097: environment.addControl(new XButton(listAction));
098: environment.addControl(new XButton(previousAction));
099: environment.addControl(new XButton(nextAction));
100: }
101:
102: /**
103: * Create and initialize the conform component.
104: * @return the conform component
105: */
106: protected abstract ObjectForm initializeForm();
107:
108: protected ObjectList getList() {
109: if (objectList == null) {
110: objectList = initializeList();
111: listControls(objectList.getEnvironment());
112: objectList.getEnvironment().setDelegate(environment);
113: listSelectAction.setList(objectList);
114: listDeselectAction.setList(objectList);
115:
116: objectList.setLinkAction("key", new AbstractObjectAction() {
117: public void actionPerformed(ObjectActionEvent e) {
118: Object object = e.getTargets()[0];
119: objectList.setCurrent(object);
120: setObject(object);
121:
122: if (showAction.isEnabled())
123: showAction.actionPerformed(new ActionEvent(
124: GenericObjectViewer.this , 0, "edit"));
125: }
126: });
127:
128: /*
129: this.objectList.setSelectionMode(STable.SINGLE_SELECTION);
130: this.objectList.addSelectionListener(new ListSelectionListener() {
131: public void valueChanged(ListSelectionEvent e) {
132: if (e.getValueIsAdjusting())
133: return;
134:
135: Object selectedObject = objectList.getSelectedObject();
136: if (selectedObject == null) {
137: objectList.setSelectedObject(getObject());
138: return;
139: }
140: setObject(selectedObject);
141: if (showAction.isEnabled())
142: showAction.actionPerformed(new ActionEvent(GenericObjectViewer.this, 0, "edit"));
143:
144: previousAction.setEnabled(objectList.hasPrevious());
145: nextAction.setEnabled(objectList.hasNext());
146: }
147: });
148: */
149: }
150: return objectList;
151: }
152:
153: protected void listControls(Environment environment) {
154: }
155:
156: /**
157: * Create and initialize the list component.
158: * @return the list component
159: */
160: protected abstract ObjectList initializeList();
161:
162: public Object getObject() {
163: return object;
164: }
165:
166: public void setObject(Object object) {
167: this .object = object;
168: }
169:
170: public void setSelectionMode(int selectionMode) {
171: getList().setSelectionMode(selectionMode);
172: }
173:
174: public void setSelectedObject(Object object) {
175: getList().setSelectedObject(object);
176: }
177:
178: public Object getSelectedObject() {
179: return getList().getSelectedObject();
180: }
181:
182: public List getSelectedObjects() {
183: return getList().getSelectedObjects();
184: }
185:
186: public void setSelectedObjects(List objects) {
187: getList().setSelectedObjects(objects);
188: }
189:
190: public abstract Class getType();
191:
192: /**
193: * Provide the ChooseAction and append the object list actions if any.
194: * @return a list of actions, displayed along with the list.
195: */
196: protected List<Action> getListActions() {
197: return null;
198: }
199:
200: /**
201: * Provide the ListAction, PreviousAction, NextAction and SaveAction and append the object conform actions if any.
202: * @return a list of actions, displayed along with the conform.
203: */
204: protected List<Action> getFormActions() {
205: return null;
206: }
207:
208: protected String base() {
209: return getType().getName();
210: }
211:
212: private String unsavedchangesMessage(String name) {
213: String message = Client.getInstance().getResourceProvider()
214: .getMessage("businessobject.messages.unsavedChanges",
215: name);
216: if (objectForm instanceof GenericObjectForm) {
217: GenericObjectForm genericObjectForm = (GenericObjectForm) objectForm;
218: String changes = genericObjectForm.changeStatus
219: .composeMessageDetail();
220: message = message + "\n\n" + changes;
221: }
222: return message;
223: }
224:
225: protected class ListAction extends AbstractAction {
226: public ListAction(String base) {
227: putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.list");
228: putValue(AuthorizedAction.PERMISSION_KEY, base + "[r]");
229: }
230:
231: public void actionPerformed(ActionEvent e) {
232: if (objectForm.hasChanges()) {
233: String name = Client.getInstance()
234: .getResourceProvider().getMessage(
235: getType().getName());
236: String message = unsavedchangesMessage(name);
237: XOptionPane.showConfirmDialog(null, message, name,
238: new ActionListener() {
239: public void actionPerformed(ActionEvent e) {
240: if (XOptionPane.YES_ACTION == e
241: .getActionCommand())
242: showList();
243: }
244: });
245: } else
246: showList();
247: }
248: }
249:
250: protected class NextAction extends AbstractAction {
251: public NextAction(String base) {
252: putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.next");
253: putValue(AuthorizedAction.PERMISSION_KEY, base + "[r]");
254: setEnabled(false);
255: }
256:
257: public void actionPerformed(ActionEvent e) {
258: if (objectForm.hasChanges()) {
259: String name = Client.getInstance()
260: .getResourceProvider().getMessage(
261: objectForm.getType().getName());
262: String message = unsavedchangesMessage(name);
263: XOptionPane.showConfirmDialog(null, message, name,
264: new ActionListener() {
265: public void actionPerformed(ActionEvent e) {
266: if (XOptionPane.YES_ACTION == e
267: .getActionCommand())
268: next();
269: }
270: });
271: } else
272: next();
273: }
274:
275: private void next() {
276: objectList.next();
277: setObject(objectList.getCurrent());
278: showForm();
279: previousAction.setEnabled(objectList.hasPrevious());
280: nextAction.setEnabled(objectList.hasNext());
281: }
282: }
283:
284: protected class PreviousAction extends AbstractAction {
285: public PreviousAction(String base) {
286: putValue(Action.ACTION_COMMAND_KEY, base
287: + ".buttons.previous");
288: putValue(AuthorizedAction.PERMISSION_KEY, base + "[r]");
289: setEnabled(false);
290: }
291:
292: public void actionPerformed(ActionEvent e) {
293: if (objectForm.hasChanges()) {
294: String name = Client.getInstance()
295: .getResourceProvider().getMessage(
296: getType().getName());
297: String message = unsavedchangesMessage(name);
298: XOptionPane.showConfirmDialog(null, message, name,
299: new ActionListener() {
300: public void actionPerformed(ActionEvent e) {
301: if (XOptionPane.YES_ACTION == e
302: .getActionCommand())
303: previous();
304: }
305: });
306: } else
307: previous();
308: }
309:
310: private void previous() {
311: objectList.previous();
312: setObject(objectList.getCurrent());
313: showForm();
314: previousAction.setEnabled(objectList.hasPrevious());
315: nextAction.setEnabled(objectList.hasNext());
316: }
317: }
318:
319: protected class AddAction extends AbstractAction {
320: public AddAction(String base) {
321: putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.add");
322: putValue(AuthorizedAction.PERMISSION_KEY, base + "[w]");
323: }
324:
325: public void actionPerformed(ActionEvent e) {
326: setObject(null);
327: showForm();
328: objectList.setSelectedObject(null);
329: previousAction.setEnabled(false);
330: nextAction.setEnabled(false);
331: }
332: }
333:
334: protected class ShowAction extends AbstractAction {
335: public ShowAction(String base) {
336: putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.show");
337: putValue(AuthorizedAction.PERMISSION_KEY, base + "[r]");
338: }
339:
340: public void actionPerformed(ActionEvent e) {
341: showForm();
342: previousAction.setEnabled(objectList.hasPrevious());
343: nextAction.setEnabled(objectList.hasNext());
344: }
345: }
346:
347: protected class SelectObjectAction extends AbstractObjectAction {
348: public SelectObjectAction(String base) {
349: putValue(Action.NAME, Client.getInstance()
350: .getResourceProvider().getMessage(
351: base + ".buttons.select"));
352: putValue(Action.ACTION_COMMAND_KEY, Client.getInstance()
353: .getResourceProvider().getMessage(
354: base + ".buttons.list"));
355: }
356:
357: public void actionPerformed(ObjectActionEvent event) {
358: try {
359: List list = objectList.getSelectedObjects();
360: list.addAll(Arrays.asList(event.getTargets()));
361: objectList.setSelectedObjects(list);
362:
363: String name = Client.getInstance()
364: .getResourceProvider().getMessage(
365: getType().getName());
366: String message = Client.getInstance()
367: .getResourceProvider().getMessage(
368: "businessobject.messages.selected",
369: name, list.size());
370: getEnvironment().setStatusMessage(
371: new Client.Message(Client.Message.TYPE_INFO,
372: message));
373: } catch (Exception e) {
374: getEnvironment().setStatusMessage(
375: new Client.Message(Client.Message.TYPE_ERROR, e
376: .getMessage()));
377: e.printStackTrace();
378: }
379:
380: if (editing)
381: showList();
382: else
383: objectList.refresh();
384: }
385: }
386:
387: protected class DeselectObjectAction extends AbstractObjectAction {
388: public DeselectObjectAction(String base) {
389: putValue(Action.NAME, Client.getInstance()
390: .getResourceProvider().getMessage(
391: base + ".buttons.deselect"));
392: putValue(Action.ACTION_COMMAND_KEY, Client.getInstance()
393: .getResourceProvider().getMessage(
394: base + ".buttons.list"));
395: }
396:
397: public void actionPerformed(ObjectActionEvent event) {
398: try {
399: List list = objectList.getSelectedObjects();
400: list.removeAll(Arrays.asList(event.getTargets()));
401: objectList.setSelectedObjects(list);
402:
403: String name = Client.getInstance()
404: .getResourceProvider().getMessage(
405: getType().getName());
406: String message = Client.getInstance()
407: .getResourceProvider().getMessage(
408: "businessobject.messages.selected",
409: name, list.size());
410: getEnvironment().setStatusMessage(
411: new Client.Message(Client.Message.TYPE_INFO,
412: message));
413: } catch (Exception e) {
414: getEnvironment().setStatusMessage(
415: new Client.Message(Client.Message.TYPE_ERROR, e
416: .getMessage()));
417: e.printStackTrace();
418: }
419:
420: if (editing)
421: showList();
422: else
423: objectList.refresh();
424: }
425: }
426: }
|