1 11 package org.eclipse.jface.dialogs; 12 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.util.Policy; 21 import org.eclipse.jface.window.IShellProvider; 22 import org.eclipse.jface.window.SameShellProvider; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.graphics.Font; 28 import org.eclipse.swt.graphics.FontData; 29 import org.eclipse.swt.graphics.FontMetrics; 30 import org.eclipse.swt.graphics.GC; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.layout.FormData; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Display; 40 import org.eclipse.swt.widgets.Shell; 41 42 57 public abstract class Dialog extends Window { 58 65 public static final String DLG_IMG_ERROR = "dialog_error_image"; 67 73 public static final String DLG_IMG_INFO = "dialog_info_imageg"; 75 81 public static final String DLG_IMG_QUESTION = "dialog_question_image"; 83 90 public static final String DLG_IMG_WARNING = "dialog_warning_image"; 92 98 public static final String DLG_IMG_MESSAGE_INFO = "dialog_messasge_info_image"; 100 106 public static final String DLG_IMG_MESSAGE_WARNING = "dialog_messasge_warning_image"; 108 114 public static final String DLG_IMG_MESSAGE_ERROR = "dialog_message_error_image"; 116 122 public static final String DLG_IMG_HELP = "dialog_help_image"; 124 129 public static final String ELLIPSIS = "..."; 131 136 private static final String DIALOG_ORIGIN_X = "DIALOG_X_ORIGIN"; 138 143 private static final String DIALOG_ORIGIN_Y = "DIALOG_Y_ORIGIN"; 145 150 private static final String DIALOG_WIDTH = "DIALOG_WIDTH"; 152 157 private static final String DIALOG_HEIGHT = "DIALOG_HEIGHT"; 159 165 private static final String DIALOG_FONT_DATA = "DIALOG_FONT_NAME"; 167 173 public static final int DIALOG_DEFAULT_BOUNDS = -1; 174 175 182 183 187 public static final int DIALOG_PERSISTLOCATION = 0x0001; 188 192 public static final int DIALOG_PERSISTSIZE = 0x0002; 193 194 197 protected Control dialogArea; 198 199 202 public Control buttonBar; 203 204 207 private HashMap buttons = new HashMap (); 208 209 212 private FontMetrics fontMetrics; 213 214 217 private static final int HORIZONTAL_DIALOG_UNIT_PER_CHAR = 4; 218 219 222 private static final int VERTICAL_DIALOG_UNITS_PER_CHAR = 8; 223 224 244 public static int convertHeightInCharsToPixels(FontMetrics fontMetrics, 245 int chars) { 246 return fontMetrics.getHeight() * chars; 247 } 248 249 269 public static int convertHorizontalDLUsToPixels(FontMetrics fontMetrics, 270 int dlus) { 271 return (fontMetrics.getAverageCharWidth() * dlus + HORIZONTAL_DIALOG_UNIT_PER_CHAR / 2) 273 / HORIZONTAL_DIALOG_UNIT_PER_CHAR; 274 } 275 276 296 public static int convertVerticalDLUsToPixels(FontMetrics fontMetrics, 297 int dlus) { 298 return (fontMetrics.getHeight() * dlus + VERTICAL_DIALOG_UNITS_PER_CHAR / 2) 300 / VERTICAL_DIALOG_UNITS_PER_CHAR; 301 } 302 303 323 public static int convertWidthInCharsToPixels(FontMetrics fontMetrics, 324 int chars) { 325 return fontMetrics.getAverageCharWidth() * chars; 326 } 327 328 344 public static String shortenText(String textValue, Control control) { 345 if (textValue == null) { 346 return null; 347 } 348 GC gc = new GC(control); 349 int maxWidth = control.getBounds().width - 5; 350 if (gc.textExtent(textValue).x < maxWidth) { 351 gc.dispose(); 352 return textValue; 353 } 354 int length = textValue.length(); 355 int pivot = length / 2; 356 int start = pivot; 357 int end = pivot + 1; 358 while (start >= 0 && end < length) { 359 String s1 = textValue.substring(0, start); 360 String s2 = textValue.substring(end, length); 361 String s = s1 + ELLIPSIS + s2; 362 int l = gc.textExtent(s).x; 363 if (l < maxWidth) { 364 gc.dispose(); 365 return s; 366 } 367 start--; 368 end++; 369 } 370 gc.dispose(); 371 return textValue; 372 } 373 374 378 public static IDialogBlockedHandler blockedHandler = new IDialogBlockedHandler() { 379 384 public void clearBlocked() { 385 } 387 388 394 public void showBlocked(IProgressMonitor blocking, 395 IStatus blockingStatus, String blockedName) { 396 } 398 399 406 public void showBlocked(Shell parentShell, IProgressMonitor blocking, 407 IStatus blockingStatus, String blockedName) { 408 } 410 }; 411 412 421 protected Dialog(Shell parentShell) { 422 this(new SameShellProvider(parentShell)); 423 if (parentShell == null && Policy.DEBUG_DIALOG_NO_PARENT) { 424 Policy.getLog().log( 425 new Status(IStatus.INFO, Policy.JFACE, IStatus.INFO, this 426 .getClass() 427 + " created with no shell", new Exception ())); 429 } 430 } 431 432 440 protected Dialog(IShellProvider parentShell) { 441 super(parentShell); 442 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL 443 | getDefaultOrientation()); 444 setBlockOnOpen(true); 445 } 446 447 462 protected void buttonPressed(int buttonId) { 463 if (IDialogConstants.OK_ID == buttonId) { 464 okPressed(); 465 } else if (IDialogConstants.CANCEL_ID == buttonId) { 466 cancelPressed(); 467 } 468 } 469 470 478 protected void cancelPressed() { 479 setReturnCode(CANCEL); 480 close(); 481 } 482 483 498 protected int convertHeightInCharsToPixels(int chars) { 499 if (fontMetrics == null) { 501 return 0; 502 } 503 return convertHeightInCharsToPixels(fontMetrics, chars); 504 } 505 506 521 protected int convertHorizontalDLUsToPixels(int dlus) { 522 if (fontMetrics == null) { 524 return 0; 525 } 526 return convertHorizontalDLUsToPixels(fontMetrics, dlus); 527 } 528 529 544 protected int convertVerticalDLUsToPixels(int dlus) { 545 if (fontMetrics == null) { 547 return 0; 548 } 549 return convertVerticalDLUsToPixels(fontMetrics, dlus); 550 } 551 552 567 protected int convertWidthInCharsToPixels(int chars) { 568 if (fontMetrics == null) { 570 return 0; 571 } 572 return convertWidthInCharsToPixels(fontMetrics, chars); 573 } 574 575 606 protected Button createButton(Composite parent, int id, String label, 607 boolean defaultButton) { 608 ((GridLayout) parent.getLayout()).numColumns++; 610 Button button = new Button(parent, SWT.PUSH); 611 button.setText(label); 612 button.setFont(JFaceResources.getDialogFont()); 613 button.setData(new Integer (id)); 614 button.addSelectionListener(new SelectionAdapter() { 615 public void widgetSelected(SelectionEvent event) { 616 buttonPressed(((Integer ) event.widget.getData()).intValue()); 617 } 618 }); 619 if (defaultButton) { 620 Shell shell = parent.getShell(); 621 if (shell != null) { 622 shell.setDefaultButton(button); 623 } 624 } 625 buttons.put(new Integer (id), button); 626 setButtonLayoutData(button); 627 return button; 628 } 629 630 646 protected Control createButtonBar(Composite parent) { 647 Composite composite = new Composite(parent, SWT.NONE); 648 GridLayout layout = new GridLayout(); 651 layout.numColumns = 0; layout.makeColumnsEqualWidth = true; 653 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 654 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 655 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 656 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 657 composite.setLayout(layout); 658 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END 659 | GridData.VERTICAL_ALIGN_CENTER); 660 composite.setLayoutData(data); 661 composite.setFont(parent.getFont()); 662 663 createButtonsForButtonBar(composite); 665 return composite; 666 } 667 668 681 protected void createButtonsForButtonBar(Composite parent) { 682 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, 684 true); 685 createButton(parent, IDialogConstants.CANCEL_ID, 686 IDialogConstants.CANCEL_LABEL, false); 687 } 688 689 692 protected void initializeBounds() { 693 String platform = SWT.getPlatform(); 694 if ("carbon".equals(platform)) { Shell shell = getShell(); 697 if (shell != null) { 698 Button defaultButton = shell.getDefaultButton(); 699 if (defaultButton != null 700 && isContained(buttonBar, defaultButton)) { 701 defaultButton.moveBelow(null); 702 } 703 } 704 } 705 706 super.initializeBounds(); 707 } 708 709 718 private boolean isContained(Control container, Control control) { 719 Composite parent; 720 while ((parent = control.getParent()) != null) { 721 if (parent == container) { 722 return true; 723 } 724 control = parent; 725 } 726 return false; 727 } 728 729 739 protected Control createContents(Composite parent) { 740 Composite composite = new Composite(parent, 0); 742 GridLayout layout = new GridLayout(); 743 layout.marginHeight = 0; 744 layout.marginWidth = 0; 745 layout.verticalSpacing = 0; 746 composite.setLayout(layout); 747 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 748 applyDialogFont(composite); 749 initializeDialogUnits(composite); 751 dialogArea = createDialogArea(composite); 753 buttonBar = createButtonBar(composite); 754 755 return composite; 756 } 757 758 786 protected Control createDialogArea(Composite parent) { 787 Composite composite = new Composite(parent, SWT.NONE); 789 GridLayout layout = new GridLayout(); 790 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 791 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 792 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 793 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 794 composite.setLayout(layout); 795 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 796 applyDialogFont(composite); 797 return composite; 798 } 799 800 815 protected Button getButton(int id) { 816 return (Button) buttons.get(new Integer (id)); 817 } 818 819 828 protected Control getButtonBar() { 829 return buttonBar; 830 } 831 832 846 protected Button getCancelButton() { 847 return getButton(IDialogConstants.CANCEL_ID); 848 } 849 850 859 protected Control getDialogArea() { 860 return dialogArea; 861 } 862 863 881 public static Image getImage(String key) { 882 return JFaceResources.getImageRegistry().get(key); 883 } 884 885 899 protected Button getOKButton() { 900 return getButton(IDialogConstants.OK_ID); 901 } 902 903 914 protected void initializeDialogUnits(Control control) { 915 GC gc = new GC(control); 917 gc.setFont(JFaceResources.getDialogFont()); 918 fontMetrics = gc.getFontMetrics(); 919 gc.dispose(); 920 } 921 922 930 protected void okPressed() { 931 setReturnCode(OK); 932 close(); 933 } 934 935 941 protected void setButtonLayoutData(Button button) { 942 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 943 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 944 Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 945 data.widthHint = Math.max(widthHint, minSize.x); 946 button.setLayoutData(data); 947 } 948 949 955 protected void setButtonLayoutFormData(Button button) { 956 FormData data = new FormData(); 957 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 958 Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 959 data.width = Math.max(widthHint, minSize.x); 960 button.setLayoutData(data); 961 } 962 963 966 public boolean close() { 967 if (getShell() != null && !getShell().isDisposed()) { 968 saveDialogBounds(getShell()); 969 } 970 971 boolean returnValue = super.close(); 972 if (returnValue) { 973 buttons = new HashMap (); 974 buttonBar = null; 975 dialogArea = null; 976 } 977 return returnValue; 978 } 979 980 989 public static void applyDialogFont(Control control) { 990 if (control == null || dialogFontIsDefault()) { 991 return; 992 } 993 Font dialogFont = JFaceResources.getDialogFont(); 994 applyDialogFont(control, dialogFont); 995 } 996 997 1007 private static void applyDialogFont(Control control, Font dialogFont) { 1008 if (hasDefaultFont(control)) { 1009 control.setFont(dialogFont); 1010 } 1011 if (control instanceof Composite) { 1012 Control[] children = ((Composite) control).getChildren(); 1013 for (int i = 0; i < children.length; i++) { 1014 applyDialogFont(children[i], dialogFont); 1015 } 1016 } 1017 } 1018 1019 1026 private static boolean hasDefaultFont(Control control) { 1027 FontData[] controlFontData = control.getFont().getFontData(); 1028 FontData[] defaultFontData = getDefaultFont(control).getFontData(); 1029 if (controlFontData.length == defaultFontData.length) { 1030 for (int i = 0; i < controlFontData.length; i++) { 1031 if (controlFontData[i].equals(defaultFontData[i])) { 1032 continue; 1033 } 1034 return false; 1035 } 1036 return true; 1037 } 1038 return false; 1039 } 1040 1041 1047 private static Font getDefaultFont(Control control) { 1048 String fontName = "DEFAULT_FONT_" + control.getClass().getName(); if (JFaceResources.getFontRegistry().hasValueFor(fontName)) { 1050 return JFaceResources.getFontRegistry().get(fontName); 1051 } 1052 Font cached = control.getFont(); 1053 control.setFont(null); 1054 Font defaultFont = control.getFont(); 1055 control.setFont(cached); 1056 JFaceResources.getFontRegistry().put(fontName, 1057 defaultFont.getFontData()); 1058 return defaultFont; 1059 } 1060 1061 1067 protected static boolean dialogFontIsDefault() { 1068 FontData[] dialogFontData = JFaceResources.getFontRegistry() 1069 .getFontData(JFaceResources.DIALOG_FONT); 1070 FontData[] defaultFontData = JFaceResources.getFontRegistry() 1071 .getFontData(JFaceResources.DEFAULT_FONT); 1072 return Arrays.equals(dialogFontData, defaultFontData); 1073 } 1074 1075 1080 public void create() { 1081 super.create(); 1082 applyDialogFont(buttonBar); 1083 } 1084 1085 1091 public static IDialogBlockedHandler getBlockedHandler() { 1092 return blockedHandler; 1093 } 1094 1095 1102 public static void setBlockedHandler(IDialogBlockedHandler blockedHandler) { 1103 Dialog.blockedHandler = blockedHandler; 1104 } 1105 1106 1117 protected IDialogSettings getDialogBoundsSettings() { 1118 return null; 1119 } 1120 1121 1135 protected int getDialogBoundsStrategy() { 1136 return DIALOG_PERSISTLOCATION | DIALOG_PERSISTSIZE; 1137 } 1138 1139 1149 private void saveDialogBounds(Shell shell) { 1150 IDialogSettings settings = getDialogBoundsSettings(); 1151 if (settings != null) { 1152 Point shellLocation = shell.getLocation(); 1153 Point shellSize = shell.getSize(); 1154 Shell parent = getParentShell(); 1155 if (parent != null) { 1156 Point parentLocation = parent.getLocation(); 1157 shellLocation.x -= parentLocation.x; 1158 shellLocation.y -= parentLocation.y; 1159 } 1160 int strategy = getDialogBoundsStrategy(); 1161 if ((strategy & DIALOG_PERSISTLOCATION) != 0) { 1162 settings.put(DIALOG_ORIGIN_X, shellLocation.x); 1163 settings.put(DIALOG_ORIGIN_Y, shellLocation.y); 1164 } 1165 if ((strategy & DIALOG_PERSISTSIZE) != 0) { 1166 settings.put(DIALOG_WIDTH, shellSize.x); 1167 settings.put(DIALOG_HEIGHT, shellSize.y); 1168 FontData [] fontDatas = JFaceResources.getDialogFont().getFontData(); 1169 if (fontDatas.length > 0) { 1170 settings.put(DIALOG_FONT_DATA, fontDatas[0].toString()); 1171 } 1172 } 1173 } 1174 } 1175 1176 1187 protected Point getInitialSize() { 1188 Point result = super.getInitialSize(); 1189 1190 if ((getDialogBoundsStrategy() & DIALOG_PERSISTSIZE)!= 0) { 1192 IDialogSettings settings = getDialogBoundsSettings(); 1193 if (settings != null) { 1194 boolean useStoredBounds = true; 1199 String previousDialogFontData = settings.get(DIALOG_FONT_DATA); 1200 if (previousDialogFontData != null && previousDialogFontData.length() > 0) { 1205 FontData [] fontDatas = JFaceResources.getDialogFont().getFontData(); 1206 if (fontDatas.length > 0) { 1207 String currentDialogFontData = fontDatas[0].toString(); 1208 useStoredBounds = currentDialogFontData.equalsIgnoreCase(previousDialogFontData); 1209 } 1210 } 1211 if (useStoredBounds) { 1212 try { 1213 int width = settings.getInt(DIALOG_WIDTH); 1215 if (width != DIALOG_DEFAULT_BOUNDS) { 1216 result.x = width; 1217 } 1218 int height = settings.getInt(DIALOG_HEIGHT); 1219 if (height != DIALOG_DEFAULT_BOUNDS) { 1220 result.y = height; 1221 } 1222 1223 } catch (NumberFormatException e) { 1224 } 1225 } 1226 } 1227 } 1228 return result; 1231 } 1232 1233 1247 protected Point getInitialLocation(Point initialSize) { 1248 Point result = super.getInitialLocation(initialSize); 1249 if ((getDialogBoundsStrategy() & DIALOG_PERSISTLOCATION)!= 0) { 1250 IDialogSettings settings = getDialogBoundsSettings(); 1251 if (settings != null) { 1252 try { 1253 int x = settings.getInt(DIALOG_ORIGIN_X); 1254 int y = settings.getInt(DIALOG_ORIGIN_Y); 1255 result = new Point(x, y); 1256 Shell parent = getParentShell(); 1259 if (parent != null) { 1260 Point parentLocation = parent.getLocation(); 1261 result.x += parentLocation.x; 1262 result.y += parentLocation.y; 1263 } 1264 } catch (NumberFormatException e) { 1265 } 1266 } 1267 } 1268 return result; 1271 } 1272} 1273 | Popular Tags |