001: package org.osbl.client.wings.shell;
002:
003: import org.wings.SComponent;
004: import org.wings.SButton;
005:
006: import javax.swing.*;
007: import java.util.*;
008:
009: public class Window implements Environment {
010: protected Window parent;
011:
012: boolean active;
013: boolean visible;
014: private SComponent contentPane;
015:
016: private String titleCode;
017: private Object[] titleArguments;
018: private List<SComponent> controls;
019: private List<Tool> tools;
020: private List<Action> developmentActions;
021: private Client.Message statusMessage;
022: private boolean hasChanges;
023: private SButton defaultButton;
024:
025: protected final Client client;
026:
027: public Window() {
028: client = Client.getInstance();
029: }
030:
031: public Window(SComponent contentPane) {
032: this .contentPane = contentPane;
033: client = Client.getInstance();
034: }
035:
036: public Window(SComponent contentPane, String titleCode,
037: Object... titleArguments) {
038: this .contentPane = contentPane;
039: this .titleCode = titleCode;
040: this .titleArguments = titleArguments;
041: client = Client.getInstance();
042: }
043:
044: void setParent(Window parent) {
045: this .parent = parent;
046: }
047:
048: public Environment getParent() {
049: return parent;
050: }
051:
052: /**
053: * The respective window stack is active.
054: * @return
055: */
056: public boolean isActive() {
057: return active;
058: }
059:
060: public void setActive(boolean active) {
061: this .active = active;
062: install();
063: }
064:
065: /**
066: * The window is lying on top of its stack.
067: * @return
068: */
069: public boolean isVisible() {
070: return visible;
071: }
072:
073: public void setDelegate(Environment delegate) {
074: // TODO: the delegate is actually the process client
075: }
076:
077: public Environment getDelegate() {
078: return null;
079: }
080:
081: public void setVisible(boolean visible) {
082: this .visible = visible;
083: install();
084: }
085:
086: private void install() {
087: if (active && visible) {
088: client.setContent(contentPane);
089: client.setTitle(title());
090: client.setControls(controls);
091: client.setTools(tools);
092: client.setDevelopmentActions(developmentActions);
093: client.setStatusMessage(statusMessage);
094: client.setDefaultButton(defaultButton);
095: }
096: }
097:
098: private String title() {
099: String title = titleCode != null ? client.getResourceProvider()
100: .getMessage(titleCode, titleArguments) : null;
101: return parent != null ? parent.title() + " / " + title : title;
102: }
103:
104: public SComponent getContentPane() {
105: return contentPane;
106: }
107:
108: public void setContentPane(SComponent contentPane) {
109: if (this .contentPane != contentPane) {
110: this .contentPane = contentPane;
111: if (active && visible)
112: client.setContent(contentPane);
113: }
114: }
115:
116: public SButton getDefaultButton() {
117: return defaultButton;
118: }
119:
120: public void setDefaultButton(SButton defaultButton) {
121: if (this .defaultButton != defaultButton) {
122: this .defaultButton = defaultButton;
123: if (active && visible)
124: client.setDefaultButton(defaultButton);
125: }
126: }
127:
128: public void setTitle(String titleCode, Object... titleArguments) {
129: if (isDifferent(this .titleCode, titleCode)
130: || isDifferent(this .titleArguments, titleArguments)) {
131: this .titleCode = titleCode;
132: this .titleArguments = titleArguments;
133: if (active && visible)
134: client.setTitle(title());
135: }
136: }
137:
138: public String getTitleCode() {
139: return titleCode;
140: }
141:
142: public void setTitleCode(String titleCode) {
143: if (isDifferent(this .titleCode, titleCode)) {
144: this .titleCode = titleCode;
145: if (active && visible)
146: client.setTitle(title());
147: }
148: }
149:
150: public Object[] getTitleArguments() {
151: return titleArguments;
152: }
153:
154: public void setTitleArguments(Object[] titleArguments) {
155: if (isDifferent(this .titleArguments, titleArguments)) {
156: this .titleArguments = titleArguments;
157: if (active && visible)
158: client.setTitle(title());
159: }
160: }
161:
162: public List<SComponent> getControls() {
163: return controls;
164: }
165:
166: public void addControl(SComponent component) {
167: controls.add(component);
168: if (active && visible)
169: client.setControls(controls);
170: }
171:
172: public void removeControl(SComponent component) {
173: boolean changed = controls.remove(component);
174: if (changed && active && visible)
175: client.setControls(controls);
176: }
177:
178: public void setControls(List<SComponent> controls) {
179: if (isDifferent(this .controls, controls)) {
180: this .controls = controls;
181: if (active && visible)
182: client.setControls(controls);
183: }
184: }
185:
186: public List<Tool> getTools() {
187: return tools;
188: }
189:
190: public void addTool(Tool tool) {
191: tools.add(tool);
192: if (active && visible)
193: client.setTools(tools);
194: }
195:
196: public void removeTool(Tool tool) {
197: boolean changed = tools.remove(tool);
198: if (changed && active && visible)
199: client.setTools(tools);
200: }
201:
202: public void setTools(List<Tool> tools) {
203: if (isDifferent(this .tools, tools)) {
204: this .tools = tools;
205: if (active && visible)
206: client.setTools(tools);
207: }
208: }
209:
210: public List<Action> getDevelopmentActions() {
211: return developmentActions;
212: }
213:
214: public void addDevelopmentAction(Action action) {
215: developmentActions.add(action);
216: if (active && visible)
217: client.setDevelopmentActions(developmentActions);
218: }
219:
220: public void removeDevelopmentAction(Action action) {
221: boolean changed = developmentActions.remove(action);
222: if (changed && active && visible)
223: client.setDevelopmentActions(developmentActions);
224: }
225:
226: public void setDevelopmentActions(List<Action> actions) {
227: if (isDifferent(this .developmentActions, actions)) {
228: this .developmentActions = actions;
229: if (active && visible)
230: client.setDevelopmentActions(developmentActions);
231: }
232: }
233:
234: public Client.Message getStatusMessage() {
235: return statusMessage;
236: }
237:
238: public void setStatusMessage(Client.Message statusMessage) {
239: this .statusMessage = statusMessage;
240: if (active && visible)
241: client.setStatusMessage(statusMessage);
242: }
243:
244: public boolean getHasChanges() {
245: return hasChanges;
246: }
247:
248: public void setHasChanges(boolean hasChanges) {
249: this .hasChanges = hasChanges;
250: }
251:
252: protected static boolean isDifferent(Object oldObject,
253: Object newObject) {
254: return oldObject != newObject
255: && (oldObject == null || !oldObject.equals(newObject));
256: }
257:
258: protected static boolean isDifferent(Object[] oldObjects,
259: Object[] newObjects) {
260: return oldObjects != newObjects
261: && (oldObjects == null || !Arrays.equals(oldObjects,
262: newObjects));
263: }
264:
265: public String toString() {
266: return titleCode;
267: }
268:
269: @Deprecated
270: public static Window currentWindow(SComponent component) {
271: return Client.getInstance().currentWindow(component);
272: }
273:
274: public static Window currentWindow(Environment environment) {
275: do {
276: if (environment instanceof Window)
277: return (Window) environment;
278:
279: environment = environment.getDelegate();
280: } while (environment != null);
281:
282: return null;
283: }
284:
285: public static Window getWindow(Environment environment) {
286: if (!(environment instanceof Window)) {
287: do {
288: environment = environment.getDelegate();
289: } while (environment != null
290: && !(environment instanceof Window));
291: }
292: return (Window) environment;
293: }
294: }
|