KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > templates > 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.ui.texteditor.templates;
12
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 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24
25 import org.eclipse.core.runtime.IStatus;
26
27 /**
28  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
29  * The status message must be passed over as StatusInfo object and can be
30  * an error, warning or ok. The OK button is enabled or disabled depending
31  * on the status.
32  *
33  * @since 3.0
34  */

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

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

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

71     protected void updateStatus(IStatus status) {
72         fLastStatus= status;
73         if (fStatusLine != null && !fStatusLine.isDisposed()) {
74             updateButtonsEnableState(status);
75             fStatusLine.setErrorStatus(status);
76         }
77     }
78     
79     /**
80      * Returns the last status.
81      *
82      * @return the last status
83      */

84     public IStatus getStatus() {
85         return fLastStatus;
86     }
87
88     /**
89      * Updates the status of the ok button to reflect the given status.
90      * Subclasses may override this method to update additional buttons.
91      * @param status the status.
92      */

93     protected void updateButtonsEnableState(IStatus status) {
94         if (fOkButton != null && !fOkButton.isDisposed())
95             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
96     }
97     
98     /*
99      * @see Window#create(Shell)
100      */

101     protected void configureShell(Shell shell) {
102         super.configureShell(shell);
103         if (fTitle != null)
104             shell.setText(fTitle);
105     }
106
107     /*
108      * @see Window#create()
109      */

110     public void create() {
111         super.create();
112         if (fLastStatus != null) {
113             // policy: dialogs are not allowed to come up with an error message
114
if (fLastStatus.matches(IStatus.ERROR)) {
115                 StatusInfo status= new StatusInfo();
116                 status.setError(""); //$NON-NLS-1$
117
fLastStatus= status;
118             }
119             updateStatus(fLastStatus);
120         }
121     }
122
123     /*
124      * @see Dialog#createButtonsForButtonBar(Composite)
125      */

126     protected void createButtonsForButtonBar(Composite parent) {
127         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
128         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
129     }
130     
131     /*
132      * @see Dialog#createButtonBar(Composite)
133      */

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

162     public void setTitle(String JavaDoc title) {
163         fTitle= title != null ? title : ""; //$NON-NLS-1$
164
Shell shell= getShell();
165         if ((shell != null) && !shell.isDisposed())
166             shell.setText(fTitle);
167     }
168
169     /**
170      * Sets the image for this dialog.
171      * @param image the image.
172      */

173     public void setImage(Image image) {
174         fImage= image;
175         Shell shell= getShell();
176         if ((shell != null) && !shell.isDisposed())
177             shell.setImage(fImage);
178     }
179     
180 }
181
Popular Tags