KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > StatusDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.jdt.internal.ui.dialogs;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.dialogs.IDialogConstants;
27
28 import org.eclipse.ui.internal.MessageLine;
29
30 /**
31  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
32  * The status message must be passed over as StatusInfo object and can be
33  * an error, warning, info or ok. The OK button is enabled or disabled depending
34  * on the status.
35  */

36 public abstract class StatusDialog extends Dialog {
37     
38     private Button fOkButton;
39     private MessageLine fStatusLine;
40     private IStatus fLastStatus;
41     private String JavaDoc fTitle;
42     private Image fImage;
43     private boolean fStatusLineAboveButtons= true;
44     
45     /**
46      * Creates an instance of a status dialog.
47      */

48     public StatusDialog(Shell parent) {
49         super(parent);
50         fLastStatus= new StatusInfo();
51     }
52     
53     /**
54      * Specifies whether status line appears to the left of the buttons (default)
55      * or above them.
56      *
57      * @param aboveButtons if <code>true</code> status line is placed above buttons; if
58      * <code>false</code> to the right
59      */

60     public void setStatusLineAboveButtons(boolean aboveButtons) {
61         fStatusLineAboveButtons= aboveButtons;
62     }
63     
64     /**
65      * Update the dialog's status line to reflect the given status.
66      * It is save to call this method before the dialog has been opened.
67      */

68     protected void updateStatus(IStatus status) {
69         fLastStatus= status;
70         if (fStatusLine != null && !fStatusLine.isDisposed()) {
71             updateButtonsEnableState(status);
72             fStatusLine.setErrorStatus(status);
73         }
74     }
75     
76     /**
77      * Returns the last status.
78      */

79     public IStatus getStatus() {
80         return fLastStatus;
81     }
82
83     /**
84      * Updates the status of the ok button to reflect the given status.
85      * Subclasses may override this method to update additional buttons.
86      * @param status the status.
87      */

88     protected void updateButtonsEnableState(IStatus status) {
89         if (fOkButton != null && !fOkButton.isDisposed())
90             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
91     }
92     
93     /*
94      * @see Window#create(Shell)
95      */

96     protected void configureShell(Shell shell) {
97         super.configureShell(shell);
98         if (fTitle != null)
99             shell.setText(fTitle);
100     }
101
102     /*
103      * @see Window#create()
104      */

105     public void create() {
106         super.create();
107         if (fLastStatus != null) {
108             // policy: dialogs are not allowed to come up with an error message
109
if (fLastStatus.matches(IStatus.ERROR)) {
110                 // remove the message
111
fLastStatus= new Status(IStatus.ERROR, fLastStatus.getPlugin(), fLastStatus.getCode(), "", fLastStatus.getException()); //$NON-NLS-1$
112
}
113             updateStatus(fLastStatus);
114         }
115     }
116
117     /*
118      * @see Dialog#createButtonsForButtonBar(Composite)
119      */

120     protected void createButtonsForButtonBar(Composite parent) {
121         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
122         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
123     }
124     
125     /*
126      * @see Dialog#createButtonBar(Composite)
127      */

128     protected Control createButtonBar(Composite parent) {
129         Composite composite= new Composite(parent, SWT.NULL);
130         GridLayout layout= new GridLayout();
131     
132         if (fStatusLineAboveButtons) {
133             layout.numColumns= 1;
134         } else {
135             layout.numColumns= 2;
136         }
137         
138         layout.marginHeight= 0;
139         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
140         composite.setLayout(layout);
141         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
142         
143         fStatusLine= new MessageLine(composite);
144         fStatusLine.setAlignment(SWT.LEFT);
145         fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
146         fStatusLine.setErrorStatus(null);
147         applyDialogFont(composite);
148         super.createButtonBar(composite);
149         return composite;
150     }
151     
152     /**
153      * Sets the title for this dialog.
154      * @param title the title.
155      */

156     public void setTitle(String JavaDoc 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      * @param image the image.
166      */

167     public void setImage(Image image) {
168         fImage= image;
169         Shell shell= getShell();
170         if ((shell != null) && !shell.isDisposed())
171             shell.setImage(fImage);
172     }
173     
174 }
175
Popular Tags