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
18 * an error, warning or ok. The OK button is enabled or disabled depending
21 public abstract class StatusDialog extends Dialog {
23 private Button fOkButton;
24 private MessageLine fStatusLine;
25 private IStatus fLastStatus;
26 private String fTitle;
29 private boolean fStatusLineAboveButtons;
32 * Creates an instane of a status dialog.
34 public StatusDialog(Shell parent) {
36 fStatusLineAboveButtons= false;
40 * Specifies whether status line appears to the left of the buttons (default)
43 * @param aboveButtons if <code>true</code> status line is placed above buttons; if
44 * <code>false</code> to the right
46 public void setStatusLineAboveButtons(boolean aboveButtons) {
47 fStatusLineAboveButtons= aboveButtons;
51 * Update the dialog's status line to reflect the given status.
52 * It is save to call this method before the dialog has been opened.
54 protected void updateStatus(IStatus status) {
56 if (fStatusLine != null && !fStatusLine.isDisposed()) {
57 updateButtonsEnableState(status);
58 fStatusLine.setErrorStatus(status);
63 * Returns the last status.
65 public IStatus getStatus() {
70 * Updates the status of the ok button to reflect the given status.
71 * Subclasses may override this method to update additional buttons.
72 * @param status the status.
74 protected void updateButtonsEnableState(IStatus status) {
75 if (fOkButton != null && !fOkButton.isDisposed())
76 fOkButton.setEnabled(!status.matches(IStatus.ERROR));
80 * @see Window#create(Shell)
82 protected void configureShell(Shell shell) {
83 super.configureShell(shell);
85 shell.setText(fTitle);
89 * @see Window#create()
91 public void create() {
93 if (fLastStatus != null) {
94 // policy: dialogs are not allowed to come up with an error message
95 if (fLastStatus.matches(IStatus.ERROR)) {
96 StatusInfo status= new StatusInfo();
97 status.setError(""); //$NON-NLS-1$
100 updateStatus(fLastStatus);
105 * @see Dialog#createButtonsForButtonBar(Composite)
107 protected void createButtonsForButtonBar(Composite parent) {
108 fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
109 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
113 * @see Dialog#createButtonBar(Composite)
115 protected Control createButtonBar(Composite parent) {
116 Composite composite= new Composite(parent, SWT.NULL);
117 GridLayout layout= new GridLayout();
118 layout.numColumns= 1;
119 layout.marginHeight= 0;
120 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
121 composite.setLayout(layout);
122 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124 fStatusLine= new MessageLine(composite);
125 fStatusLine.setAlignment(SWT.LEFT);
126 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
127 fStatusLine.setErrorStatus(null); //$NON-NLS-1$
129 super.createButtonBar(composite);
134 * Sets the title for this dialog.
135 * @param title the title.
137 public void setTitle(String title) {
138 fTitle= title != null ? title : ""; //$NON-NLS-1$
139 Shell shell= getShell();
140 if ((shell != null) && !shell.isDisposed())
141 shell.setText(fTitle);
145 * Sets the image for this dialog.
146 * @param image the image.
148 public void setImage(Image image) {
150 Shell shell= getShell();
151 if ((shell != null) && !shell.isDisposed())
152 shell.setImage(fImage);