001: package org.osbl.client.wings.form;
002:
003: import org.wings.SComponent;
004: import org.wings.SButton;
005: import org.osbl.client.wings.shell.*;
006:
007: import javax.swing.*;
008: import java.util.List;
009: import java.util.ArrayList;
010:
011: public class DelegateEnvironment implements Environment {
012: Environment delegate;
013: boolean active;
014:
015: private SComponent contentPane;
016: private SButton defaultButton;
017:
018: private String titleCode;
019: private Object[] titleArguments;
020:
021: protected boolean hasChanges;
022: protected Client.Message statusMessage;
023: protected List<SComponent> controls = new ArrayList<SComponent>();
024: protected List<Tool> tools = new ArrayList<Tool>();
025: protected List<Action> developmentActions = new ArrayList<Action>();
026:
027: protected boolean nestedHasChanges;
028: protected Client.Message nestedStatusMessage;
029: protected List<SComponent> nestedControls = new ArrayList<SComponent>();
030: protected List<Tool> nestedTools = new ArrayList<Tool>();
031: protected List<Action> nestedDevelopmentActions = new ArrayList<Action>();
032:
033: protected DelegateEnvironment nestedEnvironment;
034:
035: public DelegateEnvironment() {
036: }
037:
038: public DelegateEnvironment(boolean active) {
039: this .active = active;
040: }
041:
042: public DelegateEnvironment(Environment delegate) {
043: this .delegate = delegate;
044: }
045:
046: public void setDelegate(Environment delegate) {
047: this .delegate = delegate;
048: if (active && delegate != null)
049: activate(delegate);
050: }
051:
052: public Environment getDelegate() {
053: return delegate;
054: }
055:
056: public void setNestedEnvironment(
057: DelegateEnvironment nestedEnvironment) {
058: this .nestedEnvironment = nestedEnvironment;
059: if (nestedEnvironment != null) {
060: // override contentPane, defaultButton, title and statusMessage
061: // mix others
062: nestedEnvironment.setActive(true);
063: nestedEnvironment.setDelegate(new Environment() {
064: public void setContentPane(SComponent contentPane) {
065: if (active && delegate != null)
066: delegate.setContentPane(contentPane);
067: }
068:
069: public void setDefaultButton(SButton defaultButton) {
070: if (active && delegate != null)
071: delegate.setDefaultButton(defaultButton);
072: }
073:
074: public void setTitle(String titleCode,
075: Object... titleArguments) {
076: if (active && delegate != null)
077: delegate.setTitle(titleCode, titleArguments);
078: }
079:
080: public void setStatusMessage(
081: Client.Message statusMessage) {
082: if (active && delegate != null)
083: delegate.setStatusMessage(statusMessage);
084: nestedStatusMessage = statusMessage;
085: }
086:
087: public void setHasChanges(boolean hasChanges) {
088: if (active && delegate != null)
089: delegate.setHasChanges(hasChanges);
090: nestedHasChanges = hasChanges;
091: }
092:
093: public boolean getHasChanges() {
094: return nestedHasChanges;
095: }
096:
097: public void addControl(SComponent component) {
098: if (active && delegate != null)
099: delegate.addControl(component);
100: nestedControls.add(component);
101: }
102:
103: public void removeControl(SComponent component) {
104: if (active && delegate != null)
105: delegate.removeControl(component);
106: nestedControls.remove(component);
107: }
108:
109: public void setControls(List<SComponent> controls) {
110: nestedControls.clear();
111: nestedControls.addAll(controls);
112: if (active && delegate != null)
113: delegate.setControls(union(
114: DelegateEnvironment.this .controls,
115: nestedControls));
116: }
117:
118: public void addTool(Tool tool) {
119: if (active && delegate != null)
120: delegate.addTool(tool);
121: nestedTools.add(tool);
122: }
123:
124: public void removeTool(Tool tool) {
125: if (active && delegate != null)
126: delegate.removeTool(tool);
127: nestedTools.remove(tool);
128: }
129:
130: public void setTools(List<Tool> tools) {
131: nestedTools.clear();
132: nestedTools.addAll(tools);
133: if (active && delegate != null)
134: delegate.setTools(union(
135: DelegateEnvironment.this .tools,
136: nestedTools));
137: }
138:
139: public void addDevelopmentAction(Action action) {
140: if (active && delegate != null)
141: delegate.addDevelopmentAction(action);
142: nestedDevelopmentActions.add(action);
143: }
144:
145: public void removeDevelopmentAction(Action action) {
146: if (active && delegate != null)
147: delegate.removeDevelopmentAction(action);
148: nestedDevelopmentActions.remove(action);
149: }
150:
151: public void setDevelopmentActions(
152: List<Action> developmentactions) {
153: nestedDevelopmentActions.clear();
154: nestedDevelopmentActions.addAll(developmentactions);
155: if (active && delegate != null)
156: delegate
157: .setDevelopmentActions(union(
158: DelegateEnvironment.this .developmentActions,
159: nestedDevelopmentActions));
160: }
161:
162: public boolean isActive() {
163: return true;
164: }
165:
166: public void setDelegate(Environment delegate) {
167: }
168:
169: public Environment getDelegate() {
170: return delegate;
171: }
172:
173: public void setActive(boolean active) {
174: }
175:
176: public void setVisible(boolean visible) {
177: }
178: });
179: }
180: }
181:
182: private List union(List first, List second) {
183: List union = new ArrayList(first.size() + second.size());
184: union.addAll(first);
185: union.addAll(second);
186: return union;
187: }
188:
189: public void setActive(boolean active) {
190: this .active = active;
191: if (active && delegate != null)
192: activate(delegate);
193: }
194:
195: public boolean isActive() {
196: return active;
197: }
198:
199: private void activate(Environment delegate) {
200: delegate
201: .setContentPane(nestedEnvironment != null ? nestedEnvironment.contentPane
202: : contentPane);
203: delegate
204: .setDefaultButton(nestedEnvironment != null ? nestedEnvironment.defaultButton
205: : defaultButton);
206: delegate
207: .setTitle(
208: nestedEnvironment != null ? nestedEnvironment.titleCode
209: : titleCode,
210: nestedEnvironment != null ? nestedEnvironment.titleArguments
211: : titleArguments);
212: delegate.setControls(union(controls, nestedControls));
213: delegate.setTools(union(tools, nestedTools));
214: delegate.setDevelopmentActions(union(developmentActions,
215: nestedDevelopmentActions));
216: delegate
217: .setStatusMessage(nestedEnvironment != null ? nestedEnvironment.statusMessage
218: : statusMessage);
219: delegate
220: .setHasChanges(nestedEnvironment != null ? nestedEnvironment.hasChanges
221: : hasChanges);
222: }
223:
224: public void setContentPane(SComponent contentPane) {
225: if (active && delegate != null)
226: delegate
227: .setContentPane(nestedEnvironment != null ? nestedEnvironment.contentPane
228: : contentPane);
229: this .contentPane = contentPane;
230: }
231:
232: public void setDefaultButton(SButton defaultButton) {
233: if (active && delegate != null)
234: delegate
235: .setDefaultButton(nestedEnvironment != null ? nestedEnvironment.defaultButton
236: : defaultButton);
237: this .defaultButton = defaultButton;
238: }
239:
240: public void setTitle(String titleCode, Object... titleArguments) {
241: if (active && delegate != null)
242: delegate
243: .setTitle(
244: nestedEnvironment != null ? nestedEnvironment.titleCode
245: : titleCode,
246: nestedEnvironment != null ? nestedEnvironment.titleArguments
247: : titleArguments);
248: this .titleCode = titleCode;
249: this .titleArguments = titleArguments;
250: }
251:
252: public void addControl(SComponent component) {
253: if (active && delegate != null)
254: delegate.addControl(component);
255: this .controls.add(component);
256: }
257:
258: public void removeControl(SComponent component) {
259: if (active && delegate != null)
260: delegate.removeControl(component);
261: this .controls.remove(component);
262: }
263:
264: public void setControls(List<SComponent> controls) {
265: if (active && delegate != null)
266: delegate.setControls(union(controls, nestedControls));
267: this .controls.clear();
268: this .controls.addAll(controls);
269: }
270:
271: public void addTool(Tool tool) {
272: if (active && delegate != null)
273: delegate.addTool(tool);
274: this .tools.add(tool);
275: }
276:
277: public void removeTool(Tool tool) {
278: if (active && delegate != null)
279: delegate.removeTool(tool);
280: this .tools.remove(tool);
281: }
282:
283: public void setTools(List<Tool> tools) {
284: if (active && delegate != null)
285: delegate.setTools(union(tools, nestedTools));
286: this .tools.clear();
287: this .tools.addAll(tools);
288: }
289:
290: public void addDevelopmentAction(Action action) {
291: if (active && delegate != null)
292: delegate.addDevelopmentAction(action);
293: this .developmentActions.add(action);
294: }
295:
296: public void removeDevelopmentAction(Action action) {
297: if (active && delegate != null)
298: delegate.removeDevelopmentAction(action);
299: this .developmentActions.remove(action);
300: }
301:
302: public void setDevelopmentActions(List<Action> developmentActions) {
303: if (active && delegate != null)
304: delegate.setDevelopmentActions(union(developmentActions,
305: nestedDevelopmentActions));
306: this .developmentActions.clear();
307: this .developmentActions.addAll(developmentActions);
308: }
309:
310: public void setStatusMessage(Client.Message statusMessage) {
311: if (active && delegate != null)
312: delegate
313: .setStatusMessage(nestedEnvironment != null ? nestedEnvironment.statusMessage
314: : statusMessage);
315: this .statusMessage = statusMessage;
316: }
317:
318: public void setHasChanges(boolean hasChanges) {
319: if (active && delegate != null)
320: delegate
321: .setHasChanges(nestedEnvironment != null ? nestedEnvironment.hasChanges
322: : hasChanges);
323: this .hasChanges = hasChanges;
324: }
325:
326: public boolean getHasChanges() {
327: return nestedEnvironment != null ? nestedEnvironment.hasChanges
328: : hasChanges;
329: }
330: }
|