1) Reintroduced some out commented methods. (Did break some preference settings)
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / ui / StatusDialog.java
1 package net.sourceforge.phpdt.externaltools.internal.ui;
2
3 /**********************************************************************
4  Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
5  This file is made available under the terms of the Common Public License v1.0
6  which accompanies this distribution, and is available at
7  http://www.eclipse.org/legal/cpl-v10.html
8  **********************************************************************/
9
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.jface.dialogs.Dialog;
12 import org.eclipse.jface.dialogs.IDialogConstants;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21
22 /**
23  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
24  * The status message must be passed over as StatusInfo object and can be an
25  * error, warning or ok. The OK button is enabled or disabled depending on the
26  * status.
27  */
28 public abstract class StatusDialog extends Dialog {
29
30         private Button fOkButton;
31
32         private MessageLine fStatusLine;
33
34         private IStatus fLastStatus;
35
36         private String fTitle;
37
38         private Image fImage;
39
40         //private boolean fStatusLineAboveButtons;
41
42         /**
43          * Creates an instane of a status dialog.
44          */
45         public StatusDialog(Shell parent) {
46                 super(parent);
47                 //fStatusLineAboveButtons = false;
48                 fLastStatus = new StatusInfo();
49         }
50
51         /**
52          * Specifies whether status line appears to the left of the buttons
53          * (default) or above them.
54          * 
55          * @param aboveButtons
56          *            if <code>true</code> status line is placed above buttons; if
57          *            <code>false</code> to the right
58          */
59 //      public void setStatusLineAboveButtons(boolean aboveButtons) {
60 //              //fStatusLineAboveButtons = aboveButtons;
61 //      }
62
63         /**
64          * Update the dialog's status line to reflect the given status. It is save
65          * to call this method before the dialog has been opened.
66          */
67         protected void updateStatus(IStatus status) {
68                 fLastStatus = status;
69                 if (fStatusLine != null && !fStatusLine.isDisposed()) {
70                         updateButtonsEnableState(status);
71                         fStatusLine.setErrorStatus(status);
72                 }
73         }
74
75         /**
76          * Returns the last status.
77          */
78         public IStatus getStatus() {
79                 return fLastStatus;
80         }
81
82         /**
83          * Updates the status of the ok button to reflect the given status.
84          * Subclasses may override this method to update additional buttons.
85          * 
86          * @param status
87          *            the status.
88          */
89         protected void updateButtonsEnableState(IStatus status) {
90                 if (fOkButton != null && !fOkButton.isDisposed())
91                         fOkButton.setEnabled(!status.matches(IStatus.ERROR));
92         }
93
94         /*
95          * @see Window#create(Shell)
96          */
97         protected void configureShell(Shell shell) {
98                 super.configureShell(shell);
99                 if (fTitle != null)
100                         shell.setText(fTitle);
101         }
102
103         /*
104          * @see Window#create()
105          */
106         public void create() {
107                 super.create();
108                 if (fLastStatus != null) {
109                         // policy: dialogs are not allowed to come up with an error message
110                         if (fLastStatus.matches(IStatus.ERROR)) {
111                                 StatusInfo status = new StatusInfo();
112                                 status.setError(""); //$NON-NLS-1$
113                                 fLastStatus = status;
114                         }
115                         updateStatus(fLastStatus);
116                 }
117         }
118
119         /*
120          * @see Dialog#createButtonsForButtonBar(Composite)
121          */
122         protected void createButtonsForButtonBar(Composite parent) {
123                 fOkButton = createButton(parent, IDialogConstants.OK_ID,
124                                 IDialogConstants.OK_LABEL, true);
125                 createButton(parent, IDialogConstants.CANCEL_ID,
126                                 IDialogConstants.CANCEL_LABEL, false);
127         }
128
129         /*
130          * @see Dialog#createButtonBar(Composite)
131          */
132         protected Control createButtonBar(Composite parent) {
133                 Composite composite = new Composite(parent, SWT.NULL);
134                 GridLayout layout = new GridLayout();
135                 layout.numColumns = 1;
136                 layout.marginHeight = 0;
137                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
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); //$NON-NLS-1$
145
146                 super.createButtonBar(composite);
147                 return composite;
148         }
149
150         /**
151          * Sets the title for this dialog.
152          * 
153          * @param title
154          *            the title.
155          */
156         public void setTitle(String title) {
157                 fTitle = title != null ? title : ""; //$NON-NLS-1$
158                 Shell shell = getShell();
159                 if ((shell != null) && !shell.isDisposed())
160                         shell.setText(fTitle);
161         }
162
163         /**
164          * Sets the image for this dialog.
165          * 
166          * @param image
167          *            the image.
168          */
169         public void setImage(Image image) {
170                 fImage = image;
171                 Shell shell = getShell();
172                 if ((shell != null) && !shell.isDisposed())
173                         shell.setImage(fImage);
174         }
175
176 }