KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > SelectionStatusDialog


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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
11  * font should be activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import java.util.Arrays JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Shell;
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 or ok. The OK button is enabled or disabled depending
34  * on the status.
35  *
36  * @since 2.0
37  */

38 public abstract class SelectionStatusDialog extends SelectionDialog {
39
40     private MessageLine fStatusLine;
41
42     private IStatus fLastStatus;
43
44     private Image fImage;
45
46     private boolean fStatusLineAboveButtons = false;
47
48     /**
49      * Creates an instance of a <code>SelectionStatusDialog</code>.
50      * @param parent
51      */

52     public SelectionStatusDialog(Shell parent) {
53         super(parent);
54     }
55
56     /**
57      * Controls whether status line appears to the left of the buttons (default)
58      * or above them.
59      *
60      * @param aboveButtons if <code>true</code> status line is placed above buttons; if
61      * <code>false</code> to the right
62      */

63     public void setStatusLineAboveButtons(boolean aboveButtons) {
64         fStatusLineAboveButtons = aboveButtons;
65     }
66
67     /**
68      * Sets the image for this dialog.
69      * @param image the image.
70      */

71     public void setImage(Image image) {
72         fImage = image;
73     }
74
75     /**
76      * Returns the first element from the list of results. Returns <code>null</code>
77      * if no element has been selected.
78      *
79      * @return the first result element if one exists. Otherwise <code>null</code> is
80      * returned.
81      */

82     public Object JavaDoc getFirstResult() {
83         Object JavaDoc[] result = getResult();
84         if (result == null || result.length == 0) {
85             return null;
86         }
87         return result[0];
88     }
89
90     /**
91      * Sets a result element at the given position.
92      * @param position
93      * @param element
94      */

95     protected void setResult(int position, Object JavaDoc element) {
96         Object JavaDoc[] result = getResult();
97         result[position] = element;
98         setResult(Arrays.asList(result));
99     }
100
101     /**
102      * Compute the result and return it.
103      */

104     protected abstract void computeResult();
105
106     /*
107      * @see Window#configureShell(shell)
108      */

109     protected void configureShell(Shell shell) {
110         super.configureShell(shell);
111         if (fImage != null) {
112             shell.setImage(fImage);
113         }
114     }
115
116     /**
117      * Update the dialog's status line to reflect the given status. It is safe to call
118      * this method before the dialog has been opened.
119      * @param status
120      */

121     protected void updateStatus(IStatus status) {
122         fLastStatus = status;
123         if (fStatusLine != null && !fStatusLine.isDisposed()) {
124             updateButtonsEnableState(status);
125             fStatusLine.setErrorStatus(status);
126         }
127     }
128
129     /**
130      * Update the status of the ok button to reflect the given status. Subclasses
131      * may override this method to update additional buttons.
132      * @param status
133      */

134     protected void updateButtonsEnableState(IStatus status) {
135         Button okButton = getOkButton();
136         if (okButton != null && !okButton.isDisposed()) {
137             okButton.setEnabled(!status.matches(IStatus.ERROR));
138         }
139     }
140
141     /*
142      * @see Dialog#okPressed()
143      */

144     protected void okPressed() {
145         computeResult();
146         super.okPressed();
147     }
148
149     /*
150      * @see Window#create()
151      */

152     public void create() {
153         super.create();
154         if (fLastStatus != null) {
155             updateStatus(fLastStatus);
156         }
157     }
158
159     /*
160      * @see Dialog#createButtonBar(Composite)
161      */

162     protected Control createButtonBar(Composite parent) {
163         Font font = parent.getFont();
164         Composite composite = new Composite(parent, SWT.NULL);
165         GridLayout layout = new GridLayout();
166         if (!fStatusLineAboveButtons) {
167             layout.numColumns = 2;
168         }
169         layout.marginHeight = 0;
170         layout.marginLeft = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
171         layout.marginWidth = 0;
172         composite.setLayout(layout);
173         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
174         composite.setFont(font);
175
176         if (!fStatusLineAboveButtons && isHelpAvailable()) {
177             createHelpControl(composite);
178         }
179         fStatusLine = new MessageLine(composite);
180         fStatusLine.setAlignment(SWT.LEFT);
181         GridData statusData = new GridData(GridData.FILL_HORIZONTAL);
182         fStatusLine.setErrorStatus(null);
183         fStatusLine.setFont(font);
184         if (fStatusLineAboveButtons && isHelpAvailable()) {
185             statusData.horizontalSpan = 2;
186             createHelpControl(composite);
187         }
188         fStatusLine.setLayoutData(statusData);
189
190         /*
191          * Create the rest of the button bar, but tell it not to
192          * create a help button (we've already created it).
193          */

194         boolean helpAvailable = isHelpAvailable();
195         setHelpAvailable(false);
196         super.createButtonBar(composite);
197         setHelpAvailable(helpAvailable);
198         return composite;
199     }
200
201 }
202
Popular Tags