1 16 package com.google.gwt.dev.shell; 17 18 import com.google.gwt.dev.GWTShell; 19 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.DisposeEvent; 22 import org.eclipse.swt.events.DisposeListener; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.graphics.Rectangle; 26 import org.eclipse.swt.layout.FillLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Dialog; 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.swt.widgets.MessageBox; 33 import org.eclipse.swt.widgets.Shell; 34 35 38 public abstract class DialogBase extends Dialog implements DisposeListener { 39 40 private class Buttons extends GridPanel { 41 public Buttons(Composite parent) { 42 super(parent, SWT.NONE, hasCancel ? 2 : 1, true); 43 44 if (hasOk) { 45 okButton = new Button(this, SWT.PUSH); 46 setGridData(okButton, 1, 1, FILL, FILL, false, false); 47 okButton.setText(" OK "); 48 okButton.addSelectionListener(new SelectionAdapter() { 49 public void widgetSelected(SelectionEvent e) { 50 clickOkButton(); 51 } 52 }); 53 } 54 55 if (hasCancel) { 56 cancelButton = new Button(this, SWT.PUSH); 57 setGridData(cancelButton, 1, 1, FILL, FILL, false, false); 58 cancelButton.setText("Cancel"); 59 cancelButton.addSelectionListener(new SelectionAdapter() { 60 public void widgetSelected(SelectionEvent e) { 61 clickCancelButton(); 62 } 63 }); 64 } 65 66 shell.setDefaultButton(okButton); 67 } 68 } 69 70 private class Contents extends GridPanel { 71 72 public Contents(Composite parent) { 73 super(parent, SWT.NONE, 1, false, 0, 0); 74 75 Control contents = createContents(this); 76 setGridData(contents, 1, 1, FILL, FILL, true, true); 77 78 if (hasOk || hasCancel) { 79 Buttons buttons = new Buttons(this); 80 setGridData(buttons, 1, 1, RIGHT, BOTTOM, false, false); 81 } 82 } 83 } 84 85 88 public static boolean confirmAction(Shell shell, String msg, String msgTitle) { 89 MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES 90 | SWT.NO); 91 msgBox.setText(msgTitle); 92 msgBox.setMessage(msg); 93 return msgBox.open() == SWT.YES; 94 } 95 96 private Button cancelButton; 97 98 private boolean cancelled = true; 99 100 private boolean hasCancel; 101 102 private boolean hasOk; 103 104 private int minHeight; 105 106 private int minWidth; 107 108 private Button okButton; 109 110 private Shell shell; 111 112 public DialogBase(Shell parent, int minWidth, int minHeight) { 113 this(parent, minWidth, minHeight, true, true); 114 } 115 116 public DialogBase(Shell parent, int minWidth, int minHeight, 117 boolean hasOkButton, boolean hasCancelButton) { 118 super(parent, SWT.NONE); 119 this.minWidth = minWidth; 120 this.minHeight = minHeight; 121 hasOk = hasOkButton; 122 hasCancel = hasCancelButton; 123 } 124 125 public Shell getShell() { 126 return shell; 127 } 128 129 public boolean open() { 130 return open(true); 131 } 132 133 public boolean open(boolean autoSize) { 134 Shell parent = getParent(); 135 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL 136 | SWT.RESIZE); 137 shell.setImages(GWTShell.getIcons()); 138 shell.setText(getText()); 139 shell.setLayout(new FillLayout()); 140 141 new Contents(shell); 142 143 onOpen(); 144 145 int myWidth; 146 int myHeight; 147 if (autoSize) { 148 shell.pack(); 152 153 Rectangle shellBounds = shell.getBounds(); 154 155 myWidth = Math.max(shellBounds.width, minWidth); 156 myHeight = Math.max(shellBounds.height, minHeight); 157 } else { 158 myWidth = minWidth; 159 myHeight = minHeight; 160 } 161 162 Rectangle parentBounds = parent.getBounds(); 165 int myLeft = parentBounds.x + (parentBounds.width / 2 - myWidth / 2); 166 int myTop = parentBounds.y + (parentBounds.height / 4); 167 168 shell.setBounds(myLeft, myTop, myWidth, myHeight); 169 170 shell.open(); 171 172 Display display = parent.getDisplay(); 173 while (!shell.isDisposed()) { 174 if (!display.readAndDispatch()) { 175 display.sleep(); 176 } 177 } 178 179 return !cancelled; 180 } 181 182 public void setText(String string) { 183 super.setText(string); 184 shell.setText(string); 185 } 186 187 public void widgetDisposed(DisposeEvent e) { 188 } 189 190 protected void clickCancelButton() { 191 cancelled = true; 192 onCancel(); 193 shell.dispose(); 194 } 195 196 protected void clickOkButton() { 197 cancelled = false; 198 onOk(); 199 shell.dispose(); 200 } 201 202 protected abstract Control createContents(Composite parent); 203 204 protected void onCancel() { 205 } 206 207 protected void onOk() { 208 } 209 210 protected void onOpen() { 211 } 212 213 protected void setOkEnabled(boolean enabled) { 214 okButton.setEnabled(enabled); 215 } 216 } 217 | Popular Tags |