001: package org.osbl.client.wings.action;
002:
003: import java.awt.event.ActionEvent;
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import org.wings.SIcon;
008: import org.wings.util.SessionLocal;
009:
010: import org.osbl.client.wings.form.*;
011: import org.osbl.client.wings.form.editor.EditorWindow;
012: import org.osbl.client.wings.shell.Window;
013: import org.osbl.client.wings.shell.Client;
014: import org.osbl.client.action.AbstractObjectAction;
015: import org.osbl.client.action.ObjectActionEvent;
016: import org.osbl.*;
017:
018: import javax.swing.*;
019:
020: public class ShowEditorAction extends NavigationAction {
021: private ObjectLocators objectLocators;
022:
023: private Window parentWindow;
024: private EditorWindow editorWindow;
025:
026: protected Class type;
027: protected Map<String, Object> filters = new HashMap<String, Object>();
028:
029: static final SessionLocal windows = new SessionLocal() {
030: protected Object initialValue() {
031: return new HashMap();
032: }
033: };
034:
035: public ShowEditorAction(Window parentWindow, Class type,
036: String command, SIcon icon) {
037: super (command, icon);
038: this .parentWindow = parentWindow;
039: this .type = type;
040: putValue(PERMISSION_KEY, type.getName() + "[r]");
041: }
042:
043: public ShowEditorAction(Window parentWindow, Class type,
044: String command) {
045: super (command);
046: this .parentWindow = parentWindow;
047: this .type = type;
048: putValue(PERMISSION_KEY, type.getName() + "[r]");
049: }
050:
051: public ShowEditorAction(Class type, String command, SIcon icon) {
052: super (command, icon);
053: this .type = type;
054: putValue(PERMISSION_KEY, type.getName() + "[r]");
055: }
056:
057: public ShowEditorAction(Class type, String command) {
058: super (command);
059: this .type = type;
060: putValue(PERMISSION_KEY, type.getName() + "[r]");
061: }
062:
063: public void setFilter(String property, Object value) {
064: filters.put(property, value);
065: }
066:
067: public Object getFilter(String property) {
068: return filters.get(property);
069: }
070:
071: public void clearFilters() {
072: filters.clear();
073: }
074:
075: public void setParentWindow(Window parentWindow) {
076: this .parentWindow = parentWindow;
077: }
078:
079: public void actionPerformed(ActionEvent event) {
080: EditorWindow editorWindow = getEditorWindow();
081: ObjectEditor editor = editorWindow.getEditor();
082: applyFilters(editor.getList());
083: editor.showList();
084: pushWindow(editorWindow);
085: }
086:
087: public void navigate(Object object) {
088: EditorWindow editorWindow = getEditorWindow();
089: ObjectEditor editor = editorWindow.getEditor();
090: editor.setObject(object);
091: editor.showForm();
092: pushWindow(editorWindow);
093: }
094:
095: protected void pushWindow(EditorWindow editorWindow) {
096: if (parentWindow != null)
097: Client.getInstance().pushWindow(parentWindow, editorWindow);
098: else
099: Client.getInstance().pushWindow(editorWindow);
100: }
101:
102: public void navigate(Map<String, String> query) {
103: EditorWindow editorWindow = getEditorWindow();
104: ObjectEditor editor = editorWindow.getEditor();
105:
106: ObjectLocator objectLocator = getObjectLocators().get(type);
107: if (objectLocator != null) {
108: Object object = objectLocator.objectByQuery(query);
109: if (object != null) {
110: editor.setObject(object);
111: editor.showForm();
112: } else {
113: applyFilters(editor.getList());
114: editor.showList();
115: }
116: } else {
117: applyFilters(editor.getList());
118: editor.showList();
119: }
120:
121: pushWindow(editorWindow);
122: }
123:
124: private void applyFilters(ObjectList objectList) {
125: if (objectList instanceof FilterableObjectList) {
126: FilterableObjectList filterableObjectList = (FilterableObjectList) objectList;
127: filterableObjectList.clearFilters();
128: if (filters != null && filters.size() > 0)
129: for (Map.Entry<String, Object> entry : filters
130: .entrySet())
131: filterableObjectList.setFilter(entry.getKey(),
132: entry.getValue());
133: }
134: }
135:
136: private EditorWindow getEditorWindow() {
137: if (parentWindow != null) {
138: if (editorWindow == null) {
139: ObjectEditor editor = Editors.get(type);
140: editorWindow = new EditorWindow(editor);
141: editorWindow.addPopAction(new DefaultPopAction());
142: }
143: return editorWindow;
144: } else {
145: Map<Class, EditorWindow> map = (Map<Class, EditorWindow>) windows
146: .get();
147: EditorWindow editorWindow = map.get(type);
148: if (editorWindow == null) {
149: ObjectEditor editor = Editors.get(type);
150: editorWindow = new EditorWindow(editor);
151: map.put(type, editorWindow);
152: }
153: return editorWindow;
154: }
155: }
156:
157: public ObjectLocators getObjectLocators() {
158: if (objectLocators == null)
159: objectLocators = (ObjectLocators) ServiceProvider
160: .getInstance().getService("ObjectLocators");
161: return objectLocators;
162: }
163:
164: public static class DefaultPopAction extends AbstractObjectAction {
165: public DefaultPopAction() {
166: putValue(Action.ACTION_COMMAND_KEY, ".buttons.back");
167: }
168:
169: public void actionPerformed(ObjectActionEvent event) {
170: }
171: }
172: }
|