001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.managers.windows;
019:
020: import java.lang.reflect.*;
021: import java.util.*;
022:
023: import net.ar.webonswing.helpers.*;
024: import net.ar.webonswing.wrapping.*;
025:
026: /**
027: * Implementacion simple de un administrador de ventanas; tiene la capacidad de
028: * apilar las ventanas que se van abriendo para poder trabajar con ventanas de
029: * tipo "modal".
030: *
031: * @author Fernando Damian Petrola
032: */
033: public class DefaultWindowManager implements WindowManager {
034: public static final String AFTER_HIDE_METHOD_NAME = "theAfterHideMethodName";
035: public static final String OPENER_WINDOW = "theOpenerWindow";
036: public static final String WINDOW_MANAGER_PROPERTY = "theWindowManager";
037:
038: protected Stack theStack;
039: protected Hashtable theOpenersTable;
040: private VisualWindow currentWindow;
041:
042: protected void showNormal(Object aWindowToShow) {
043: }
044:
045: protected void hideNormal(Object aWindow) {
046: }
047:
048: protected WindowManager getWindowManager(Object anOpenerWindow) {
049: VisualComponent theComponent = (VisualComponent) anOpenerWindow;
050: return (WindowManager) theComponent
051: .getClientProperty(WINDOW_MANAGER_PROPERTY);
052: }
053:
054: protected void setWindowManager(Object aWindow,
055: WindowManager aWindowManager) {
056: VisualComponent theComponent = (VisualComponent) aWindow;
057: theComponent.putClientProperty(WINDOW_MANAGER_PROPERTY,
058: aWindowManager);
059: }
060:
061: protected VisualWindow getNewVisualWindowFor(Object aWindowToShow) {
062: return (VisualWindow) aWindowToShow;
063: }
064:
065: protected boolean isWindowModal(Object aWindowToShow) {
066: return false;
067: }
068:
069: protected boolean isWindowEquals(Object aComponent,
070: VisualWindow theWindowWrapper) {
071: return aComponent.equals(theWindowWrapper);
072: }
073:
074: protected Object getRealWindow(Object aWindow) {
075: return aWindow;
076: }
077:
078: public DefaultWindowManager() {
079: theStack = new Stack();
080: theOpenersTable = new Hashtable();
081: }
082:
083: public void show(VisualWindow aWindow, boolean isModal) {
084: if (!isModal)
085: theStack.clear();
086: else
087: addModalWindowExecuteMethod(aWindow);
088:
089: setCurrentWindow(aWindow);
090: }
091:
092: protected void addModalWindowExecuteMethod(VisualWindow aWindow) {
093: Object theMethod = aWindow
094: .getClientProperty(AFTER_HIDE_METHOD_NAME);
095: if (theMethod != null)
096: theOpenersTable.put(aWindow.getName(), theMethod);
097: }
098:
099: public void hide(VisualWindow aWindow) {
100: VisualWindow foundWindow = findWindowWithId(aWindow.getName());
101: if (existOpennedWindows() && foundWindow != null) {
102: int indexOfWindow = theStack.indexOf(foundWindow);
103:
104: if (indexOfWindow > 0) {
105: VisualWindow parentWindow = (VisualWindow) theStack
106: .get(indexOfWindow - 1);
107: executeMethodForModalClosing(aWindow, parentWindow);
108: setCurrentWindow(parentWindow);
109: } else
110: setCurrentWindow((VisualWindow) theStack.peek());
111:
112: theStack.remove(foundWindow);
113: }
114:
115: if (!existOpennedWindows()) {
116: theStack.push(aWindow);
117: setCurrentWindow(aWindow);
118: }
119: }
120:
121: protected void executeMethodForModalClosing(VisualWindow aWindow,
122: VisualWindow aParentWindow) {
123: String theMethodName = (String) theOpenersTable.get(aWindow
124: .getName());
125: if (theMethodName != null)
126: callReturnMethod(aParentWindow, aWindow, theMethodName);
127: }
128:
129: public VisualWindow getCurrentWindow() {
130: if (!existOpennedWindows())
131: return null;
132: else
133: return currentWindow;
134: }
135:
136: public void setCurrentWindow(VisualWindow aWindow) {
137: setWindowManager(aWindow, this );
138:
139: currentWindow = findWindowWithId(aWindow.getName());
140:
141: if (currentWindow == null) {
142: currentWindow = aWindow;
143: theStack.push(aWindow);
144: }
145: }
146:
147: public VisualWindow findWindowWithId(Object aName) {
148: for (Iterator i = theStack.iterator(); i.hasNext();) {
149: VisualWindow theWindow = (VisualWindow) i.next();
150: if (theWindow.getName().equals(aName))
151: return theWindow;
152: }
153:
154: return null;
155: }
156:
157: public void replaceCurrentWindow(VisualWindow aReplaceWindow) {
158: theStack.set(theStack.indexOf(currentWindow), aReplaceWindow);
159: setCurrentWindow(aReplaceWindow);
160: }
161:
162: public void showChildWindow(Object anOpenerWindow,
163: Object aWindowToShow) {
164: showAndExecute(anOpenerWindow, aWindowToShow, null);
165: }
166:
167: protected void callReturnMethod(VisualWindow aParentWindow,
168: VisualWindow aWindow, String theMethodName) {
169: Object theWindow = getRealWindow(aWindow);
170: Object theOpenerWindow = getRealWindow(aParentWindow);
171:
172: callMethod(theMethodName, theWindow, theOpenerWindow);
173: }
174:
175: protected void callMethod(String theMethodName, Object theWindow,
176: Object theOpenerWindow) {
177: try {
178: Method theMethod = theOpenerWindow.getClass()
179: .getMethod(theMethodName,
180: new Class[] { theWindow.getClass() });
181: if (theMethod != null)
182: theMethod.invoke(theOpenerWindow,
183: new Object[] { theWindow });
184: } catch (Exception e) {
185: throw new WebOnSwingException(
186: "Cannot invoke the method after window hide", e);
187: }
188: }
189:
190: public void showAndExecute(Object anOpenerWindow,
191: Object aWindowToShow, String aMethodName) {
192: WindowManager theWindowManager = getWindowManager(anOpenerWindow);
193: setWindowManager(aWindowToShow, theWindowManager);
194:
195: if (theWindowManager != null) {
196: VisualWindow theWindowToShowWrapper = getNewVisualWindowFor(aWindowToShow);
197: if (aMethodName != null)
198: theWindowToShowWrapper.putClientProperty(
199: AFTER_HIDE_METHOD_NAME, aMethodName);
200:
201: theWindowToShowWrapper.putClientProperty(OPENER_WINDOW,
202: anOpenerWindow);
203: theWindowManager.show(theWindowToShowWrapper,
204: isWindowModal(aWindowToShow));
205: } else {
206: showNormal(aWindowToShow);
207:
208: if (aMethodName != null)
209: callMethod(aMethodName, aWindowToShow, anOpenerWindow);
210: }
211: }
212:
213: public void hide(Object aWindow) {
214: WindowManager theWindowManager = getWindowManager(aWindow);
215:
216: if (theWindowManager != null) {
217: VisualWindow theFoundWindow = theWindowManager
218: .findWindow(aWindow);
219: if (theFoundWindow != null)
220: theWindowManager.hide(theFoundWindow);
221: } else
222: hideNormal(aWindow);
223: }
224:
225: public VisualWindow findWindow(Object aComponent) {
226: for (Iterator i = theStack.iterator(); i.hasNext();) {
227: VisualWindow theWindowWrapper = (VisualWindow) i.next();
228: if (isWindowEquals(aComponent, theWindowWrapper))
229: return theWindowWrapper;
230: }
231:
232: return null;
233: }
234:
235: public VisualWindow getWindow(String aWindowId) {
236: VisualWindow foundWindow = null;
237:
238: if (existOpennedWindows())
239: if (aWindowId != null)
240: foundWindow = findWindowWithId(aWindowId);
241:
242: return foundWindow;
243: }
244:
245: private boolean existOpennedWindows() {
246: return !theStack.isEmpty();
247: }
248: }
|