1 11 package org.eclipse.jface.dialogs; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.custom.CLabel; 15 import org.eclipse.swt.graphics.Image; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Shell; 23 24 31 public class MessageDialog extends IconAndMessageDialog { 32 35 public final static int NONE = 0; 36 37 40 public final static int ERROR = 1; 41 42 45 public final static int INFORMATION = 2; 46 47 50 public final static int QUESTION = 3; 51 52 55 public final static int WARNING = 4; 56 57 60 private String [] buttonLabels; 61 62 65 private Button[] buttons; 66 67 70 private int defaultButtonIndex; 71 72 75 private String title; 76 77 80 private Image titleImage; 81 82 85 private Image image = null; 86 87 90 private Control customArea; 91 92 131 public MessageDialog(Shell parentShell, String dialogTitle, 132 Image dialogTitleImage, String dialogMessage, int dialogImageType, 133 String [] dialogButtonLabels, int defaultIndex) { 134 super(parentShell); 135 this.title = dialogTitle; 136 this.titleImage = dialogTitleImage; 137 this.message = dialogMessage; 138 139 switch (dialogImageType) { 140 case ERROR: { 141 this.image = getErrorImage(); 142 break; 143 } 144 case INFORMATION: { 145 this.image = getInfoImage(); 146 break; 147 } 148 case QUESTION: { 149 this.image = getQuestionImage(); 150 break; 151 } 152 case WARNING: { 153 this.image = getWarningImage(); 154 break; 155 } 156 } 157 this.buttonLabels = dialogButtonLabels; 158 this.defaultButtonIndex = defaultIndex; 159 } 160 161 165 protected void buttonPressed(int buttonId) { 166 setReturnCode(buttonId); 167 close(); 168 } 169 170 174 protected void configureShell(Shell shell) { 175 super.configureShell(shell); 176 if (title != null) { 177 shell.setText(title); 178 } 179 if (titleImage != null) { 180 shell.setImage(titleImage); 181 } 182 } 183 184 187 protected void createButtonsForButtonBar(Composite parent) { 188 buttons = new Button[buttonLabels.length]; 189 for (int i = 0; i < buttonLabels.length; i++) { 190 String label = buttonLabels[i]; 191 Button button = createButton(parent, i, label, 192 defaultButtonIndex == i); 193 buttons[i] = button; 194 } 195 } 196 197 209 protected Control createCustomArea(Composite parent) { 210 return null; 211 } 212 213 220 protected Control createDialogArea(Composite parent) { 221 createMessageArea(parent); 223 Composite composite = new Composite(parent, SWT.NONE); 225 GridLayout layout = new GridLayout(); 226 layout.marginHeight = 0; 227 layout.marginWidth = 0; 228 composite.setLayout(layout); 229 GridData data = new GridData(GridData.FILL_BOTH); 230 data.horizontalSpan = 2; 231 composite.setLayoutData(data); 232 customArea = createCustomArea(composite); 234 if (customArea == null) { 236 customArea = new Label(composite, SWT.NULL); 237 } 238 return composite; 239 } 240 241 248 protected Button getButton(int index) { 249 return buttons[index]; 250 } 251 252 261 protected int getMinimumMessageWidth() { 262 return convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); 263 } 264 265 271 protected void handleShellCloseEvent() { 272 super.handleShellCloseEvent(); 275 setReturnCode(SWT.DEFAULT); 276 } 277 278 290 public static boolean openConfirm(Shell parent, String title, String message) { 291 MessageDialog dialog = new MessageDialog(parent, title, null, message, QUESTION, new String [] { IDialogConstants.OK_LABEL, 297 IDialogConstants.CANCEL_LABEL }, 0); return dialog.open() == 0; 300 } 301 302 312 public static void openError(Shell parent, String title, String message) { 313 MessageDialog dialog = new MessageDialog(parent, title, null, message, ERROR, new String [] { IDialogConstants.OK_LABEL }, 0); dialog.open(); 323 return; 324 } 325 326 336 public static void openInformation(Shell parent, String title, 337 String message) { 338 MessageDialog dialog = new MessageDialog(parent, title, null, message, INFORMATION, 344 new String [] { IDialogConstants.OK_LABEL }, 0); 345 dialog.open(); 347 return; 348 } 349 350 362 public static boolean openQuestion(Shell parent, String title, 363 String message) { 364 MessageDialog dialog = new MessageDialog(parent, title, null, message, QUESTION, new String [] { IDialogConstants.YES_LABEL, 370 IDialogConstants.NO_LABEL }, 0); return dialog.open() == 0; 372 } 373 374 384 public static void openWarning(Shell parent, String title, String message) { 385 MessageDialog dialog = new MessageDialog(parent, title, null, message, WARNING, new String [] { IDialogConstants.OK_LABEL }, 0); dialog.open(); 395 return; 396 } 397 398 402 protected Button createButton(Composite parent, int id, String label, 403 boolean defaultButton) { 404 Button button = super.createButton(parent, id, label, defaultButton); 405 if (defaultButton && !customShouldTakeFocus()) { 408 button.setFocus(); 409 } 410 return button; 411 } 412 413 421 protected boolean customShouldTakeFocus() { 422 if (customArea instanceof Label) { 423 return false; 424 } 425 if (customArea instanceof CLabel) { 426 return (customArea.getStyle() & SWT.NO_FOCUS) > 0; 427 } 428 return true; 429 } 430 431 435 public Image getImage() { 436 return image; 437 } 438 439 444 protected String [] getButtonLabels() { 445 return buttonLabels; 446 } 447 448 453 protected int getDefaultButtonIndex() { 454 return defaultButtonIndex; 455 } 456 457 463 protected void setButtons(Button[] buttons) { 464 if (buttons == null) { 465 throw new NullPointerException ( 466 "The array of buttons cannot be null.");} this.buttons = buttons; 468 } 469 470 476 protected void setButtonLabels(String [] buttonLabels) { 477 if (buttonLabels == null) { 478 throw new NullPointerException ( 479 "The array of button labels cannot be null.");} this.buttonLabels = buttonLabels; 481 } 482 } 483 | Popular Tags |