1 12 package org.eclipse.jface.dialogs; 13 14 import org.eclipse.jface.layout.GridDataFactory; 15 import org.eclipse.jface.layout.GridLayoutFactory; 16 import org.eclipse.jface.layout.LayoutConstants; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.accessibility.AccessibleAdapter; 20 import org.eclipse.swt.accessibility.AccessibleEvent; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.swt.widgets.Label; 27 import org.eclipse.swt.widgets.Shell; 28 29 35 public abstract class IconAndMessageDialog extends Dialog { 36 39 protected String message; 40 41 44 protected Label messageLabel; 45 46 49 protected Label imageLabel; 50 51 58 public IconAndMessageDialog(Shell parentShell) { 59 super(parentShell); 60 } 61 62 73 protected Control createMessageArea(Composite composite) { 74 Image image = getImage(); 77 if (image != null) { 78 imageLabel = new Label(composite, SWT.NULL); 79 image.setBackground(imageLabel.getBackground()); 80 imageLabel.setImage(image); 81 addAccessibleListeners(imageLabel, image); 82 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING) 83 .applyTo(imageLabel); 84 } 85 if (message != null) { 87 messageLabel = new Label(composite, getMessageLabelStyle()); 88 messageLabel.setText(message); 89 GridDataFactory 90 .fillDefaults() 91 .align(SWT.FILL, SWT.BEGINNING) 92 .grab(true, false) 93 .hint( 94 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), 95 SWT.DEFAULT).applyTo(messageLabel); 96 } 97 return composite; 98 } 99 100 private String getAccessibleMessageFor(Image image) { 101 if (image.equals(getErrorImage())) { 102 return JFaceResources.getString("error"); } 104 105 if (image.equals(getWarningImage())) { 106 return JFaceResources.getString("warning"); } 108 109 if (image.equals(getInfoImage())) { 110 return JFaceResources.getString("info"); } 112 113 if (image.equals(getQuestionImage())) { 114 return JFaceResources.getString("question"); } 116 117 return null; 118 } 119 120 127 private void addAccessibleListeners(Label label, final Image image) { 128 label.getAccessible().addAccessibleListener(new AccessibleAdapter() { 129 public void getName(AccessibleEvent event) { 130 final String accessibleMessage = getAccessibleMessageFor(image); 131 if (accessibleMessage == null) { 132 return; 133 } 134 event.result = accessibleMessage; 135 } 136 }); 137 } 138 139 146 protected int getMessageLabelStyle() { 147 return SWT.WRAP; 148 } 149 150 153 protected Control createButtonBar(Composite parent) { 154 Composite composite = new Composite(parent, SWT.NONE); 155 GridLayoutFactory.fillDefaults().numColumns(0) .equalWidth(true).applyTo(composite); 158 159 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).span( 160 2, 1).applyTo(composite); 161 composite.setFont(parent.getFont()); 162 createButtonsForButtonBar(composite); 164 return composite; 165 } 166 167 176 protected abstract Image getImage(); 177 178 181 protected Control createContents(Composite parent) { 182 initializeDialogUnits(parent); 184 Point defaultMargins = LayoutConstants.getMargins(); 185 Point defaultSpacing = LayoutConstants.getSpacing(); 186 GridLayoutFactory.fillDefaults().margins(defaultMargins.x, 187 defaultMargins.y * 3 / 2).spacing(defaultSpacing.x * 2, 188 defaultSpacing.y).numColumns(getColumnCount()).applyTo( 189 parent); 190 191 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); 192 createDialogAndButtonArea(parent); 193 return parent; 194 } 195 196 202 int getColumnCount() { 203 return 2; 204 } 205 206 211 protected void createDialogAndButtonArea(Composite parent) { 212 dialogArea = createDialogArea(parent); 214 buttonBar = createButtonBar(parent); 215 applyDialogFont(parent); 217 } 218 219 224 public Image getErrorImage() { 225 return getSWTImage(SWT.ICON_ERROR); 226 } 227 228 233 public Image getWarningImage() { 234 return getSWTImage(SWT.ICON_WARNING); 235 } 236 237 242 public Image getInfoImage() { 243 return getSWTImage(SWT.ICON_INFORMATION); 244 } 245 246 251 public Image getQuestionImage() { 252 return getSWTImage(SWT.ICON_QUESTION); 253 } 254 255 262 private Image getSWTImage(final int imageID) { 263 Shell shell = getShell(); 264 final Display display; 265 if (shell == null) { 266 shell = getParentShell(); 267 } 268 if (shell == null) { 269 display = Display.getCurrent(); 270 } else { 271 display = shell.getDisplay(); 272 } 273 274 final Image[] image = new Image[1]; 275 display.syncExec(new Runnable () { 276 public void run() { 277 image[0] = display.getSystemImage(imageID); 278 } 279 }); 280 281 return image[0]; 282 283 } 284 285 } 286 | Popular Tags |