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