1 package net.sourceforge.phpdt.internal.ui.dialogs;
3 import org.eclipse.core.runtime.IStatus;
4 import org.eclipse.jface.dialogs.Dialog;
5 import org.eclipse.jface.dialogs.IDialogConstants;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.Image;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Shell;
16 * An abstract base class for dialogs with a status bar and ok/cancel buttons.
17 * The status message must be passed over as StatusInfo object and can be an
18 * error, warning or ok. The OK button is enabled or disabled depending on the
21 public abstract class StatusDialog extends Dialog {
23 private Button fOkButton;
25 private MessageLine fStatusLine;
27 private IStatus fLastStatus;
29 private String fTitle;
33 //private boolean fStatusLineAboveButtons;
36 * Creates an instane of a status dialog.
38 public StatusDialog(Shell parent) {
40 //fStatusLineAboveButtons = false;
44 * Specifies whether status line appears to the left of the buttons
45 * (default) or above them.
48 * if <code>true</code> status line is placed above buttons; if
49 * <code>false</code> to the right
51 public void setStatusLineAboveButtons(boolean aboveButtons) {
52 //fStatusLineAboveButtons = aboveButtons;
56 * Update the dialog's status line to reflect the given status. It is save
57 * to call this method before the dialog has been opened.
59 protected void updateStatus(IStatus status) {
61 if (fStatusLine != null && !fStatusLine.isDisposed()) {
62 updateButtonsEnableState(status);
63 fStatusLine.setErrorStatus(status);
68 * Returns the last status.
70 public IStatus getStatus() {
75 * Updates the status of the ok button to reflect the given status.
76 * Subclasses may override this method to update additional buttons.
81 protected void updateButtonsEnableState(IStatus status) {
82 if (fOkButton != null && !fOkButton.isDisposed())
83 fOkButton.setEnabled(!status.matches(IStatus.ERROR));
87 * @see Window#create(Shell)
89 protected void configureShell(Shell shell) {
90 super.configureShell(shell);
92 shell.setText(fTitle);
96 * @see Window#create()
98 public void create() {
100 if (fLastStatus != null) {
101 // policy: dialogs are not allowed to come up with an error message
102 if (fLastStatus.matches(IStatus.ERROR)) {
103 StatusInfo status = new StatusInfo();
104 status.setError(""); //$NON-NLS-1$
105 fLastStatus = status;
107 updateStatus(fLastStatus);
112 * @see Dialog#createButtonsForButtonBar(Composite)
114 protected void createButtonsForButtonBar(Composite parent) {
115 fOkButton = createButton(parent, IDialogConstants.OK_ID,
116 IDialogConstants.OK_LABEL, true);
117 createButton(parent, IDialogConstants.CANCEL_ID,
118 IDialogConstants.CANCEL_LABEL, false);
122 * @see Dialog#createButtonBar(Composite)
124 protected Control createButtonBar(Composite parent) {
125 Composite composite = new Composite(parent, SWT.NULL);
126 GridLayout layout = new GridLayout();
127 layout.numColumns = 1;
128 layout.marginHeight = 0;
129 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
130 composite.setLayout(layout);
131 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133 fStatusLine = new MessageLine(composite);
134 fStatusLine.setAlignment(SWT.LEFT);
135 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
136 fStatusLine.setErrorStatus(null); //$NON-NLS-1$
138 super.createButtonBar(composite);
143 * Sets the title for this dialog.
148 public void setTitle(String title) {
149 fTitle = title != null ? title : ""; //$NON-NLS-1$
150 Shell shell = getShell();
151 if ((shell != null) && !shell.isDisposed())
152 shell.setText(fTitle);
156 * Sets the image for this dialog.
161 public void setImage(Image image) {
163 Shell shell = getShell();
164 if ((shell != null) && !shell.isDisposed())
165 shell.setImage(fImage);