KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ShellPool


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.internal;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedList JavaDoc;
15
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.events.ShellAdapter;
19 import org.eclipse.swt.events.ShellEvent;
20 import org.eclipse.swt.events.ShellListener;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23
24 /**
25  * Manages a pool of shells. This can be used instead of creating and destroying
26  * shells. By reusing shells, they will never be disposed until the pool goes away.
27  * This is useful in situations where client code may have cached pointers to the
28  * shells to use as a parent for dialogs. It also works around bug 86226 (SWT menus
29  * cannot be reparented).
30  *
31  * @since 3.1
32  */

33 public class ShellPool {
34     
35     private int flags;
36     
37     /**
38      * Parent shell (or null if none)
39      */

40     private Shell parentShell;
41     
42     private LinkedList JavaDoc availableShells = new LinkedList JavaDoc();
43     
44     private final static String JavaDoc CLOSE_LISTENER = "close listener"; //$NON-NLS-1$
45

46     private boolean isDisposed = false;
47     
48     private DisposeListener disposeListener = new DisposeListener() {
49         public void widgetDisposed(DisposeEvent e) {
50             WorkbenchPlugin.log(new RuntimeException JavaDoc("Widget disposed too early!")); //$NON-NLS-1$
51
}
52     };
53     
54     private ShellListener closeListener = new ShellAdapter() {
55         
56         public void shellClosed(ShellEvent e) {
57                 if (isDisposed) {
58                     return;
59                 }
60                 
61                 if (e.doit) {
62                     Shell s = (Shell)e.widget;
63                     ShellListener l = (ShellListener)s.getData(CLOSE_LISTENER);
64                     
65                     if (l != null) {
66                         s.setData(CLOSE_LISTENER, null);
67                         l.shellClosed(e);
68                         
69                         Control[] children = s.getChildren();
70                         for (int i = 0; i < children.length; i++) {
71                             Control control = children[i];
72                           
73                             control.dispose();
74                         }
75                         availableShells.add(s);
76                         s.setVisible(false);
77                     }
78                 }
79                 e.doit = false;
80          }
81     };
82     
83     /**
84      * Creates a shell pool that allocates shells that are children of the
85      * given parent and are created with the given flags.
86      *
87      * @param parentShell parent shell (may be null, indicating that this pool creates
88      * top-level shells)
89      * @param childFlags flags for all child shells
90      */

91     public ShellPool(Shell parentShell, int childFlags) {
92         this.parentShell = parentShell;
93         this.flags = childFlags;
94     }
95     
96     /**
97      * Returns a new shell. The shell must not be disposed directly, but it may be closed.
98      * Once the shell is closed, it will be returned to the shell pool. Note: callers must
99      * remove all listeners from the shell before closing it.
100      */

101     public Shell allocateShell(ShellListener closeListener) {
102         Shell result;
103         if (!availableShells.isEmpty()) {
104             result = (Shell)availableShells.removeFirst();
105         } else {
106             result = new Shell(parentShell, flags);
107             result.addShellListener(this.closeListener);
108             result.addDisposeListener(disposeListener);
109         }
110         
111         result.setData(CLOSE_LISTENER, closeListener);
112         return result;
113     }
114     
115     /**
116      * Disposes this pool. Any unused shells in the pool are disposed immediately,
117      * and any shells in use will be disposed once they are closed.
118      *
119      * @since 3.1
120      */

121     public void dispose() {
122         for (Iterator JavaDoc iter = availableShells.iterator(); iter.hasNext();) {
123             Shell next = (Shell) iter.next();
124             next.removeDisposeListener(disposeListener);
125             
126             next.dispose();
127         }
128         
129         availableShells.clear();
130         isDisposed = true;
131     }
132 }
133
Popular Tags