1 /******************************************************************************* 2 * Copyright (c) 2003, 2007 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * John Reimer <terminal.node@gmail.com> 12 *******************************************************************************/ 13 module org.eclipse.swt.browser.PromptDialog; 14 15 import java.lang.all; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Dialog; 23 import org.eclipse.swt.widgets.Display; 24 import org.eclipse.swt.widgets.Event; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Listener; 27 import org.eclipse.swt.widgets.Monitor; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Text; 30 import org.eclipse.swt.widgets.Widget; 31 32 class PromptDialog : Dialog { 33 34 this(Shell parent, int style) { 35 super(parent, style); 36 } 37 38 this(Shell parent) { 39 this(parent, 0); 40 } 41 42 void alertCheck(String title, String text, String check, ref int checkValue) { 43 Shell parent = getParent(); 44 /* const */ Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 45 if (title !is null) shell.setText(title); 46 GridLayout gridLayout = new GridLayout(); 47 shell.setLayout(gridLayout); 48 Label label = new Label(shell, SWT.WRAP); 49 label.setText(text); 50 GridData data = new GridData(); 51 org.eclipse.swt.widgets.Monitor.Monitor monitor = parent.getMonitor(); 52 int maxWidth = monitor.getBounds().width * 2 / 3; 53 int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 54 data.widthHint = Math.min(width, maxWidth); 55 data.horizontalAlignment = GridData.FILL; 56 data.grabExcessHorizontalSpace = true; 57 label.setLayoutData (data); 58 59 Button checkButton = check !is null ? new Button(shell, SWT.CHECK) : null; 60 if (checkButton !is null) { 61 checkButton.setText(check); 62 checkButton.setSelection(checkValue !is 0); 63 data = new GridData (); 64 data.horizontalAlignment = GridData.BEGINNING; 65 checkButton.setLayoutData (data); 66 } 67 Button okButton = new Button(shell, SWT.PUSH); 68 okButton.setText("OK"); // TODO: Need to do this through Resource Bundle 69 //okButton.setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$ 70 data = new GridData (); 71 data.horizontalAlignment = GridData.CENTER; 72 okButton.setLayoutData (data); 73 okButton.addListener(SWT.Selection, new class() Listener { 74 public void handleEvent(Event event) { 75 if (checkButton !is null) checkValue = checkButton.getSelection() ? 1 : 0; 76 shell.close(); 77 } 78 }); 79 80 shell.pack(); 81 shell.open(); 82 Display display = parent.getDisplay(); 83 while (!shell.isDisposed()) { 84 if (!display.readAndDispatch()) display.sleep(); 85 } 86 } 87 88 void confirmEx(String title, String text, String check, String button0, String button1, String button2, int defaultIndex, ref int checkValue, ref int result) { 89 Shell parent = getParent(); 90 /* const */ Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 91 shell.setText(title); 92 GridLayout gridLayout = new GridLayout(); 93 shell.setLayout(gridLayout); 94 Label label = new Label(shell, SWT.WRAP); 95 label.setText(text); 96 GridData data = new GridData(); 97 org.eclipse.swt.widgets.Monitor.Monitor monitor = parent.getMonitor(); 98 int maxWidth = monitor.getBounds().width * 2 / 3; 99 int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 100 data.widthHint = Math.min(width, maxWidth); 101 data.horizontalAlignment = GridData.FILL; 102 data.grabExcessHorizontalSpace = true; 103 label.setLayoutData (data); 104 105 Button[] buttons = new Button[4]; 106 Listener listener = new class() Listener { 107 public void handleEvent(Event event) { 108 if (buttons[0] !is null) checkValue = buttons[0].getSelection() ? 1 : 0; 109 Widget widget = event.widget; 110 for (int i = 1; i < buttons.length; i++) { 111 if (widget is buttons[i]) { 112 result = i - 1; 113 break; 114 } 115 } 116 shell.close(); 117 } 118 }; 119 if (check !is null) { 120 buttons[0] = new Button(shell, SWT.CHECK); 121 buttons[0].setText(check); 122 buttons[0].setSelection(checkValue !is 0); 123 data = new GridData (); 124 data.horizontalAlignment = GridData.BEGINNING; 125 buttons[0].setLayoutData (data); 126 } 127 Composite composite = new Composite(shell, SWT.NONE); 128 data = new GridData(); 129 data.horizontalAlignment = GridData.CENTER; 130 composite.setLayoutData (data); 131 GridLayout layout = new GridLayout(); 132 layout.makeColumnsEqualWidth = true; 133 composite.setLayout(layout); 134 int buttonCount = 0; 135 if (button0 !is null) { 136 buttons[1] = new Button(composite, SWT.PUSH); 137 buttons[1].setText(button0); 138 buttons[1].addListener(SWT.Selection, listener); 139 buttons[1].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 140 buttonCount++; 141 } 142 if (button1 !is null) { 143 buttons[2] = new Button(composite, SWT.PUSH); 144 buttons[2].setText(button1); 145 buttons[2].addListener(SWT.Selection, listener); 146 buttons[2].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 147 buttonCount++; 148 } 149 if (button2 !is null) { 150 buttons[3] = new Button(composite, SWT.PUSH); 151 buttons[3].setText(button2); 152 buttons[3].addListener(SWT.Selection, listener); 153 buttons[3].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 154 buttonCount++; 155 } 156 layout.numColumns = buttonCount; 157 Button defaultButton = buttons [defaultIndex + 1]; 158 if (defaultButton !is null) shell.setDefaultButton (defaultButton); 159 160 shell.pack(); 161 shell.open(); 162 Display display = parent.getDisplay(); 163 while (!shell.isDisposed()) { 164 if (!display.readAndDispatch()) display.sleep(); 165 } 166 } 167 168 void prompt(String title, String text, String check, /* final */ref String value, /* final */ ref int checkValue, /* final */ref int result) { 169 Shell parent = getParent(); 170 /* final */ Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 171 if (title !is null) shell.setText(title); 172 GridLayout gridLayout = new GridLayout(); 173 shell.setLayout(gridLayout); 174 Label label = new Label(shell, SWT.WRAP); 175 label.setText(text); 176 GridData data = new GridData(); 177 org.eclipse.swt.widgets.Monitor.Monitor monitor = parent.getMonitor(); 178 int maxWidth = monitor.getBounds().width * 2 / 3; 179 int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 180 data.widthHint = Math.min(width, maxWidth); 181 data.horizontalAlignment = GridData.FILL; 182 data.grabExcessHorizontalSpace = true; 183 label.setLayoutData (data); 184 185 Text valueText = new Text(shell, SWT.BORDER); 186 if (value !is null) valueText.setText(value); 187 data = new GridData(); 188 width = valueText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 189 if (width > maxWidth) data.widthHint = maxWidth; 190 data.horizontalAlignment = GridData.FILL; 191 data.grabExcessHorizontalSpace = true; 192 valueText.setLayoutData(data); 193 194 Button[] buttons = new Button[3]; 195 Listener listener = new class() Listener { 196 public void handleEvent(Event event) { 197 if (buttons[0] !is null) checkValue = buttons[0].getSelection() ? 1 : 0; 198 value = valueText.getText(); 199 result = event.widget is buttons[1] ? 1 : 0; 200 shell.close(); 201 } 202 }; 203 if (check !is null) { 204 buttons[0] = new Button(shell, SWT.CHECK); 205 buttons[0].setText(check); 206 buttons[0].setSelection(checkValue !is 0); 207 data = new GridData (); 208 data.horizontalAlignment = GridData.BEGINNING; 209 buttons[0].setLayoutData (data); 210 } 211 Composite composite = new Composite(shell, SWT.NONE); 212 data = new GridData(); 213 data.horizontalAlignment = GridData.CENTER; 214 composite.setLayoutData (data); 215 composite.setLayout(new GridLayout(2, true)); 216 buttons[1] = new Button(composite, SWT.PUSH); 217 //buttons[1].setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$ 218 buttons[1].setText("OK"); 219 buttons[1].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 220 buttons[1].addListener(SWT.Selection, listener); 221 buttons[2] = new Button(composite, SWT.PUSH); 222 //buttons[2].setText(SWT.getMessage("SWT_Cancel")); //$NON-NLS-1$ 223 buttons[2].setText("Cancel"); 224 buttons[2].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 225 buttons[2].addListener(SWT.Selection, listener); 226 227 shell.pack(); 228 shell.open(); 229 Display display = parent.getDisplay(); 230 while (!shell.isDisposed()) { 231 if (!display.readAndDispatch()) display.sleep(); 232 } 233 } 234 235 void promptUsernameAndPassword(String title, String text, String check, ref String user, ref String pass, ref int checkValue, ref int result) { 236 Shell parent = getParent(); 237 /* final */ Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 238 shell.setText(title); 239 GridLayout gridLayout = new GridLayout(); 240 shell.setLayout(gridLayout); 241 Label label = new Label(shell, SWT.WRAP); 242 label.setText(text); 243 GridData data = new GridData(); 244 org.eclipse.swt.widgets.Monitor.Monitor monitor = parent.getMonitor(); 245 int maxWidth = monitor.getBounds().width * 2 / 3; 246 int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 247 data.widthHint = Math.min(width, maxWidth); 248 data.horizontalAlignment = GridData.FILL; 249 data.grabExcessHorizontalSpace = true; 250 label.setLayoutData (data); 251 252 Label userLabel = new Label(shell, SWT.NONE); 253 //userLabel.setText(SWT.getMessage("SWT_Username")); //$NON-NLS-1$ 254 userLabel.setText("Username:"); 255 Text userText = new Text(shell, SWT.BORDER); 256 if (user !is null) userText.setText(user); 257 data = new GridData(); 258 data.horizontalAlignment = GridData.FILL; 259 data.grabExcessHorizontalSpace = true; 260 userText.setLayoutData(data); 261 262 Label passwordLabel = new Label(shell, SWT.NONE); 263 //passwordLabel.setText(SWT.getMessage("SWT_Password")); //$NON-NLS-1$ 264 passwordLabel.setText("Password:"); 265 Text passwordText = new Text(shell, SWT.PASSWORD | SWT.BORDER); 266 if (pass !is null) passwordText.setText(pass); 267 data = new GridData(); 268 data.horizontalAlignment = GridData.FILL; 269 data.grabExcessHorizontalSpace = true; 270 passwordText.setLayoutData(data); 271 272 Button[] buttons = new Button[3]; 273 Listener listener = new class() Listener { 274 public void handleEvent(Event event) { 275 if (buttons[0] !is null) checkValue = buttons[0].getSelection() ? 1 : 0; 276 user = userText.getText(); 277 pass = passwordText.getText(); 278 result = event.widget is buttons[1] ? 1 : 0; 279 shell.close(); 280 } 281 }; 282 if (check !is null) { 283 buttons[0] = new Button(shell, SWT.CHECK); 284 buttons[0].setText(check); 285 buttons[0].setSelection(checkValue !is 0); 286 data = new GridData (); 287 data.horizontalAlignment = GridData.BEGINNING; 288 buttons[0].setLayoutData (data); 289 } 290 Composite composite = new Composite(shell, SWT.NONE); 291 data = new GridData(); 292 data.horizontalAlignment = GridData.CENTER; 293 composite.setLayoutData (data); 294 composite.setLayout(new GridLayout(2, true)); 295 buttons[1] = new Button(composite, SWT.PUSH); 296 //buttons[1].setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$ 297 buttons[1].setText("OK"); 298 buttons[1].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 299 buttons[1].addListener(SWT.Selection, listener); 300 buttons[2] = new Button(composite, SWT.PUSH); 301 //buttons[2].setText(SWT.getMessage("SWT_Cancel")); //$NON-NLS-1$ 302 buttons[2].setText("Cancel"); 303 buttons[2].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 304 buttons[2].addListener(SWT.Selection, listener); 305 306 shell.setDefaultButton(buttons[1]); 307 shell.pack(); 308 shell.open(); 309 Display display = parent.getDisplay(); 310 while (!shell.isDisposed()) { 311 if (!display.readAndDispatch()) display.sleep(); 312 } 313 } 314 }