KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > dialogs > StatusDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.dialogs;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.resource.JFaceColors;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.util.Policy;
18 import org.eclipse.jface.util.Util;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.CLabel;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Shell;
29
30 /**
31  * An abstract base class for dialogs with a status bar and OK/CANCEL buttons.
32  * The status message is specified in an IStatus which can be of severity ERROR,
33  * WARNING, INFO or OK. The OK button is enabled or disabled depending on the
34  * status.
35  *
36  * @since 3.1
37  */

38 public abstract class StatusDialog extends TrayDialog {
39
40     private Button fOkButton;
41
42     private MessageLine fStatusLine;
43
44     private IStatus fLastStatus;
45
46     private String JavaDoc fTitle;
47
48     private Image fImage;
49
50     private boolean fStatusLineAboveButtons = true;
51
52     /**
53      * A message line displaying a status.
54      */

55     private class MessageLine extends CLabel {
56
57         private Color fNormalMsgAreaBackground;
58
59         /**
60          * Creates a new message line as a child of the given parent.
61          *
62          * @param parent
63          */

64         public MessageLine(Composite parent) {
65             this(parent, SWT.LEFT);
66         }
67
68         /**
69          * Creates a new message line as a child of the parent and with the
70          * given SWT stylebits.
71          *
72          * @param parent
73          * @param style
74          */

75         public MessageLine(Composite parent, int style) {
76             super(parent, style);
77             fNormalMsgAreaBackground = getBackground();
78         }
79
80         /**
81          * Find an image assocated with the status.
82          *
83          * @param status
84          * @return Image
85          */

86         private Image findImage(IStatus status) {
87             if (status.isOK()) {
88                 return null;
89             } else if (status.matches(IStatus.ERROR)) {
90                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
91             } else if (status.matches(IStatus.WARNING)) {
92                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
93             } else if (status.matches(IStatus.INFO)) {
94                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
95             }
96             return null;
97         }
98
99         /**
100          * Sets the message and image to the given status.
101          *
102          * @param status
103          * IStatus or <code>null</code>. <code>null</code> will
104          * set the empty text and no image.
105          */

106         public void setErrorStatus(IStatus status) {
107             if (status != null && !status.isOK()) {
108                 String JavaDoc message = status.getMessage();
109                 if (message != null && message.length() > 0) {
110                     setText(message);
111                     // unqualified call of setImage is too ambiguous for
112
// Foundation 1.0 compiler
113
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=140576
114
MessageLine.this.setImage(findImage(status));
115                     setBackground(JFaceColors.getErrorBackground(getDisplay()));
116                     return;
117                 }
118             }
119             setText(""); //$NON-NLS-1$
120
// unqualified call of setImage is too ambiguous for Foundation 1.0
121
// compiler
122
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=140576
123
MessageLine.this.setImage(null);
124             setBackground(fNormalMsgAreaBackground);
125         }
126     }
127
128     /**
129      * Creates an instance of a status dialog.
130      *
131      * @param parent
132      * the parent Shell of the dialog
133      */

134     public StatusDialog(Shell parent) {
135         super(parent);
136         fLastStatus = new Status(IStatus.OK, Policy.JFACE, IStatus.OK,
137                 Util.ZERO_LENGTH_STRING, null);
138     }
139
140     /**
141      * Specifies whether status line appears to the left of the buttons
142      * (default) or above them.
143      *
144      * @param aboveButtons
145      * if <code>true</code> status line is placed above buttons; if
146      * <code>false</code> to the right
147      */

148     public void setStatusLineAboveButtons(boolean aboveButtons) {
149         fStatusLineAboveButtons = aboveButtons;
150     }
151
152     /**
153      * Update the dialog's status line to reflect the given status. It is safe
154      * to call this method before the dialog has been opened.
155      *
156      * @param status
157      * the status to set
158      */

159     protected void updateStatus(IStatus status) {
160         fLastStatus = status;
161         if (fStatusLine != null && !fStatusLine.isDisposed()) {
162             updateButtonsEnableState(status);
163             fStatusLine.setErrorStatus(status);
164         }
165     }
166
167     /**
168      * Returns the last status.
169      *
170      * @return IStatus
171      */

172     public IStatus getStatus() {
173         return fLastStatus;
174     }
175
176     /**
177      * Updates the status of the ok button to reflect the given status.
178      * Subclasses may override this method to update additional buttons.
179      *
180      * @param status
181      * the status.
182      */

183     protected void updateButtonsEnableState(IStatus status) {
184         if (fOkButton != null && !fOkButton.isDisposed()) {
185             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
186         }
187     }
188
189     /*
190      * @see Window#create(Shell)
191      */

192     protected void configureShell(Shell shell) {
193         super.configureShell(shell);
194         if (fTitle != null) {
195             shell.setText(fTitle);
196         }
197     }
198
199     /*
200      * @see Window#create()
201      */

202     public void create() {
203         super.create();
204         if (fLastStatus != null) {
205             // policy: dialogs are not allowed to come up with an error message
206
if (fLastStatus.matches(IStatus.ERROR)) {
207                 // remove the message
208
fLastStatus = new Status(IStatus.ERROR,
209                         fLastStatus.getPlugin(), fLastStatus.getCode(),
210                         "", fLastStatus.getException()); //$NON-NLS-1$
211
}
212             updateStatus(fLastStatus);
213         }
214     }
215
216     /*
217      * @see Dialog#createButtonsForButtonBar(Composite)
218      */

219     protected void createButtonsForButtonBar(Composite parent) {
220         fOkButton = createButton(parent, IDialogConstants.OK_ID,
221                 IDialogConstants.OK_LABEL, true);
222         createButton(parent, IDialogConstants.CANCEL_ID,
223                 IDialogConstants.CANCEL_LABEL, false);
224     }
225
226     /*
227      * @see Dialog#createButtonBar(Composite)
228      */

229     protected Control createButtonBar(Composite parent) {
230         Composite composite = new Composite(parent, SWT.NULL);
231         GridLayout layout = new GridLayout();
232
233         if (fStatusLineAboveButtons) {
234             layout.numColumns = 1;
235         } else {
236             layout.numColumns = 2;
237         }
238
239         layout.marginHeight = 0;
240         layout.marginLeft = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
241         layout.marginWidth = 0;
242         composite.setLayout(layout);
243         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
244
245         if (!fStatusLineAboveButtons && isHelpAvailable()) {
246             createHelpControl(composite);
247         }
248         fStatusLine = new MessageLine(composite);
249         fStatusLine.setAlignment(SWT.LEFT);
250         GridData statusData = new GridData(GridData.FILL_HORIZONTAL);
251         fStatusLine.setErrorStatus(null);
252         if (fStatusLineAboveButtons && isHelpAvailable()) {
253             statusData.horizontalSpan = 2;
254             createHelpControl(composite);
255         }
256         fStatusLine.setLayoutData(statusData);
257         applyDialogFont(composite);
258
259         /*
260          * Create the rest of the button bar, but tell it not to create a help
261          * button (we've already created it).
262          */

263         boolean helpAvailable = isHelpAvailable();
264         setHelpAvailable(false);
265         super.createButtonBar(composite);
266         setHelpAvailable(helpAvailable);
267         return composite;
268     }
269
270     /**
271      * Sets the title for this dialog.
272      *
273      * @param title
274      * the title.
275      */

276     public void setTitle(String JavaDoc title) {
277         fTitle = title != null ? title : ""; //$NON-NLS-1$
278
Shell shell = getShell();
279         if ((shell != null) && !shell.isDisposed()) {
280             shell.setText(fTitle);
281         }
282     }
283
284     /**
285      * Sets the image for this dialog.
286      *
287      * @param image
288      * the image.
289      */

290     public void setImage(Image image) {
291         fImage = image;
292         Shell shell = getShell();
293         if ((shell != null) && !shell.isDisposed()) {
294             shell.setImage(fImage);
295         }
296     }
297
298 }
299
Popular Tags