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.dev.shell;
017:
018: import com.google.gwt.dev.GWTShell;
019:
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.DisposeEvent;
022: import org.eclipse.swt.events.DisposeListener;
023: import org.eclipse.swt.events.SelectionAdapter;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.graphics.Rectangle;
026: import org.eclipse.swt.layout.FillLayout;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Dialog;
031: import org.eclipse.swt.widgets.Display;
032: import org.eclipse.swt.widgets.MessageBox;
033: import org.eclipse.swt.widgets.Shell;
034:
035: /**
036: * Shared boilerplate for dialogs.
037: */
038: public abstract class DialogBase extends Dialog implements
039: DisposeListener {
040:
041: private class Buttons extends GridPanel {
042: public Buttons(Composite parent) {
043: super (parent, SWT.NONE, hasCancel ? 2 : 1, true);
044:
045: if (hasOk) {
046: okButton = new Button(this , SWT.PUSH);
047: setGridData(okButton, 1, 1, FILL, FILL, false, false);
048: okButton.setText(" OK ");
049: okButton.addSelectionListener(new SelectionAdapter() {
050: @Override
051: public void widgetSelected(SelectionEvent e) {
052: clickOkButton();
053: }
054: });
055: }
056:
057: if (hasCancel) {
058: cancelButton = new Button(this , SWT.PUSH);
059: setGridData(cancelButton, 1, 1, FILL, FILL, false,
060: false);
061: cancelButton.setText("Cancel");
062: cancelButton
063: .addSelectionListener(new SelectionAdapter() {
064: @Override
065: public void widgetSelected(SelectionEvent e) {
066: clickCancelButton();
067: }
068: });
069: }
070:
071: shell.setDefaultButton(okButton);
072: }
073: }
074:
075: private class Contents extends GridPanel {
076:
077: public Contents(Composite parent) {
078: super (parent, SWT.NONE, 1, false, 0, 0);
079:
080: Control contents = createContents(this );
081: setGridData(contents, 1, 1, FILL, FILL, true, true);
082:
083: if (hasOk || hasCancel) {
084: Buttons buttons = new Buttons(this );
085: setGridData(buttons, 1, 1, RIGHT, BOTTOM, false, false);
086: }
087: }
088: }
089:
090: /**
091: * Pops up a confirm/cancel dialog.
092: */
093: public static boolean confirmAction(Shell shell, String msg,
094: String msgTitle) {
095: MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING
096: | SWT.YES | SWT.NO);
097: msgBox.setText(msgTitle);
098: msgBox.setMessage(msg);
099: return msgBox.open() == SWT.YES;
100: }
101:
102: private Button cancelButton;
103:
104: private boolean cancelled = true;
105:
106: private boolean hasCancel;
107:
108: private boolean hasOk;
109:
110: private int minHeight;
111:
112: private int minWidth;
113:
114: private Button okButton;
115:
116: private Shell shell;
117:
118: public DialogBase(Shell parent, int minWidth, int minHeight) {
119: this (parent, minWidth, minHeight, true, true);
120: }
121:
122: public DialogBase(Shell parent, int minWidth, int minHeight,
123: boolean hasOkButton, boolean hasCancelButton) {
124: super (parent, SWT.NONE);
125: this .minWidth = minWidth;
126: this .minHeight = minHeight;
127: hasOk = hasOkButton;
128: hasCancel = hasCancelButton;
129: }
130:
131: public Shell getShell() {
132: return shell;
133: }
134:
135: public boolean open() {
136: return open(true);
137: }
138:
139: public boolean open(boolean autoSize) {
140: Shell parent = getParent();
141: shell = new Shell(parent, SWT.DIALOG_TRIM
142: | SWT.APPLICATION_MODAL | SWT.RESIZE);
143: shell.setImages(GWTShell.getIcons());
144: shell.setText(getText());
145: shell.setLayout(new FillLayout());
146:
147: new Contents(shell);
148:
149: onOpen();
150:
151: int myWidth;
152: int myHeight;
153: if (autoSize) {
154: // Try to make the dialog big enough to hold the packed layout or
155: // the requested size, whichever is bigger.
156: //
157: shell.pack();
158:
159: Rectangle shellBounds = shell.getBounds();
160:
161: myWidth = Math.max(shellBounds.width, minWidth);
162: myHeight = Math.max(shellBounds.height, minHeight);
163: } else {
164: myWidth = minWidth;
165: myHeight = minHeight;
166: }
167:
168: // Try to center within parent shell.
169: //
170: Rectangle parentBounds = parent.getBounds();
171: int myLeft = parentBounds.x
172: + (parentBounds.width / 2 - myWidth / 2);
173: int myTop = parentBounds.y + (parentBounds.height / 4);
174:
175: shell.setBounds(myLeft, myTop, myWidth, myHeight);
176:
177: shell.open();
178:
179: Display display = parent.getDisplay();
180: while (!shell.isDisposed()) {
181: if (!display.readAndDispatch()) {
182: display.sleep();
183: }
184: }
185:
186: return !cancelled;
187: }
188:
189: @Override
190: public void setText(String string) {
191: super .setText(string);
192: shell.setText(string);
193: }
194:
195: public void widgetDisposed(DisposeEvent e) {
196: }
197:
198: protected void clickCancelButton() {
199: cancelled = true;
200: onCancel();
201: shell.dispose();
202: }
203:
204: protected void clickOkButton() {
205: cancelled = false;
206: onOk();
207: shell.dispose();
208: }
209:
210: protected abstract Control createContents(Composite parent);
211:
212: protected void onCancel() {
213: }
214:
215: protected void onOk() {
216: }
217:
218: protected void onOpen() {
219: }
220:
221: protected void setOkEnabled(boolean enabled) {
222: okButton.setEnabled(enabled);
223: }
224: }
|