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