001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.comp.glasspanes;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028: import jacareto.system.EnvironmentMember;
029:
030: import java.awt.Component;
031: import java.awt.Window;
032:
033: import java.util.Hashtable;
034: import java.util.Iterator;
035: import java.util.Vector;
036:
037: import javax.swing.JDialog;
038: import javax.swing.JFrame;
039:
040: /**
041: * <p>
042: * Manages the classpane of the components.
043: * </p>
044: *
045: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
046: * @version 1.01
047: */
048: public class GlassPaneManager extends EnvironmentMember {
049: /** The env. */
050: private Environment env;
051:
052: /** The instance which knows the components. */
053: private Components components;
054:
055: /** The stored glass pane compounds. */
056: private Hashtable glassPaneCompounds;
057:
058: /** A vector which stores all windows who have got a glass pane compound. */
059: private Vector glassPaneWindows;
060:
061: /**
062: * Creates a new GlassPaneCompound.
063: *
064: * @param env the environment
065: * @param components the instance which knows the components
066: */
067: public GlassPaneManager(Environment env, Components components) {
068: super (env);
069: this .components = components;
070: glassPaneCompounds = new Hashtable();
071: glassPaneWindows = new Vector(5, 5);
072: }
073:
074: /**
075: * Returns a glass pane compound for windows based on swing. If the window does not have a
076: * glasspane compound, <code>null</code> is returned.
077: *
078: * @param window the window
079: *
080: * @return the glasspane compound, or <code>null</code> if the window does not have a glasspane
081: * compound
082: */
083: public GlassPaneCompound getGlassPaneCompound(Window window) {
084: GlassPaneCompound result = null;
085:
086: if (glassPaneCompounds.containsKey(window)) {
087: result = (GlassPaneCompound) glassPaneCompounds.get(window);
088: }
089:
090: return result;
091: }
092:
093: /**
094: * Returns all glasspane compounds.
095: *
096: * @return all glasspane compounds in an array.
097: */
098: public GlassPaneCompound[] getGlassPaneCompounds() {
099: GlassPaneCompound[] result = new GlassPaneCompound[glassPaneCompounds
100: .size()];
101: Iterator it = glassPaneCompounds.values().iterator();
102: int index = 0;
103:
104: while (it.hasNext()) {
105: result[index++] = (GlassPaneCompound) it.next();
106: }
107:
108: return result;
109: }
110:
111: /**
112: * Creates a glasspane compound for the given window. The compound is integrated into the
113: * window as glasspane.
114: *
115: * @param window window (must be a SWING window).
116: *
117: * @return the glasspane compound
118: */
119: public GlassPaneCompound createGlassPaneCompound(Window window) {
120: // replace the window's glass pane with a new glass pane compound
121: // if it is not already one
122: GlassPaneCompound compound = null;
123:
124: if ((window != null) && window instanceof JFrame) {
125: // The window is a subclass of javax.swing.JFrame
126: JFrame frame = (JFrame) window;
127: Component targetApplicationPane = frame.getGlassPane();
128:
129: if ((targetApplicationPane != null)
130: && targetApplicationPane instanceof GlassPaneCompound) {
131: compound = (GlassPaneCompound) frame.getGlassPane();
132: } else {
133: compound = new GlassPaneCompound(getEnvironment(),
134: components, window);
135: frame.setGlassPane(compound);
136: glassPaneWindows.add(frame);
137:
138: frame.getGlassPane().setVisible(true);
139: frame.validate();
140: frame.repaint();
141: }
142: } else if ((window != null) && window instanceof JDialog) {
143: // The window is a subclass of javax.swing.JDialog
144: JDialog dialog = (JDialog) window;
145: Component targetApplicationPane = dialog.getGlassPane();
146:
147: if ((targetApplicationPane != null)
148: && targetApplicationPane instanceof GlassPaneCompound) {
149: compound = (GlassPaneCompound) dialog.getGlassPane();
150: } else {
151: compound = new GlassPaneCompound(getEnvironment(),
152: components, window);
153: dialog.setGlassPane(compound);
154: glassPaneWindows.add(dialog);
155:
156: dialog.getGlassPane().setVisible(true);
157: dialog.validate();
158: dialog.repaint();
159: }
160: }
161:
162: if (compound != null) {
163: glassPaneCompounds.put(window, compound);
164: }
165:
166: return compound;
167: }
168:
169: /**
170: * Removes all glass pane compounds.
171: */
172: public void removeAllGlassPaneCompounds() {
173: Iterator it = glassPaneWindows.iterator();
174:
175: while (it.hasNext()) {
176: Window window = (Window) it.next();
177:
178: if (window instanceof JFrame) {
179: JFrame frame = (JFrame) window;
180: Component glasspane = frame.getGlassPane();
181:
182: if ((glasspane != null)
183: && glasspane instanceof GlassPaneCompound) {
184: GlassPaneCompound compound = (GlassPaneCompound) glasspane;
185:
186: if (compound
187: .hasGlassPane(GlassPaneCompound.TARGET_APPLICATION_PANE)) {
188: Component targetGlassPane = compound
189: .getGlassPane(GlassPaneCompound.TARGET_APPLICATION_PANE);
190: boolean isVisible = targetGlassPane.isVisible();
191: frame.setGlassPane(targetGlassPane);
192: frame.getGlassPane().setVisible(isVisible);
193: } else {
194: frame.getGlassPane().setVisible(false);
195: frame.setGlassPane(null);
196: }
197:
198: frame.validate();
199: frame.repaint();
200: }
201: } else if (window instanceof JDialog) {
202: JDialog dialog = (JDialog) window;
203: Component glasspane = dialog.getGlassPane();
204:
205: if ((glasspane != null)
206: && glasspane instanceof GlassPaneCompound) {
207: GlassPaneCompound compound = (GlassPaneCompound) glasspane;
208:
209: if (compound
210: .hasGlassPane(GlassPaneCompound.TARGET_APPLICATION_PANE)) {
211: Component targetGlassPane = compound
212: .getGlassPane(GlassPaneCompound.TARGET_APPLICATION_PANE);
213: boolean isVisible = targetGlassPane.isVisible();
214: dialog.setGlassPane(targetGlassPane);
215: dialog.getGlassPane().setVisible(isVisible);
216: } else {
217: dialog.getGlassPane().setVisible(false);
218: dialog.setGlassPane(null);
219: }
220:
221: dialog.validate();
222: dialog.repaint();
223: }
224: }
225: }
226:
227: glassPaneWindows.clear();
228: glassPaneCompounds.clear();
229: }
230:
231: /**
232: * Sets the visibility state of all glass panes with the specified type in all contained
233: * glasspane compounds
234: *
235: * @param type the glasspane type
236: * @param isVisible whether or not those panes should be visible
237: */
238: public synchronized void setVisible(int type, boolean isVisible) {
239: Vector compounds = new Vector(glassPaneCompounds.values());
240:
241: for (int i = 0; i < compounds.size(); i++) {
242: ((GlassPaneCompound) compounds.get(i)).setVisible(type,
243: isVisible);
244: }
245: }
246: }
|