KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.dialogs.TrayDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.internal.WorkbenchMessages;
26
27 /**
28  * The abstract implementation of a selection dialog. It can be primed with
29  * initial selections (<code>setInitialSelections</code>), and returns the
30  * final selection (via <code>getResult</code>) after completion.
31  * <p>
32  * Clients may subclass this dialog to inherit its selection facilities.
33  * </p>
34  */

35 public abstract class SelectionDialog extends TrayDialog {
36     // the final collection of selected elements, or null if this dialog was
37
// canceled
38
private Object JavaDoc[] result;
39
40     // a collection of the initially-selected elements
41
private List JavaDoc initialSelections = new ArrayList JavaDoc();
42
43     // title of dialog
44
private String JavaDoc title;
45
46     // message to show user
47
private String JavaDoc message = ""; //$NON-NLS-1$
48

49     // dialog bounds strategy (since 3.2)
50
private int dialogBoundsStrategy = Dialog.DIALOG_PERSISTLOCATION | Dialog.DIALOG_PERSISTSIZE;
51
52     // dialog settings for storing bounds (since 3.2)
53
private IDialogSettings dialogBoundsSettings = null;
54
55     static String JavaDoc SELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_selectLabel;
56
57     static String JavaDoc DESELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_deselectLabel;
58
59     /**
60      * Creates a dialog instance. Note that the dialog will have no visual
61      * representation (no widgets) until it is told to open.
62      *
63      * @param parentShell
64      * the parent shell
65      */

66     protected SelectionDialog(Shell parentShell) {
67         super(parentShell);
68     }
69
70     /*
71      * (non-Javadoc) Method declared in Window.
72      */

73     protected void configureShell(Shell shell) {
74         super.configureShell(shell);
75         if (title != null) {
76             shell.setText(title);
77         }
78     }
79
80     /*
81      * (non-Javadoc) Method declared on Dialog.
82      */

83     protected void createButtonsForButtonBar(Composite parent) {
84         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
85                 true);
86         createButton(parent, IDialogConstants.CANCEL_ID,
87                 IDialogConstants.CANCEL_LABEL, false);
88     }
89
90     /**
91      * Creates the message area for this dialog.
92      * <p>
93      * This method is provided to allow subclasses to decide where the message
94      * will appear on the screen.
95      * </p>
96      *
97      * @param composite
98      * the parent composite
99      * @return the message label
100      */

101     protected Label createMessageArea(Composite composite) {
102         Label label = new Label(composite, SWT.NONE);
103         if (message != null) {
104             label.setText(message);
105         }
106         label.setFont(composite.getFont());
107         return label;
108     }
109
110     /**
111      * Returns the initial selection in this selection dialog.
112      *
113      * @deprecated use getInitialElementSelections() instead
114      * @return the list of initial selected elements or null
115      */

116     protected List JavaDoc getInitialSelections() {
117         if (initialSelections.isEmpty()) {
118             return null;
119         } else {
120             return getInitialElementSelections();
121         }
122     }
123
124     /**
125      * Returns the list of initial element selections.
126      *
127      * @return List
128      */

129     protected List JavaDoc getInitialElementSelections() {
130         return initialSelections;
131     }
132
133     /**
134      * Returns the message for this dialog.
135      *
136      * @return the message for this dialog
137      */

138     protected String JavaDoc getMessage() {
139         return message;
140     }
141
142     /**
143      * Returns the ok button.
144      *
145      * @return the ok button or <code>null</code> if the button is not created
146      * yet.
147      */

148     public Button getOkButton() {
149         return getButton(IDialogConstants.OK_ID);
150     }
151
152     /**
153      * Returns the list of selections made by the user, or <code>null</code>
154      * if the selection was canceled.
155      *
156      * @return the array of selected elements, or <code>null</code> if Cancel
157      * was pressed
158      */

159     public Object JavaDoc[] getResult() {
160         return result;
161     }
162
163     /**
164      * Sets the initial selection in this selection dialog to the given
165      * elements.
166      *
167      * @param selectedElements
168      * the array of elements to select
169      */

170     public void setInitialSelections(Object JavaDoc[] selectedElements) {
171         initialSelections = new ArrayList JavaDoc(selectedElements.length);
172         for (int i = 0; i < selectedElements.length; i++) {
173             initialSelections.add(selectedElements[i]);
174         }
175     }
176
177     /**
178      * Sets the initial selection in this selection dialog to the given
179      * elements.
180      *
181      * @param selectedElements
182      * the List of elements to select
183      */

184     public void setInitialElementSelections(List JavaDoc selectedElements) {
185         initialSelections = selectedElements;
186     }
187
188     /**
189      * Sets the message for this dialog.
190      *
191      * @param message
192      * the message
193      */

194     public void setMessage(String JavaDoc message) {
195         this.message = message;
196     }
197
198     /**
199      * Set the selections made by the user, or <code>null</code> if the
200      * selection was canceled.
201      *
202      * @param newResult
203      * list of selected elements, or <code>null</code> if Cancel
204      * was pressed
205      */

206     protected void setResult(List JavaDoc newResult) {
207         if (newResult == null) {
208             result = null;
209         } else {
210             result = new Object JavaDoc[newResult.size()];
211             newResult.toArray(result);
212         }
213     }
214
215     /**
216      * Set the selections made by the user, or <code>null</code> if the
217      * selection was canceled.
218      * <p>
219      * The selections may accessed using <code>getResult</code>.
220      * </p>
221      *
222      * @param newResult -
223      * the new values
224      * @since 2.0
225      */

226     protected void setSelectionResult(Object JavaDoc[] newResult) {
227         result = newResult;
228     }
229
230     /**
231      * Sets the title for this dialog.
232      *
233      * @param title
234      * the title
235      */

236     public void setTitle(String JavaDoc title) {
237         this.title = title;
238     }
239
240     /**
241      * Set the dialog settings that should be used to save the bounds of this
242      * dialog. This method is provided so that clients that directly use
243      * SelectionDialogs without subclassing them may specify how the bounds of
244      * the dialog are to be saved.
245      *
246      * @param settings
247      * the {@link IDialogSettings} that should be used to store the
248      * bounds of the dialog
249      *
250      * @param strategy
251      * the integer constant specifying how the bounds are saved.
252      * Specified using {@link Dialog#DIALOG_PERSISTLOCATION}
253      * and {@link Dialog#DIALOG_PERSISTSIZE}.
254      *
255      * @since 3.2
256      *
257      * @see Dialog#getDialogBoundsStrategy()
258      * @see Dialog#getDialogBoundsSettings()
259      */

260     public void setDialogBoundsSettings(IDialogSettings settings, int strategy) {
261         dialogBoundsStrategy = strategy;
262         dialogBoundsSettings = settings;
263     }
264
265     /**
266      * Gets the dialog settings that should be used for remembering the bounds
267      * of the dialog, according to the dialog bounds strategy. Overridden to
268      * provide the dialog settings that were set using
269      * {@link #setDialogBoundsSettings(IDialogSettings, int)}.
270      *
271      * @return the dialog settings used to store the dialog's location and/or
272      * size, or <code>null</code> if the dialog's bounds should not be
273      * stored.
274      *
275      * @since 3.2
276      *
277      * @see Dialog#getDialogBoundsStrategy()
278      * @see #setDialogBoundsSettings(IDialogSettings, int)
279      */

280     protected IDialogSettings getDialogBoundsSettings() {
281         return dialogBoundsSettings;
282     }
283
284     /**
285      * Get the integer constant that describes the strategy for persisting the
286      * dialog bounds. Overridden to provide the dialog bounds strategy that was
287      * set using {@link #setDialogBoundsSettings(IDialogSettings, int)}.
288      *
289      * @return the constant describing the strategy for persisting the dialog
290      * bounds.
291      *
292      * @since 3.2
293      * @see Dialog#DIALOG_PERSISTLOCATION
294      * @see Dialog#DIALOG_PERSISTSIZE
295      * @see Dialog#getDialogBoundsSettings()
296      * @see #setDialogBoundsSettings(IDialogSettings, int)
297      */

298     protected int getDialogBoundsStrategy() {
299         return dialogBoundsStrategy;
300     }
301 }
302
Popular Tags