Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / 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
25  * an error, warning or ok. The OK button is enabled or disabled depending
26  * on the status.
27  */
28 public abstract class StatusDialog extends Dialog {
29
30         private Button fOkButton;
31         private MessageLine fStatusLine;
32         private IStatus fLastStatus;
33         private String fTitle;
34         private Image fImage;
35
36         private boolean fStatusLineAboveButtons;
37
38         /**
39          * Creates an instane of a status dialog.
40          */
41         public StatusDialog(Shell parent) {
42                 super(parent);
43                 fStatusLineAboveButtons= false;
44                 fLastStatus= new StatusInfo();
45         }
46
47         /**
48          * Specifies whether status line appears to the left of the buttons (default)
49          * or above them.
50          *
51          * @param aboveButtons if <code>true</code> status line is placed above buttons; if
52          *      <code>false</code> to the right
53          */
54         public void setStatusLineAboveButtons(boolean aboveButtons) {
55                 fStatusLineAboveButtons= aboveButtons;
56         }
57
58         /**
59          * Update the dialog's status line to reflect the given status.
60          * It is save to call this method before the dialog has been opened.
61          */
62         protected void updateStatus(IStatus status) {
63                 fLastStatus= status;
64                 if (fStatusLine != null && !fStatusLine.isDisposed()) {
65                         updateButtonsEnableState(status);
66                         fStatusLine.setErrorStatus(status);
67                 }
68         }
69
70         /**
71          * Returns the last status.
72          */
73         public IStatus getStatus() {
74                 return fLastStatus;
75         }
76
77         /**
78          * Updates the status of the ok button to reflect the given status.
79          * Subclasses may override this method to update additional buttons.
80          * @param status the status.
81          */
82         protected void updateButtonsEnableState(IStatus status) {
83                 if (fOkButton != null && !fOkButton.isDisposed())
84                         fOkButton.setEnabled(!status.matches(IStatus.ERROR));
85         }
86
87         /*
88          * @see Window#create(Shell)
89          */
90         protected void configureShell(Shell shell) {
91                 super.configureShell(shell);
92                 if (fTitle != null)
93                         shell.setText(fTitle);
94         }
95
96         /*
97          * @see Window#create()
98          */
99         public void create() {
100                 super.create();
101                 if (fLastStatus != null) {
102                         // policy: dialogs are not allowed to come up with an error message
103                         if (fLastStatus.matches(IStatus.ERROR)) {
104                                 StatusInfo status= new StatusInfo();
105                                 status.setError(""); //$NON-NLS-1$
106                                 fLastStatus= status;
107                         }
108                         updateStatus(fLastStatus);
109                 }
110         }
111
112         /*
113          * @see Dialog#createButtonsForButtonBar(Composite)
114          */
115         protected void createButtonsForButtonBar(Composite parent) {
116                 fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
117                 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
118         }
119
120         /*
121          * @see Dialog#createButtonBar(Composite)
122          */
123         protected Control createButtonBar(Composite parent) {
124                 Composite composite= new Composite(parent, SWT.NULL);
125                 GridLayout layout= new GridLayout();
126                 layout.numColumns= 1;
127                 layout.marginHeight= 0;
128                 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
129                 composite.setLayout(layout);
130                 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131
132                 fStatusLine= new MessageLine(composite);
133                 fStatusLine.setAlignment(SWT.LEFT);
134                 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
135                 fStatusLine.setErrorStatus(null); //$NON-NLS-1$
136
137                 super.createButtonBar(composite);
138                 return composite;
139         }
140
141         /**
142          * Sets the title for this dialog.
143          * @param title the title.
144          */
145         public void setTitle(String title) {
146                 fTitle= title != null ? title : ""; //$NON-NLS-1$
147                 Shell shell= getShell();
148                 if ((shell != null) && !shell.isDisposed())
149                         shell.setText(fTitle);
150         }
151
152         /**
153          * Sets the image for this dialog.
154          * @param image the image.
155          */
156         public void setImage(Image image) {
157                 fImage= image;
158                 Shell shell= getShell();
159                 if ((shell != null) && !shell.isDisposed())
160                         shell.setImage(fImage);
161         }
162
163 }