misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / 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
18  * an error, warning or ok. The OK button is enabled or disabled depending
19  * on the status.
20  */ 
21 public abstract class StatusDialog extends Dialog  {
22         
23         private Button fOkButton;
24         private MessageLine fStatusLine;
25         private IStatus fLastStatus;
26         private String fTitle;
27         private Image fImage;
28         
29         private boolean fStatusLineAboveButtons;
30         
31         /**
32          * Creates an instane of a status dialog.
33          */
34         public StatusDialog(Shell parent) {
35                 super(parent);
36                 fStatusLineAboveButtons= false;
37         }
38         
39         /**
40          * Specifies whether status line appears to the left of the buttons (default)
41          * or above them.
42          *
43          * @param aboveButtons if <code>true</code> status line is placed above buttons; if
44          *      <code>false</code> to the right
45          */
46         public void setStatusLineAboveButtons(boolean aboveButtons) {
47                 fStatusLineAboveButtons= aboveButtons;
48         }       
49         
50         /**
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.
53          */
54         protected void updateStatus(IStatus status) {
55                 fLastStatus= status;
56                 if (fStatusLine != null && !fStatusLine.isDisposed()) {
57                         updateButtonsEnableState(status);
58                         fStatusLine.setErrorStatus(status);
59                 }
60         }
61         
62         /**
63          * Returns the last status.
64          */
65         public IStatus getStatus() {
66                 return fLastStatus;
67         }
68
69         /**
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.
73          */
74         protected void updateButtonsEnableState(IStatus status) {
75                 if (fOkButton != null && !fOkButton.isDisposed())
76                         fOkButton.setEnabled(!status.matches(IStatus.ERROR));
77         }
78         
79         /* 
80          * @see Window#create(Shell)
81          */
82         protected void configureShell(Shell shell) {
83                 super.configureShell(shell);
84                 if (fTitle != null)
85                         shell.setText(fTitle);
86         }
87
88         /*
89          * @see Window#create()
90          */     
91         public void create() {
92                 super.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$
98                                 fLastStatus= status;
99                         }
100                         updateStatus(fLastStatus);
101                 }
102         }
103
104         /*
105          * @see Dialog#createButtonsForButtonBar(Composite)
106          */
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);
110         }
111         
112         /*
113          * @see Dialog#createButtonBar(Composite)
114          */                             
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));
123                 
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$
128
129                 super.createButtonBar(composite);
130                 return composite;
131         }
132         
133         /**
134          * Sets the title for this dialog.
135          * @param title the title.
136          */
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);
142         }
143
144         /**
145          * Sets the image for this dialog.
146          * @param image the image.
147          */
148         public void setImage(Image image) {
149                 fImage= image;
150                 Shell shell= getShell();
151                 if ((shell != null) && !shell.isDisposed())
152                         shell.setImage(fImage);
153         }       
154         
155 }