KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > StatusDialog


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

11 package org.eclipse.ant.internal.ui.preferences;
12
13
14 import org.eclipse.ant.internal.ui.model.IAntUIHelpContextIds;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.help.WorkbenchHelp;
27
28 /**
29  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
30  * The status message must be passed over as StatusInfo object and can be
31  * an error, warning or ok. The OK button is enabled or disabled depending
32  * on the status.
33  */

34 public abstract class StatusDialog extends Dialog {
35
36     private Button fOkButton;
37     private MessageLine fStatusLine;
38     private IStatus fLastStatus;
39     private String JavaDoc fTitle;
40     private Image fImage;
41
42     /**
43      * Creates an instane of a status dialog.
44      */

45     public StatusDialog(Shell parent) {
46         super(parent);
47         fLastStatus= new StatusInfo();
48     }
49
50     /**
51      * Update the dialog's status line to reflect the given status.
52      * It is save to call this method before the dialog has been opened.
53      */

54     protected void updateStatus(IStatus status) {
55         fLastStatus= status;
56         if (fStatusLine != null && !fStatusLine.isDisposed()) {
57             updateButtonsEnableState(status);
58             fStatusLine.setErrorStatus(status);
59         }
60     }
61
62     /**
63      * Returns the last status.
64      */

65     public IStatus getStatus() {
66         return fLastStatus;
67     }
68
69     /**
70      * Updates the status of the ok button to reflect the given status.
71      * Subclasses may override this method to update additional buttons.
72      * @param status the status.
73      */

74     protected void updateButtonsEnableState(IStatus status) {
75         if (fOkButton != null && !fOkButton.isDisposed())
76             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
81      */

82     protected void configureShell(Shell shell) {
83         super.configureShell(shell);
84         if (fTitle != null) {
85             shell.setText(fTitle);
86         }
87         WorkbenchHelp.setHelp(shell, IAntUIHelpContextIds.STATUS_DIALOG);
88     }
89
90     /*
91      * @see Window#create()
92      */

93     public void create() {
94         super.create();
95         if (fLastStatus != null) {
96             // policy: dialogs are not allowed to come up with an error message
97
if (fLastStatus.matches(IStatus.ERROR)) {
98                 StatusInfo status= new StatusInfo();
99                 status.setError(""); //$NON-NLS-1$
100
fLastStatus= status;
101             }
102             updateStatus(fLastStatus);
103         }
104     }
105
106     /*
107      * @see Dialog#createButtonsForButtonBar(Composite)
108      */

109     protected void createButtonsForButtonBar(Composite parent) {
110         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
111         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
112     }
113
114     /*
115      * @see Dialog#createButtonBar(Composite)
116      */

117     protected Control createButtonBar(Composite parent) {
118         Composite composite= new Composite(parent, SWT.NULL);
119         GridLayout layout= new GridLayout();
120         layout.numColumns= 1;
121         layout.marginHeight= 0;
122         layout.marginWidth= 0;
123         composite.setLayout(layout);
124         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125
126         fStatusLine= new MessageLine(composite);
127         fStatusLine.setAlignment(SWT.LEFT);
128         fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
129         fStatusLine.setErrorStatus(null); //$NON-NLS-1$
130

131         super.createButtonBar(composite);
132         return composite;
133     }
134
135     /**
136      * Sets the title for this dialog.
137      * @param title the title.
138      */

139     public void setTitle(String JavaDoc title) {
140         fTitle= title != null ? title : ""; //$NON-NLS-1$
141
Shell shell= getShell();
142         if ((shell != null) && !shell.isDisposed())
143             shell.setText(fTitle);
144     }
145
146     /**
147      * Sets the image for this dialog.
148      * @param image the image.
149      */

150     public void setImage(Image image) {
151         fImage= image;
152         Shell shell= getShell();
153         if ((shell != null) && !shell.isDisposed())
154             shell.setImage(fImage);
155     }
156
157 }
158
Popular Tags