KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > DialogBox


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Event;
20
21 /**
22  * A form of popup that has a caption area at the top and can be dragged by the
23  * user.
24  * <p>
25  * <img class='gallery' SRC='DialogBox.png'/>
26  * </p>
27  * <h3>CSS Style Rules</h3>
28  * <ul class='css'>
29  * <li>.gwt-DialogBox { the outside of the dialog }</li>
30  * <li>.gwt-DialogBox .Caption { the caption }</li>
31  * </ul>
32  * <p>
33  * <h3>Example</h3>
34  * {@example com.google.gwt.examples.DialogBoxExample}
35  * </p>
36  */

37 public class DialogBox extends PopupPanel implements HasHTML, MouseListener {
38
39   private HTML caption = new HTML();
40   private Widget child;
41   private boolean dragging;
42   private int dragStartX, dragStartY;
43   private FlexTable panel = new FlexTable();
44
45   /**
46    * Creates an empty dialog box. It should not be shown until its child widget
47    * has been added using {@link #add(Widget)}.
48    */

49   public DialogBox() {
50     this(false);
51   }
52
53   /**
54    * Creates an empty dialog box specifying its "auto-hide" property. It should
55    * not be shown until its child widget has been added using
56    * {@link #add(Widget)}.
57    *
58    * @param autoHide <code>true</code> if the dialog should be automatically
59    * hidden when the user clicks outside of it
60    */

61   public DialogBox(boolean autoHide) {
62     this(autoHide, true);
63   }
64
65   /**
66    * Creates an empty dialog box specifying its "auto-hide" property. It should
67    * not be shown until its child widget has been added using
68    * {@link #add(Widget)}.
69    *
70    * @param autoHide <code>true</code> if the dialog should be automatically
71    * hidden when the user clicks outside of it
72    * @param modal <code>true</code> if keyboard and mouse events for widgets
73    * not contained by the dialog should be ignored
74    */

75   public DialogBox(boolean autoHide, boolean modal) {
76     super(autoHide, modal);
77     panel.setWidget(0, 0, caption);
78     panel.setHeight("100%");
79     panel.setBorderWidth(0);
80     panel.setCellPadding(0);
81     panel.setCellSpacing(0);
82     panel.getCellFormatter().setHeight(1, 0, "100%");
83     panel.getCellFormatter().setWidth(1, 0, "100%");
84     panel.getCellFormatter().setAlignment(1, 0,
85         HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
86     super.setWidget(panel);
87
88     setStyleName("gwt-DialogBox");
89     caption.setStyleName("Caption");
90     caption.addMouseListener(this);
91   }
92
93   public String JavaDoc getHTML() {
94     return caption.getHTML();
95   }
96
97   public String JavaDoc getText() {
98     return caption.getText();
99   }
100
101   public boolean onEventPreview(Event event) {
102     // We need to preventDefault() on mouseDown events (outside of the
103
// DialogBox content) to keep text from being selected when it
104
// is dragged.
105
if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) {
106       if (DOM.isOrHasChild(caption.getElement(), DOM.eventGetTarget(event))) {
107         DOM.eventPreventDefault(event);
108       }
109     }
110
111     return super.onEventPreview(event);
112   }
113
114   public void onMouseDown(Widget sender, int x, int y) {
115     dragging = true;
116     DOM.setCapture(caption.getElement());
117     dragStartX = x;
118     dragStartY = y;
119   }
120
121   public void onMouseEnter(Widget sender) {
122   }
123
124   public void onMouseLeave(Widget sender) {
125   }
126
127   public void onMouseMove(Widget sender, int x, int y) {
128     if (dragging) {
129       int absX = x + getAbsoluteLeft();
130       int absY = y + getAbsoluteTop();
131       setPopupPosition(absX - dragStartX, absY - dragStartY);
132     }
133   }
134
135   public void onMouseUp(Widget sender, int x, int y) {
136     dragging = false;
137     DOM.releaseCapture(caption.getElement());
138   }
139
140   public boolean remove(Widget w) {
141     if (child != w) {
142       return false;
143     }
144
145     panel.remove(w);
146     return true;
147   }
148
149   public void setHTML(String JavaDoc html) {
150     caption.setHTML(html);
151   }
152
153   public void setText(String JavaDoc text) {
154     caption.setText(text);
155   }
156
157   public void setWidget(Widget w) {
158     // If there is already a widget, remove it.
159
if (child != null) {
160       panel.remove(child);
161     }
162
163     // Add the widget to the center of the cell.
164
if (w != null) {
165       panel.setWidget(1, 0, w);
166     }
167
168     child = w;
169   }
170
171   /**
172    * Override, so that interior panel reflows to match parent's new width.
173    *
174    * @Override
175    */

176   public void setWidth(String JavaDoc width) {
177     super.setWidth(width);
178
179     // note that you CANNOT call panel.setWidth("100%") until parent's width
180
// has been explicitly set, b/c until then parent's width is unconstrained
181
// and setting panel's width to 100% will flow parent to 100% of browser
182
// (i.e. can't do this in constructor)
183
panel.setWidth("100%");
184   }
185 }
186
Popular Tags