001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.jface.dialogs.Dialog;
016: import org.eclipse.jface.dialogs.IDialogConstants;
017: import org.eclipse.jface.dialogs.IDialogSettings;
018: import org.eclipse.jface.dialogs.TrayDialog;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Shell;
024: import org.eclipse.ui.internal.WorkbenchMessages;
025:
026: /**
027: * The abstract implementation of a selection dialog. It can be primed with
028: * initial selections (<code>setInitialSelections</code>), and returns the
029: * final selection (via <code>getResult</code>) after completion.
030: * <p>
031: * Clients may subclass this dialog to inherit its selection facilities.
032: * </p>
033: */
034: public abstract class SelectionDialog extends TrayDialog {
035: // the final collection of selected elements, or null if this dialog was
036: // canceled
037: private Object[] result;
038:
039: // a collection of the initially-selected elements
040: private List initialSelections = new ArrayList();
041:
042: // title of dialog
043: private String title;
044:
045: // message to show user
046: private String message = ""; //$NON-NLS-1$
047:
048: // dialog bounds strategy (since 3.2)
049: private int dialogBoundsStrategy = Dialog.DIALOG_PERSISTLOCATION
050: | Dialog.DIALOG_PERSISTSIZE;
051:
052: // dialog settings for storing bounds (since 3.2)
053: private IDialogSettings dialogBoundsSettings = null;
054:
055: static String SELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_selectLabel;
056:
057: static String DESELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_deselectLabel;
058:
059: /**
060: * Creates a dialog instance. Note that the dialog will have no visual
061: * representation (no widgets) until it is told to open.
062: *
063: * @param parentShell
064: * the parent shell
065: */
066: protected SelectionDialog(Shell parentShell) {
067: super (parentShell);
068: }
069:
070: /*
071: * (non-Javadoc) Method declared in Window.
072: */
073: protected void configureShell(Shell shell) {
074: super .configureShell(shell);
075: if (title != null) {
076: shell.setText(title);
077: }
078: }
079:
080: /*
081: * (non-Javadoc) Method declared on Dialog.
082: */
083: protected void createButtonsForButtonBar(Composite parent) {
084: createButton(parent, IDialogConstants.OK_ID,
085: IDialogConstants.OK_LABEL, true);
086: createButton(parent, IDialogConstants.CANCEL_ID,
087: IDialogConstants.CANCEL_LABEL, false);
088: }
089:
090: /**
091: * Creates the message area for this dialog.
092: * <p>
093: * This method is provided to allow subclasses to decide where the message
094: * will appear on the screen.
095: * </p>
096: *
097: * @param composite
098: * the parent composite
099: * @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 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 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 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[] 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[] selectedElements) {
171: initialSelections = new ArrayList(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 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 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 newResult) {
207: if (newResult == null) {
208: result = null;
209: } else {
210: result = new Object[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[] 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 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,
261: int strategy) {
262: dialogBoundsStrategy = strategy;
263: dialogBoundsSettings = settings;
264: }
265:
266: /**
267: * Gets the dialog settings that should be used for remembering the bounds
268: * of the dialog, according to the dialog bounds strategy. Overridden to
269: * provide the dialog settings that were set using
270: * {@link #setDialogBoundsSettings(IDialogSettings, int)}.
271: *
272: * @return the dialog settings used to store the dialog's location and/or
273: * size, or <code>null</code> if the dialog's bounds should not be
274: * stored.
275: *
276: * @since 3.2
277: *
278: * @see Dialog#getDialogBoundsStrategy()
279: * @see #setDialogBoundsSettings(IDialogSettings, int)
280: */
281: protected IDialogSettings getDialogBoundsSettings() {
282: return dialogBoundsSettings;
283: }
284:
285: /**
286: * Get the integer constant that describes the strategy for persisting the
287: * dialog bounds. Overridden to provide the dialog bounds strategy that was
288: * set using {@link #setDialogBoundsSettings(IDialogSettings, int)}.
289: *
290: * @return the constant describing the strategy for persisting the dialog
291: * bounds.
292: *
293: * @since 3.2
294: * @see Dialog#DIALOG_PERSISTLOCATION
295: * @see Dialog#DIALOG_PERSISTSIZE
296: * @see Dialog#getDialogBoundsSettings()
297: * @see #setDialogBoundsSettings(IDialogSettings, int)
298: */
299: protected int getDialogBoundsStrategy() {
300: return dialogBoundsStrategy;
301: }
302:
303: /*
304: * (non-Javadoc)
305: * @see org.eclipse.jface.dialogs.Dialog#isResizable()
306: */
307: protected boolean isResizable() {
308: return true;
309: }
310: }
|