001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Event;
020:
021: /**
022: * A form of popup that has a caption area at the top and can be dragged by the
023: * user. Unlike a PopupPanel, calls to {@link #setWidth(String)} and
024: * {@link #setHeight(String)} will set the width and height of the dialog box
025: * itself, even if a widget has not been added as yet.
026: * <p>
027: * <img class='gallery' src='DialogBox.png'/>
028: * </p>
029: * <h3>CSS Style Rules</h3>
030: * <ul class='css'>
031: * <li>.gwt-DialogBox { the outside of the dialog }</li>
032: * <li>.gwt-DialogBox .Caption { the caption }</li>
033: * </ul>
034: * <p>
035: * <h3>Example</h3>
036: * {@example com.google.gwt.examples.DialogBoxExample}
037: * </p>
038: */
039: public class DialogBox extends PopupPanel implements HasHTML,
040: MouseListener {
041:
042: private HTML caption = new HTML();
043: private Widget child;
044: private boolean dragging;
045: private int dragStartX, dragStartY;
046: private FlexTable panel = new FlexTable();
047:
048: /**
049: * Creates an empty dialog box. It should not be shown until its child widget
050: * has been added using {@link #add(Widget)}.
051: */
052: public DialogBox() {
053: this (false);
054: }
055:
056: /**
057: * Creates an empty dialog box specifying its "auto-hide" property. It should
058: * not be shown until its child widget has been added using
059: * {@link #add(Widget)}.
060: *
061: * @param autoHide <code>true</code> if the dialog should be automatically
062: * hidden when the user clicks outside of it
063: */
064: public DialogBox(boolean autoHide) {
065: this (autoHide, true);
066: }
067:
068: /**
069: * Creates an empty dialog box specifying its "auto-hide" property. It should
070: * not be shown until its child widget has been added using
071: * {@link #add(Widget)}.
072: *
073: * @param autoHide <code>true</code> if the dialog should be automatically
074: * hidden when the user clicks outside of it
075: * @param modal <code>true</code> if keyboard and mouse events for widgets
076: * not contained by the dialog should be ignored
077: */
078: public DialogBox(boolean autoHide, boolean modal) {
079: super (autoHide, modal);
080: panel.setWidget(0, 0, caption);
081: panel.setHeight("100%");
082: panel.setBorderWidth(0);
083: panel.setCellPadding(0);
084: panel.setCellSpacing(0);
085: panel.getCellFormatter().setHeight(1, 0, "100%");
086: panel.getCellFormatter().setWidth(1, 0, "100%");
087: panel.getCellFormatter().setAlignment(1, 0,
088: HasHorizontalAlignment.ALIGN_CENTER,
089: HasVerticalAlignment.ALIGN_MIDDLE);
090: super .setWidget(panel);
091:
092: setStyleName("gwt-DialogBox");
093: caption.setStyleName("Caption");
094: caption.addMouseListener(this );
095: }
096:
097: public String getHTML() {
098: return caption.getHTML();
099: }
100:
101: public String getText() {
102: return caption.getText();
103: }
104:
105: @Override
106: public Widget getWidget() {
107: return child;
108: }
109:
110: @Override
111: public boolean onEventPreview(Event event) {
112: // We need to preventDefault() on mouseDown events (outside of the
113: // DialogBox content) to keep text from being selected when it
114: // is dragged.
115: if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) {
116: if (DOM.isOrHasChild(caption.getElement(), DOM
117: .eventGetTarget(event))) {
118: DOM.eventPreventDefault(event);
119: }
120: }
121:
122: return super .onEventPreview(event);
123: }
124:
125: public void onMouseDown(Widget sender, int x, int y) {
126: dragging = true;
127: DOM.setCapture(caption.getElement());
128: dragStartX = x;
129: dragStartY = y;
130: }
131:
132: public void onMouseEnter(Widget sender) {
133: }
134:
135: public void onMouseLeave(Widget sender) {
136: }
137:
138: public void onMouseMove(Widget sender, int x, int y) {
139: if (dragging) {
140: int absX = x + getAbsoluteLeft();
141: int absY = y + getAbsoluteTop();
142: setPopupPosition(absX - dragStartX, absY - dragStartY);
143: }
144: }
145:
146: public void onMouseUp(Widget sender, int x, int y) {
147: dragging = false;
148: DOM.releaseCapture(caption.getElement());
149: }
150:
151: @Override
152: public boolean remove(Widget w) {
153: if (child != w) {
154: return false;
155: }
156:
157: panel.remove(w);
158: return true;
159: }
160:
161: public void setHTML(String html) {
162: caption.setHTML(html);
163: }
164:
165: public void setText(String text) {
166: caption.setText(text);
167: }
168:
169: @Override
170: public void setWidget(Widget w) {
171: // If there is already a widget, remove it.
172: if (child != null) {
173: panel.remove(child);
174: }
175:
176: // Add the widget to the center of the cell.
177: if (w != null) {
178: panel.setWidget(1, 0, w);
179: }
180:
181: child = w;
182: }
183: }
|