001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.gwt.widget;
022:
023: import java.util.*;
024: import com.google.gwt.user.client.DOM;
025: import com.google.gwt.user.client.ui.*;
026:
027: public class MoveableWindow extends PopupPanel implements IContainer {
028:
029: private static Map FRONT = new HashMap();
030:
031: private DockPanel _frame = new DockPanel();
032:
033: private Container _content = new Container();
034:
035: private List _listeners = new ArrayList();
036:
037: private TitleBar _titleBar;
038:
039: private boolean _init;
040:
041: public MoveableWindow() {
042: setStyleName("MoveableWindow");
043: setupFrame();
044: _titleBar = new TitleBar("");
045: setTitleBar(_titleBar);
046: _init = true;
047: _content.setStyleName("content");
048: DOM.setStyleAttribute(_content.getElement(),
049: "background-color", "#cccccc");
050: }
051:
052: private void setupFrame() {
053: _frame.setHorizontalAlignment(_frame.ALIGN_CENTER);
054: _frame.setStyleName("frame");
055: _frame.add(_content, DockPanel.SOUTH);
056: _frame.setWidth("100%");
057: _frame.setHeight("100%");
058: MousePanel mp = new MousePanel();
059: mp.addMouseListener(new MouseListenerAdapter() {
060: public void onMouseDown(Widget sender, int x, int y) {
061: bringToFront();
062: super .onMouseDown(sender, x, y);
063: }
064: });
065: add(mp);
066: mp.setWidget(_frame);
067: }
068:
069: public void setTitleBar(TitleBar tb) {
070: if (_titleBar != null) {
071: _frame.remove(_titleBar);
072: tb.setTitle(_titleBar.getTitleText());
073: tb = _titleBar;
074: }
075: _frame.add(tb, DockPanel.NORTH);
076: tb.setWindow(this );
077: }
078:
079: public void setTitle(String title) {
080: _titleBar.setTitleText(title);
081: }
082:
083: public void setMenuBar(MenuBar mb) {
084: _frame.add(mb, DockPanel.CENTER);
085: }
086:
087: public void setContent(Widget content) {
088: _frame.add(content, DockPanel.SOUTH);
089: }
090:
091: public void setWidget(Widget w) {
092: if (_init) {
093: throw new RuntimeException("Please use setContent instead");
094: } else {
095: super .setWidget(w);
096: }
097: }
098:
099: public ComponentLayout getLayout() {
100: return _content.getLayout();
101: }
102:
103: public void fireMinimizeEvent() {
104: for (int i = 0; i < _listeners.size(); i++) {
105: WindowListener listener = (WindowListener) _listeners
106: .get(i);
107: listener.windowMinimized(this );
108: }
109: }
110:
111: public void fireMaximizeEvent() {
112: for (int i = 0; i < _listeners.size(); i++) {
113: WindowListener listener = (WindowListener) _listeners
114: .get(i);
115: listener.windowMaximized(this );
116: }
117: }
118:
119: public void fireCloseEvent() {
120: for (int i = 0; i < _listeners.size(); i++) {
121: WindowListener listener = (WindowListener) _listeners
122: .get(i);
123: listener.windowClosed(this );
124: }
125: }
126:
127: public void addWindowListener(WindowListener listener) {
128: _listeners.add(listener);
129: }
130:
131: public void close() {
132: removeFromParent();
133: fireCloseEvent();
134: }
135:
136: public void addChild(Widget widget, CellInfo info) {
137: _content.addChild(widget, info);
138: }
139:
140: public void removeChild(Widget child) {
141: _content.removeChild(child);
142: }
143:
144: public void bringToFront() {
145: Widget parent = this .getParent();
146: if (parent != null) {
147: Widget front = (Widget) FRONT.get(parent);
148: if (front != this ) {
149: if (front instanceof MoveableWindow) {
150: ((MoveableWindow) front).putToBack();
151: }
152: DOM.setStyleAttribute(this .getElement(), "zIndex", "1");
153: _titleBar.setSelected(true);
154: FRONT.put(parent, this );
155: }
156: }
157: }
158:
159: public void putToBack() {
160: DOM.setStyleAttribute(getElement(), "zIndex", "0");
161: _titleBar.setSelected(false);
162: }
163:
164: protected void onAttach() {
165: bringToFront();
166: super .onAttach();
167: }
168:
169: protected void onDetach() {
170: Iterator it = FRONT.entrySet().iterator();
171: while (it.hasNext()) {
172: Map.Entry entry = (Map.Entry) it.next();
173: if (entry.getValue() == this) {
174: it.remove();
175: }
176: }
177: super.onDetach();
178: }
179: }
|