001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.MyDOM;
015:
016: import com.google.gwt.user.client.DOM;
017: import com.google.gwt.user.client.ui.Widget;
018:
019: /**
020: * Manages <code>Shell</code> instances. The manager ensures the active shell
021: * is always displayed over other shells and fires shell events.
022: */
023: public class ShellManager {
024:
025: private static ShellManager instance;
026:
027: /**
028: * Returns the singleton instance.
029: *
030: * @return the shell manager
031: */
032: public static ShellManager get() {
033: if (instance == null)
034: instance = new ShellManager();
035: return instance;
036: }
037:
038: private List windows;
039: private Shell active;
040:
041: private ShellManager() {
042: instance = this ;
043: windows = new ArrayList();
044: }
045:
046: /**
047: * Closes all open shells.
048: */
049: public void closeAll() {
050: int size = windows.size();
051: for (int i = size - 1; i >= 0; i--) {
052: Shell shell = (Shell) windows.get(i);
053: shell.close();
054: }
055: }
056:
057: /**
058: * Returns the active shell, or <code>null</code> if no active shell.
059: *
060: * @return the active shell or <code>null</code>
061: */
062: public Shell getActive() {
063: return active;
064: }
065:
066: /**
067: * Returns <code>true</code> if active, <code>false</code> otherwise.
068: *
069: * @param shell the shell
070: * @return the active state
071: */
072: public boolean isActive(Shell shell) {
073: return active != null && active == shell;
074: }
075:
076: /**
077: * Registers a shell with the manager.
078: *
079: * @param shell the shell to be registered
080: */
081: public void register(Shell shell) {
082: windows.add(shell);
083: }
084:
085: /**
086: * Sets the active shell.
087: *
088: * @param shell the shell to activate
089: */
090: public void setActive(Shell shell) {
091: if (active != null && active == shell) {
092: return;
093: }
094: if (active != null) {
095: active.fireEvent(Events.Deactivate);
096: }
097: active = shell;
098: if (active.shadow != null) {
099: setZIndex(active.shadow, MyDOM.getZIndex());
100: }
101: setZIndex(active, MyDOM.getZIndex());
102: active.fireEvent(Events.Activate);
103: }
104:
105: /**
106: * Unregisters a shell.
107: *
108: * @param shell the shell to unregister
109: */
110: public void unregister(Shell shell) {
111: if (shell == active)
112: active = null;
113: windows.remove(shell);
114: }
115:
116: private void setZIndex(Widget widget, int zIndex) {
117: DOM.setIntStyleAttribute(widget.getElement(), "zIndex", zIndex);
118: }
119:
120: }
|