1 15 package org.eclipse.jface.dialogs; 16 17 import org.eclipse.jface.resource.JFaceColors; 18 import org.eclipse.jface.resource.JFaceResources; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.DisposeEvent; 21 import org.eclipse.swt.events.DisposeListener; 22 import org.eclipse.swt.graphics.Color; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.graphics.RGB; 26 import org.eclipse.swt.layout.FormAttachment; 27 import org.eclipse.swt.layout.FormData; 28 import org.eclipse.swt.layout.FormLayout; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.swt.widgets.Text; 37 38 44 public class TitleAreaDialog extends TrayDialog { 45 48 public static final String DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR; 49 50 54 public static final String DLG_IMG_TITLE_BANNER = "dialog_title_banner_image"; 56 62 public final static String INFO_MESSAGE = "INFO_MESSAGE"; 64 70 public final static String WARNING_MESSAGE = "WARNING_MESSAGE"; 72 private static final int H_GAP_IMAGE = 5; 74 75 private static final int MIN_DIALOG_WIDTH = 350; 77 78 private static final int MIN_DIALOG_HEIGHT = 150; 80 81 private Label titleLabel; 82 83 private Label titleImageLabel; 84 85 private Label bottomFillerLabel; 86 87 private Label leftFillerLabel; 88 89 private RGB titleAreaRGB; 90 91 Color titleAreaColor; 92 93 private String message = ""; 95 private String errorMessage; 96 97 private Text messageLabel; 98 99 private Composite workArea; 100 101 private Label messageImageLabel; 102 103 private Image messageImage; 104 105 private boolean showingError = false; 106 107 private boolean titleImageLargest = true; 108 109 private int messageLabelHeight; 110 111 private Image titleAreaImage; 112 113 119 public TitleAreaDialog(Shell parentShell) { 120 super(parentShell); 121 } 122 123 126 protected Control createContents(Composite parent) { 127 Composite contents = new Composite(parent, SWT.NONE); 129 contents.setLayoutData(new GridData(GridData.FILL_BOTH)); 130 initializeDialogUnits(contents); 132 FormLayout layout = new FormLayout(); 133 contents.setLayout(layout); 134 workArea = new Composite(contents, SWT.NONE); 136 GridLayout childLayout = new GridLayout(); 137 childLayout.marginHeight = 0; 138 childLayout.marginWidth = 0; 139 childLayout.verticalSpacing = 0; 140 workArea.setLayout(childLayout); 141 Control top = createTitleArea(contents); 142 resetWorkAreaAttachments(top); 143 workArea.setFont(JFaceResources.getDialogFont()); 144 initializeDialogUnits(workArea); 146 dialogArea = createDialogArea(workArea); 148 buttonBar = createButtonBar(workArea); 149 return contents; 150 } 151 152 165 protected Control createDialogArea(Composite parent) { 166 Composite composite = new Composite(parent, SWT.NONE); 168 GridLayout layout = new GridLayout(); 169 layout.marginHeight = 0; 170 layout.marginWidth = 0; 171 layout.verticalSpacing = 0; 172 layout.horizontalSpacing = 0; 173 composite.setLayout(layout); 174 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 175 composite.setFont(parent.getFont()); 176 Label titleBarSeparator = new Label(composite, SWT.HORIZONTAL 178 | SWT.SEPARATOR); 179 titleBarSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 180 return composite; 181 } 182 183 190 private Control createTitleArea(Composite parent) { 191 192 parent.addDisposeListener(new DisposeListener() { 194 public void widgetDisposed(DisposeEvent e) { 195 if (titleAreaColor != null) { 196 titleAreaColor.dispose(); 197 } 198 } 199 }); 200 Display display = parent.getDisplay(); 202 Color background; 203 Color foreground; 204 if (titleAreaRGB != null) { 205 titleAreaColor = new Color(display, titleAreaRGB); 206 background = titleAreaColor; 207 foreground = null; 208 } else { 209 background = JFaceColors.getBannerBackground(display); 210 foreground = JFaceColors.getBannerForeground(display); 211 } 212 213 parent.setBackground(background); 214 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 215 int horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 216 titleImageLabel = new Label(parent, SWT.CENTER); 218 titleImageLabel.setBackground(background); 219 if (titleAreaImage == null) 220 titleImageLabel.setImage(JFaceResources 221 .getImage(DLG_IMG_TITLE_BANNER)); 222 else 223 titleImageLabel.setImage(titleAreaImage); 224 225 FormData imageData = new FormData(); 226 imageData.top = new FormAttachment(0, 0); 227 imageData.right = new FormAttachment(100, 0); titleImageLabel.setLayoutData(imageData); 234 titleLabel = new Label(parent, SWT.LEFT); 236 JFaceColors.setColors(titleLabel, foreground, background); 237 titleLabel.setFont(JFaceResources.getBannerFont()); 238 titleLabel.setText(" "); FormData titleData = new FormData(); 240 titleData.top = new FormAttachment(0, verticalSpacing); 241 titleData.right = new FormAttachment(titleImageLabel); 242 titleData.left = new FormAttachment(0, horizontalSpacing); 243 titleLabel.setLayoutData(titleData); 244 messageImageLabel = new Label(parent, SWT.CENTER); 246 messageImageLabel.setBackground(background); 247 messageLabel = new Text(parent, SWT.WRAP | SWT.READ_ONLY); 249 JFaceColors.setColors(messageLabel, foreground, background); 250 messageLabel.setText(" \n "); messageLabel.setFont(JFaceResources.getDialogFont()); 252 messageLabelHeight = messageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; 253 leftFillerLabel = new Label(parent, SWT.CENTER); 255 leftFillerLabel.setBackground(background); 256 bottomFillerLabel = new Label(parent, SWT.CENTER); 257 bottomFillerLabel.setBackground(background); 258 setLayoutsForNormalMessage(verticalSpacing, horizontalSpacing); 259 determineTitleImageLargest(); 260 if (titleImageLargest) 261 return titleImageLabel; 262 return messageLabel; 263 } 264 265 269 private void determineTitleImageLargest() { 270 int titleY = titleImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; 271 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 272 int labelY = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; 273 labelY += verticalSpacing; 274 labelY += messageLabelHeight; 275 labelY += verticalSpacing; 276 titleImageLargest = titleY > labelY; 277 } 278 279 288 private void setLayoutsForNormalMessage(int verticalSpacing, 289 int horizontalSpacing) { 290 FormData messageImageData = new FormData(); 291 messageImageData.top = new FormAttachment(titleLabel, verticalSpacing); 292 messageImageData.left = new FormAttachment(0, H_GAP_IMAGE); 293 messageImageLabel.setLayoutData(messageImageData); 294 FormData messageLabelData = new FormData(); 295 messageLabelData.top = new FormAttachment(titleLabel, verticalSpacing); 296 messageLabelData.right = new FormAttachment(titleImageLabel); 297 messageLabelData.left = new FormAttachment(messageImageLabel, 298 horizontalSpacing); 299 messageLabelData.height = messageLabelHeight; 300 if (titleImageLargest) 301 messageLabelData.bottom = new FormAttachment(titleImageLabel, 0, 302 SWT.BOTTOM); 303 messageLabel.setLayoutData(messageLabelData); 304 FormData fillerData = new FormData(); 305 fillerData.left = new FormAttachment(0, horizontalSpacing); 306 fillerData.top = new FormAttachment(messageImageLabel, 0); 307 fillerData.bottom = new FormAttachment(messageLabel, 0, SWT.BOTTOM); 308 bottomFillerLabel.setLayoutData(fillerData); 309 FormData data = new FormData(); 310 data.top = new FormAttachment(messageImageLabel, 0, SWT.TOP); 311 data.left = new FormAttachment(0, 0); 312 data.bottom = new FormAttachment(messageImageLabel, 0, SWT.BOTTOM); 313 data.right = new FormAttachment(messageImageLabel, 0); 314 leftFillerLabel.setLayoutData(data); 315 } 316 317 324 protected Point getInitialSize() { 325 Point shellSize = super.getInitialSize(); 326 return new Point(Math.max( 327 convertHorizontalDLUsToPixels(MIN_DIALOG_WIDTH), shellSize.x), 328 Math.max(convertVerticalDLUsToPixels(MIN_DIALOG_HEIGHT), 329 shellSize.y)); 330 } 331 332 341 protected Composite getTitleArea() { 342 return getShell(); 343 } 344 345 350 protected Label getTitleImageLabel() { 351 return titleImageLabel; 352 } 353 354 362 public void setErrorMessage(String newErrorMessage) { 363 if (errorMessage == null ? newErrorMessage == null : errorMessage 365 .equals(newErrorMessage)) 366 return; 367 errorMessage = newErrorMessage; 368 369 if (errorMessage == null) { 371 if (showingError) { 372 showingError = false; 374 } 375 if (message == null) message = ""; updateMessage(message); 383 messageImageLabel.setImage(messageImage); 384 setImageLabelVisible(messageImage != null); 385 } else { 386 String displayedErrorMessage = " " + errorMessage; updateMessage(displayedErrorMessage); 390 if (!showingError) { 391 showingError = true; 393 messageImageLabel.setImage(JFaceResources 394 .getImage(DLG_IMG_TITLE_ERROR)); 395 setImageLabelVisible(true); 396 } 397 } 398 layoutForNewMessage(); 399 } 400 401 404 private void layoutForNewMessage() { 405 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 406 int horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 407 if (errorMessage == null && messageImage == null) { 409 setImageLabelVisible(false); 410 setLayoutsForNormalMessage(verticalSpacing, horizontalSpacing); 411 } else { 412 messageImageLabel.setVisible(true); 413 bottomFillerLabel.setVisible(true); 414 leftFillerLabel.setVisible(true); 415 421 FormData data = new FormData(); 422 data.left = new FormAttachment(0, H_GAP_IMAGE); 423 data.top = new FormAttachment(titleLabel, verticalSpacing); 424 messageImageLabel.setLayoutData(data); 425 data = new FormData(); 426 data.top = new FormAttachment(messageImageLabel, 0); 427 data.left = new FormAttachment(0, 0); 428 data.bottom = new FormAttachment(messageLabel, 0, SWT.BOTTOM); 429 data.right = new FormAttachment(messageImageLabel, 0, SWT.RIGHT); 430 bottomFillerLabel.setLayoutData(data); 431 data = new FormData(); 432 data.top = new FormAttachment(messageImageLabel, 0, SWT.TOP); 433 data.left = new FormAttachment(0, 0); 434 data.bottom = new FormAttachment(messageImageLabel, 0, SWT.BOTTOM); 435 data.right = new FormAttachment(messageImageLabel, 0); 436 leftFillerLabel.setLayoutData(data); 437 FormData messageLabelData = new FormData(); 438 messageLabelData.top = new FormAttachment(titleLabel, 439 verticalSpacing); 440 messageLabelData.right = new FormAttachment(titleImageLabel); 441 messageLabelData.left = new FormAttachment(messageImageLabel, 0); 442 messageLabelData.height = messageLabelHeight; 443 if (titleImageLargest) 444 messageLabelData.bottom = new FormAttachment(titleImageLabel, 445 0, SWT.BOTTOM); 446 messageLabel.setLayoutData(messageLabelData); 447 } 448 if (dialogArea != null) 451 workArea.getParent().layout(true); 452 } 453 454 467 public void setMessage(String newMessage) { 468 setMessage(newMessage, IMessageProvider.NONE); 469 } 470 471 493 public void setMessage(String newMessage, int newType) { 494 Image newImage = null; 495 if (newMessage != null) { 496 switch (newType) { 497 case IMessageProvider.NONE: 498 break; 499 case IMessageProvider.INFORMATION: 500 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_INFO); 501 break; 502 case IMessageProvider.WARNING: 503 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_WARNING); 504 break; 505 case IMessageProvider.ERROR: 506 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_ERROR); 507 break; 508 } 509 } 510 showMessage(newMessage, newImage); 511 } 512 513 519 private void showMessage(String newMessage, Image newImage) { 520 if (message.equals(newMessage) && messageImage == newImage) { 522 return; 523 } 524 message = newMessage; 525 if (message == null) 526 message = ""; String shownMessage = (newImage == null) ? message : " " + message; messageImage = newImage; 531 if (!showingError) { 532 updateMessage(shownMessage); 534 messageImageLabel.setImage(messageImage); 535 setImageLabelVisible(messageImage != null); 536 layoutForNewMessage(); 537 } 538 } 539 540 546 private void updateMessage(String newMessage) { 547 messageLabel.setText(newMessage); 548 } 549 550 556 public void setTitle(String newTitle) { 557 if (titleLabel == null) 558 return; 559 String title = newTitle; 560 if (title == null) 561 title = ""; titleLabel.setText(title); 563 } 564 565 571 public void setTitleAreaColor(RGB color) { 572 titleAreaRGB = color; 573 } 574 575 581 public void setTitleImage(Image newTitleImage) { 582 583 titleAreaImage = newTitleImage; 584 if (titleImageLabel != null) { 585 titleImageLabel.setImage(newTitleImage); 586 titleImageLabel.setVisible(newTitleImage != null); 587 if (newTitleImage != null) { 588 determineTitleImageLargest(); 589 Control top; 590 if (titleImageLargest) 591 top = titleImageLabel; 592 else 593 top = messageLabel; 594 resetWorkAreaAttachments(top); 595 } 596 } 597 } 598 599 607 private void setImageLabelVisible(boolean visible) { 608 messageImageLabel.setVisible(visible); 609 bottomFillerLabel.setVisible(visible); 610 leftFillerLabel.setVisible(visible); 611 } 612 613 619 private void resetWorkAreaAttachments(Control top) { 620 FormData childData = new FormData(); 621 childData.top = new FormAttachment(top); 622 childData.right = new FormAttachment(100, 0); 623 childData.left = new FormAttachment(0, 0); 624 childData.bottom = new FormAttachment(100, 0); 625 workArea.setLayoutData(childData); 626 } 627 } 628 | Popular Tags |