KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > window > WindowManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.window;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 /**
20  * A manager for a group of windows. Window managers are an optional JFace
21  * feature used in applications which create many different windows (dialogs,
22  * wizards, etc.) in addition to a main window. A window manager can be used to
23  * remember all the windows that an application has created (independent of
24  * whether they are presently open or closed). There can be several window
25  * managers, and they can be arranged into a tree. This kind of organization
26  * makes it simple to close whole subgroupings of windows.
27  * <p>
28  * Creating a window manager is as simple as creating an instance of
29  * <code>WindowManager</code>. Associating a window with a window manager is
30  * done with <code>WindowManager.add(Window)</code>. A window is automatically
31  * removed from its window manager as a side effect of closing the window.
32  * </p>
33  *
34  * @see Window
35  */

36 public class WindowManager {
37
38     /**
39      * List of windows managed by this window manager
40      * (element type: <code>Window</code>).
41      */

42     private ArrayList JavaDoc windows = new ArrayList JavaDoc();
43
44     /**
45      * List of window managers who have this window manager
46      * as their parent (element type: <code>WindowManager</code>).
47      */

48     private List JavaDoc subManagers;
49
50     /**
51      * Creates an empty window manager without a parent window
52      * manager (that is, a root window manager).
53      */

54     public WindowManager() {
55     }
56
57     /**
58      * Creates an empty window manager with the given
59      * window manager as parent.
60      *
61      * @param parent the parent window manager
62      */

63     public WindowManager(WindowManager parent) {
64         Assert.isNotNull(parent);
65         parent.addWindowManager(this);
66     }
67
68     /**
69      * Adds the given window to the set of windows managed by
70      * this window manager. Does nothing is this window is
71      * already managed by this window manager.
72      *
73      * @param window the window
74      */

75     public void add(Window window) {
76         if (!windows.contains(window)) {
77             windows.add(window);
78             window.setWindowManager(this);
79         }
80     }
81
82     /**
83      * Adds the given window manager to the list of
84      * window managers that have this one as a parent.
85      * </p>
86      * @param wm the child window manager
87      */

88     private void addWindowManager(WindowManager wm) {
89         if (subManagers == null) {
90             subManagers = new ArrayList JavaDoc();
91         }
92         if (!subManagers.contains(wm)) {
93             subManagers.add(wm);
94         }
95     }
96
97     /**
98      * Attempts to close all windows managed by this window manager,
99      * as well as windows managed by any descendent window managers.
100      *
101      * @return <code>true</code> if all windows were sucessfully closed,
102      * and <code>false</code> if any window refused to close
103      */

104     public boolean close() {
105         List JavaDoc t = (List JavaDoc) windows.clone(); // make iteration robust
106
Iterator JavaDoc e = t.iterator();
107         while (e.hasNext()) {
108             Window window = (Window) e.next();
109             boolean closed = window.close();
110             if (!closed) {
111                 return false;
112             }
113         }
114         if (subManagers != null) {
115             e = subManagers.iterator();
116             while (e.hasNext()) {
117                 WindowManager wm = (WindowManager) e.next();
118                 boolean closed = wm.close();
119                 if (!closed) {
120                     return false;
121                 }
122             }
123         }
124         return true;
125     }
126
127     /**
128      * Returns this window manager's number of windows
129      *
130      * @return the number of windows
131      * @since 3.0
132      */

133     public int getWindowCount() {
134         return windows.size();
135     }
136
137     /**
138      * Returns this window manager's set of windows.
139      *
140      * @return a possibly empty list of window
141      */

142     public Window[] getWindows() {
143         Window bs[] = new Window[windows.size()];
144         windows.toArray(bs);
145         return bs;
146     }
147
148     /**
149      * Removes the given window from the set of windows managed by
150      * this window manager. Does nothing is this window is
151      * not managed by this window manager.
152      *
153      * @param window the window
154      */

155     public final void remove(Window window) {
156         if (windows.contains(window)) {
157             windows.remove(window);
158             window.setWindowManager(null);
159         }
160     }
161 }
162
Popular Tags