A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dialogs / SelectionStatusDialog.java
1 package net.sourceforge.phpdt.internal.ui.dialogs;
2
3 import java.util.Arrays;
4
5 import org.eclipse.core.runtime.IStatus;
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 import org.eclipse.ui.dialogs.SelectionDialog;
15
16 /**
17  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
18  * The status message must be passed over as StatusInfo object and can be an
19  * error, warning or ok. The OK button is enabled or disabled depending on the
20  * status.
21  */
22 public abstract class SelectionStatusDialog extends SelectionDialog {
23
24         private MessageLine fStatusLine;
25
26         private IStatus fLastStatus;
27
28         private Image fImage;
29
30         private boolean fStatusLineAboveButtons = false;
31
32         /**
33          * Creates an instance of a <code>SelectionStatusDialog</code>.
34          */
35         public SelectionStatusDialog(Shell parent) {
36                 super(parent);
37         }
38
39         /**
40          * Controls whether status line appears to the left of the buttons (default)
41          * or above them.
42          * 
43          * @param aboveButtons
44          *            if <code>true</code> status line is placed above buttons; if
45          *            <code>false</code> to the right
46          */
47         public void setStatusLineAboveButtons(boolean aboveButtons) {
48                 fStatusLineAboveButtons = aboveButtons;
49         }
50
51         /**
52          * Sets the image for this dialog.
53          * 
54          * @param image
55          *            the image.
56          */
57         public void setImage(Image image) {
58                 fImage = image;
59         }
60
61         /**
62          * Returns the first element from the list of results. Returns
63          * <code>null</code> if no element has been selected.
64          * 
65          * @return the first result element if one exists. Otherwise
66          *         <code>null</code> is returned.
67          */
68         public Object getFirstResult() {
69                 Object[] result = getResult();
70                 if (result == null || result.length == 0)
71                         return null;
72                 return result[0];
73         }
74
75         /**
76          * Sets a result element at the given position.
77          */
78         protected void setResult(int position, Object element) {
79                 Object[] result = getResult();
80                 result[position] = element;
81                 setResult(Arrays.asList(result));
82         }
83
84         /**
85          * Compute the result and return it.
86          */
87         protected abstract void computeResult();
88
89         protected void configureShell(Shell shell) {
90                 super.configureShell(shell);
91                 if (fImage != null)
92                         shell.setImage(fImage);
93         }
94
95         /**
96          * Update the dialog's status line to reflect the given status. It is safe
97          * to call this method before the dialog has been opened.
98          */
99         protected void updateStatus(IStatus status) {
100                 fLastStatus = status;
101                 if (fStatusLine != null && !fStatusLine.isDisposed()) {
102                         updateButtonsEnableState(status);
103                         fStatusLine.setErrorStatus(status);
104                 }
105         }
106
107         /**
108          * Update the status of the ok button to reflect the given status.
109          * Subclasses may override this method to update additional buttons.
110          */
111         protected void updateButtonsEnableState(IStatus status) {
112                 Button okButton = getOkButton();
113                 if (okButton != null && !okButton.isDisposed())
114                         okButton.setEnabled(!status.matches(IStatus.ERROR));
115         }
116
117         protected void okPressed() {
118                 computeResult();
119                 super.okPressed();
120         }
121
122         public void create() {
123                 super.create();
124                 if (fLastStatus != null)
125                         updateStatus(fLastStatus);
126         }
127
128         protected Control createButtonBar(Composite parent) {
129                 Composite composite = new Composite(parent, SWT.NULL);
130                 GridLayout layout = new GridLayout();
131                 if (fStatusLineAboveButtons) {
132                         layout.marginWidth = 5;
133                 } else {
134                         layout.numColumns = 2;
135                 }
136                 layout.marginHeight = 0;
137                 layout.marginWidth = 0;
138                 composite.setLayout(layout);
139                 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
140
141                 fStatusLine = new MessageLine(composite);
142                 fStatusLine.setAlignment(SWT.LEFT);
143                 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
144                 fStatusLine.setErrorStatus(null);
145
146                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
147                 gd.horizontalIndent = convertWidthInCharsToPixels(1);
148                 fStatusLine.setLayoutData(gd);
149
150                 super.createButtonBar(composite);
151                 return composite;
152         }
153
154 }