001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Darrell Meyer <darrell@mygwt.net> - derived implementation
011: *******************************************************************************/package net.mygwt.ui.client.widget;
012:
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.MyGWT;
015: import net.mygwt.ui.client.Style;
016: import net.mygwt.ui.client.event.BaseEvent;
017: import net.mygwt.ui.client.event.Listener;
018: import net.mygwt.ui.client.event.SelectionListener;
019:
020: import com.google.gwt.user.client.DOM;
021: import com.google.gwt.user.client.ui.HorizontalPanel;
022: import com.google.gwt.user.client.ui.WidgetHelper;
023:
024: /**
025: * A <code>Shell</code> with a button bar.
026: *
027: * <dl>
028: * <dt><b>Styles:</b></dt>
029: * <dd>CLOSE, RESIZE, MODAL</dd>
030: * <dd>OK, OK_CANCEL, YES_NO, YES_NO_CANCEL</dd>
031: * </dl>
032: */
033: public class Dialog extends Shell {
034:
035: /**
036: * Button id for a "Cancel" button (value 1).
037: */
038: public static final int CANCEL_ID = 1;
039:
040: /**
041: * Button id for a "No" button (value 3).
042: */
043: public static final int NO_ID = 3;
044:
045: /**
046: * Button id for an "Ok" button (value 0).
047: */
048: public static final int OK_ID = 0;
049:
050: /**
051: * Button id for a "Yes" button (value 2).
052: */
053: public static final int YES_ID = 2;
054:
055: private boolean closeOnButtonClick = false;
056: private HorizontalPanel footerPanel;
057: private Item status;
058: private ButtonBar buttonBar;
059: private String statusText, statusIconStyle;
060:
061: /**
062: * Creates a dialog that can be closed and resized.
063: */
064: public Dialog() {
065: this (Style.CLOSE | Style.RESIZE);
066: }
067:
068: /**
069: * Creates a new dialog with the given style.
070: *
071: * @param style the style information
072: */
073: public Dialog(int style) {
074: super (style);
075:
076: buttonBar = new ButtonBar(Style.RIGHT);
077:
078: if ((style & Style.OK) != 0) {
079: createButton(OK_ID, MyGWT.MESSAGES.ok());
080: }
081: if ((style & Style.OK_CANCEL) != 0) {
082: createButton(OK_ID, MyGWT.MESSAGES.ok());
083: createButton(CANCEL_ID, MyGWT.MESSAGES.cancel());
084: }
085: if ((style & Style.YES_NO) != 0) {
086: createButton(YES_ID, MyGWT.MESSAGES.yes());
087: createButton(NO_ID, MyGWT.MESSAGES.no());
088: }
089: if ((style & Style.YES_NO_CANCEL) != 0) {
090: createButton(YES_ID, MyGWT.MESSAGES.yes());
091: createButton(NO_ID, MyGWT.MESSAGES.no());
092: createButton(CANCEL_ID, MyGWT.MESSAGES.cancel());
093: }
094: }
095:
096: /**
097: * Adds a button to the dialog.
098: *
099: * @param button the button to be added
100: */
101: public void addButton(Button button) {
102: buttonBar.add(button);
103: }
104:
105: /**
106: * Adds a button to the dialog.
107: *
108: * @param text the button text
109: * @param listener the click listener
110: *
111: * @return the new button
112: */
113: public Button addButton(String text, SelectionListener listener) {
114: Button btn = new Button(text);
115: btn.addSelectionListener(listener);
116: addButton(btn);
117: return btn;
118: }
119:
120: /**
121: * Clears the dialog's status area.
122: */
123: public void clearStatus() {
124: status.setText("");
125: status.setIconStyle("none");
126: }
127:
128: /**
129: * Returns the dialog's button bar.
130: *
131: * @return the button bar
132: */
133: public ButtonBar getButtonBar() {
134: return buttonBar;
135: }
136:
137: /**
138: * Returns the button with the specified button id.
139: *
140: * @param buttonId the button id
141: * @return the button or <code>null</code> if no match
142: */
143: public Button getButtonById(int buttonId) {
144: return buttonBar.getButtonById(buttonId);
145: }
146:
147: /**
148: * Returns the last pressed button.
149: *
150: * @return the button or <code>null</code> if no button pressed
151: */
152: public Button getButtonPressed() {
153: return buttonBar.getButtonPressed();
154: }
155:
156: /**
157: * Returns <code>true</code> if the dialog will be closed on any button
158: * click.
159: *
160: * @return the close on click state
161: */
162: public boolean getCloseOnButtonClick() {
163: return closeOnButtonClick;
164: }
165:
166: /**
167: * Removes the button.
168: *
169: * @param button the button to be removed
170: */
171: public void removeButton(Button button) {
172: buttonBar.remove(button);
173: }
174:
175: /**
176: * Sets the close on button click state. When <code>true</code>, the dialog
177: * will be closed after a button is clicked. Default value is
178: * <code>false</code>.
179: *
180: * @param closeOnButtonClick the close on button state
181: */
182: public void setCloseOnButtonClick(boolean closeOnButtonClick) {
183: this .closeOnButtonClick = closeOnButtonClick;
184: }
185:
186: /**
187: * Sets the dialog's status text and an optional icon.
188: *
189: * @param text the status text
190: * @param iconStyle the icon style
191: */
192: public void setStatus(String text, String iconStyle) {
193: statusText = text;
194: statusIconStyle = iconStyle;
195: if (rendered) {
196: status.setText(text);
197: if (iconStyle != null) {
198: status.setIconStyle(iconStyle);
199: }
200: }
201: }
202:
203: protected void afterRender() {
204: if (statusText != null) {
205: setStatus(statusText, statusIconStyle);
206: }
207: }
208:
209: protected void doAttachChildren() {
210: super .doAttachChildren();
211: WidgetHelper.doAttach(footerPanel);
212: }
213:
214: protected void doDetachChildren() {
215: super .doDetachChildren();
216: WidgetHelper.doDetach(footerPanel);
217: }
218:
219: /**
220: * Called after a button in the button bar is selected. Default implementation
221: * closes the dialog if closeOnButtonClick = true.
222: *
223: * @param be the base event
224: */
225: protected void onButtonPressed(BaseEvent be) {
226: if (closeOnButtonClick) {
227: close();
228: }
229: }
230:
231: protected void onRender() {
232: super .onRender();
233: if (!buttonBar.isRendered()) {
234: buttonBar.render();
235: }
236: buttonBar.addListener(Events.Click, new Listener() {
237: public void handleEvent(BaseEvent be) {
238: onButtonPressed(be);
239: }
240: });
241:
242: footerPanel = new HorizontalPanel();
243: footerPanel.setWidth("100%");
244:
245: status = new Item("my-dialog-status");
246: footerPanel.add(status);
247: footerPanel.setCellWidth(status, "100%");
248:
249: footerPanel.add(buttonBar);
250: DOM.appendChild(footerElem, footerPanel.getElement());
251: }
252:
253: protected void createButton(int id, String text) {
254: Button btn = new Button(text);
255: btn.setButtonId(id);
256: addButton(btn);
257: }
258:
259: }
|