refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / dialogs / StatusDialog.java
1 package net.sourceforge.phpdt.internal.ui.dialogs;
2
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;
14
15 /**
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
19  * status.
20  */
21 public abstract class StatusDialog extends Dialog {
22
23         private Button fOkButton;
24
25         private MessageLine fStatusLine;
26
27         private IStatus fLastStatus;
28
29         private String fTitle;
30
31         private Image fImage;
32
33         private boolean fStatusLineAboveButtons;
34
35         /**
36          * Creates an instane of a status dialog.
37          */
38         public StatusDialog(Shell parent) {
39                 super(parent);
40                 fStatusLineAboveButtons = false;
41         }
42
43         /**
44          * Specifies whether status line appears to the left of the buttons
45          * (default) or above them.
46          * 
47          * @param aboveButtons
48          *            if <code>true</code> status line is placed above buttons; if
49          *            <code>false</code> to the right
50          */
51         public void setStatusLineAboveButtons(boolean aboveButtons) {
52                 fStatusLineAboveButtons = aboveButtons;
53         }
54
55         /**
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.
58          */
59         protected void updateStatus(IStatus status) {
60                 fLastStatus = status;
61                 if (fStatusLine != null && !fStatusLine.isDisposed()) {
62                         updateButtonsEnableState(status);
63                         fStatusLine.setErrorStatus(status);
64                 }
65         }
66
67         /**
68          * Returns the last status.
69          */
70         public IStatus getStatus() {
71                 return fLastStatus;
72         }
73
74         /**
75          * Updates the status of the ok button to reflect the given status.
76          * Subclasses may override this method to update additional buttons.
77          * 
78          * @param status
79          *            the status.
80          */
81         protected void updateButtonsEnableState(IStatus status) {
82                 if (fOkButton != null && !fOkButton.isDisposed())
83                         fOkButton.setEnabled(!status.matches(IStatus.ERROR));
84         }
85
86         /*
87          * @see Window#create(Shell)
88          */
89         protected void configureShell(Shell shell) {
90                 super.configureShell(shell);
91                 if (fTitle != null)
92                         shell.setText(fTitle);
93         }
94
95         /*
96          * @see Window#create()
97          */
98         public void create() {
99                 super.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;
106                         }
107                         updateStatus(fLastStatus);
108                 }
109         }
110
111         /*
112          * @see Dialog#createButtonsForButtonBar(Composite)
113          */
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);
119         }
120
121         /*
122          * @see Dialog#createButtonBar(Composite)
123          */
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));
132
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$
137
138                 super.createButtonBar(composite);
139                 return composite;
140         }
141
142         /**
143          * Sets the title for this dialog.
144          * 
145          * @param title
146          *            the title.
147          */
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);
153         }
154
155         /**
156          * Sets the image for this dialog.
157          * 
158          * @param image
159          *            the image.
160          */
161         public void setImage(Image image) {
162                 fImage = image;
163                 Shell shell = getShell();
164                 if ((shell != null) && !shell.isDisposed())
165                         shell.setImage(fImage);
166         }
167
168 }