1 11 package org.eclipse.ui.internal; 12 13 import java.util.Iterator ; 14 import java.util.LinkedList ; 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 33 public class ShellPool { 34 35 private int flags; 36 37 40 private Shell parentShell; 41 42 private LinkedList availableShells = new LinkedList (); 43 44 private final static String CLOSE_LISTENER = "close listener"; 46 private boolean isDisposed = false; 47 48 private DisposeListener disposeListener = new DisposeListener() { 49 public void widgetDisposed(DisposeEvent e) { 50 WorkbenchPlugin.log(new RuntimeException ("Widget disposed too early!")); } 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 91 public ShellPool(Shell parentShell, int childFlags) { 92 this.parentShell = parentShell; 93 this.flags = childFlags; 94 } 95 96 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 121 public void dispose() { 122 for (Iterator 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 |