1 11 package org.eclipse.jface.dialogs; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.core.runtime.Status; 15 import org.eclipse.jface.resource.JFaceColors; 16 import org.eclipse.jface.resource.JFaceResources; 17 import org.eclipse.jface.util.Policy; 18 import org.eclipse.jface.util.Util; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.CLabel; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Shell; 29 30 38 public abstract class StatusDialog extends TrayDialog { 39 40 private Button fOkButton; 41 42 private MessageLine fStatusLine; 43 44 private IStatus fLastStatus; 45 46 private String fTitle; 47 48 private Image fImage; 49 50 private boolean fStatusLineAboveButtons = true; 51 52 55 private class MessageLine extends CLabel { 56 57 private Color fNormalMsgAreaBackground; 58 59 64 public MessageLine(Composite parent) { 65 this(parent, SWT.LEFT); 66 } 67 68 75 public MessageLine(Composite parent, int style) { 76 super(parent, style); 77 fNormalMsgAreaBackground = getBackground(); 78 } 79 80 86 private Image findImage(IStatus status) { 87 if (status.isOK()) { 88 return null; 89 } else if (status.matches(IStatus.ERROR)) { 90 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR); 91 } else if (status.matches(IStatus.WARNING)) { 92 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING); 93 } else if (status.matches(IStatus.INFO)) { 94 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO); 95 } 96 return null; 97 } 98 99 106 public void setErrorStatus(IStatus status) { 107 if (status != null && !status.isOK()) { 108 String message = status.getMessage(); 109 if (message != null && message.length() > 0) { 110 setText(message); 111 MessageLine.this.setImage(findImage(status)); 115 setBackground(JFaceColors.getErrorBackground(getDisplay())); 116 return; 117 } 118 } 119 setText(""); MessageLine.this.setImage(null); 124 setBackground(fNormalMsgAreaBackground); 125 } 126 } 127 128 134 public StatusDialog(Shell parent) { 135 super(parent); 136 fLastStatus = new Status(IStatus.OK, Policy.JFACE, IStatus.OK, 137 Util.ZERO_LENGTH_STRING, null); 138 } 139 140 148 public void setStatusLineAboveButtons(boolean aboveButtons) { 149 fStatusLineAboveButtons = aboveButtons; 150 } 151 152 159 protected void updateStatus(IStatus status) { 160 fLastStatus = status; 161 if (fStatusLine != null && !fStatusLine.isDisposed()) { 162 updateButtonsEnableState(status); 163 fStatusLine.setErrorStatus(status); 164 } 165 } 166 167 172 public IStatus getStatus() { 173 return fLastStatus; 174 } 175 176 183 protected void updateButtonsEnableState(IStatus status) { 184 if (fOkButton != null && !fOkButton.isDisposed()) { 185 fOkButton.setEnabled(!status.matches(IStatus.ERROR)); 186 } 187 } 188 189 192 protected void configureShell(Shell shell) { 193 super.configureShell(shell); 194 if (fTitle != null) { 195 shell.setText(fTitle); 196 } 197 } 198 199 202 public void create() { 203 super.create(); 204 if (fLastStatus != null) { 205 if (fLastStatus.matches(IStatus.ERROR)) { 207 fLastStatus = new Status(IStatus.ERROR, 209 fLastStatus.getPlugin(), fLastStatus.getCode(), 210 "", fLastStatus.getException()); } 212 updateStatus(fLastStatus); 213 } 214 } 215 216 219 protected void createButtonsForButtonBar(Composite parent) { 220 fOkButton = createButton(parent, IDialogConstants.OK_ID, 221 IDialogConstants.OK_LABEL, true); 222 createButton(parent, IDialogConstants.CANCEL_ID, 223 IDialogConstants.CANCEL_LABEL, false); 224 } 225 226 229 protected Control createButtonBar(Composite parent) { 230 Composite composite = new Composite(parent, SWT.NULL); 231 GridLayout layout = new GridLayout(); 232 233 if (fStatusLineAboveButtons) { 234 layout.numColumns = 1; 235 } else { 236 layout.numColumns = 2; 237 } 238 239 layout.marginHeight = 0; 240 layout.marginLeft = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 241 layout.marginWidth = 0; 242 composite.setLayout(layout); 243 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 244 245 if (!fStatusLineAboveButtons && isHelpAvailable()) { 246 createHelpControl(composite); 247 } 248 fStatusLine = new MessageLine(composite); 249 fStatusLine.setAlignment(SWT.LEFT); 250 GridData statusData = new GridData(GridData.FILL_HORIZONTAL); 251 fStatusLine.setErrorStatus(null); 252 if (fStatusLineAboveButtons && isHelpAvailable()) { 253 statusData.horizontalSpan = 2; 254 createHelpControl(composite); 255 } 256 fStatusLine.setLayoutData(statusData); 257 applyDialogFont(composite); 258 259 263 boolean helpAvailable = isHelpAvailable(); 264 setHelpAvailable(false); 265 super.createButtonBar(composite); 266 setHelpAvailable(helpAvailable); 267 return composite; 268 } 269 270 276 public void setTitle(String title) { 277 fTitle = title != null ? title : ""; Shell shell = getShell(); 279 if ((shell != null) && !shell.isDisposed()) { 280 shell.setText(fTitle); 281 } 282 } 283 284 290 public void setImage(Image image) { 291 fImage = image; 292 Shell shell = getShell(); 293 if ((shell != null) && !shell.isDisposed()) { 294 shell.setImage(fImage); 295 } 296 } 297 298 } 299 | Popular Tags |