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: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
011: * font should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016:
017: import org.eclipse.jface.viewers.CheckStateChangedEvent;
018: import org.eclipse.jface.viewers.ICheckStateListener;
019: import org.eclipse.jface.viewers.ITreeContentProvider;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.SelectionAdapter;
022: import org.eclipse.swt.events.SelectionEvent;
023: import org.eclipse.swt.events.SelectionListener;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.PlatformUI;
031: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
032: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
033: import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup;
034: import org.eclipse.ui.model.WorkbenchContentProvider;
035: import org.eclipse.ui.model.WorkbenchLabelProvider;
036: import org.eclipse.ui.model.WorkbenchViewerComparator;
037:
038: /**
039: * A standard file selection dialog which solicits a list of files from the user.
040: * The <code>getResult</code> method returns the selected files.
041: * <p>
042: * This class may be instantiated; it is not intended to be subclassed.
043: * </p>
044: * <p>
045: * Example:
046: * <pre>
047: * FileSelectionDialog dialog =
048: * new FileSelectionDialog(getShell(), rootElement, msg);
049: * dialog.setInitialSelections(selectedResources);
050: * dialog.open();
051: * return dialog.getResult();
052: * </pre>
053: * </p>
054: * @deprecated Use org.eclipse.swt.widgets.FileDialog,
055: */
056: public class FileSelectionDialog extends SelectionDialog {
057: // the root file representative to populate the viewer with
058: private FileSystemElement root;
059:
060: // the visual selection widget group
061: CheckboxTreeAndListGroup selectionGroup;
062:
063: // expand all items in the tree view on dialog open
064: private boolean expandAllOnOpen = false;
065:
066: // sizing constants
067: private static final int SIZING_SELECTION_WIDGET_WIDTH = 500;
068:
069: private static final int SIZING_SELECTION_WIDGET_HEIGHT = 250;
070:
071: /**
072: * Creates a file selection dialog rooted at the given file system element.
073: *
074: * @param parentShell the parent shell
075: * @param fileSystemElement the root element to populate this dialog with
076: * @param message the message to be displayed at the top of this dialog, or
077: * <code>null</code> to display a default message
078: */
079: public FileSelectionDialog(Shell parentShell,
080: FileSystemElement fileSystemElement, String message) {
081: super (parentShell);
082: setTitle(IDEWorkbenchMessages.FileSelectionDialog_title);
083: root = fileSystemElement;
084: if (message != null) {
085: setMessage(message);
086: } else {
087: setMessage(IDEWorkbenchMessages.FileSelectionDialog_message);
088: }
089: }
090:
091: /**
092: * Add the selection and deselection buttons to the dialog.
093: * @param composite org.eclipse.swt.widgets.Composite
094: */
095: private void addSelectionButtons(Composite composite) {
096:
097: Composite buttonComposite = new Composite(composite, SWT.RIGHT);
098: GridLayout layout = new GridLayout();
099: layout.numColumns = 2;
100: buttonComposite.setLayout(layout);
101: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END);
102: composite.setData(data);
103:
104: Button selectButton = new Button(buttonComposite, SWT.PUSH);
105: selectButton.setText(SELECT_ALL_TITLE);
106: SelectionListener listener = new SelectionAdapter() {
107: public void widgetSelected(SelectionEvent e) {
108: selectionGroup.setAllSelections(true);
109: }
110: };
111: selectButton.addSelectionListener(listener);
112:
113: Button deselectButton = new Button(buttonComposite, SWT.PUSH);
114: deselectButton.setText(DESELECT_ALL_TITLE);
115: listener = new SelectionAdapter() {
116: public void widgetSelected(SelectionEvent e) {
117: selectionGroup.setAllSelections(false);
118:
119: }
120: };
121: deselectButton.addSelectionListener(listener);
122:
123: }
124:
125: /**
126: * Visually checks the previously-specified elements in the container (left)
127: * portion of this dialog's file selection viewer.
128: */
129: private void checkInitialSelections() {
130: Iterator itemsToCheck = getInitialElementSelections()
131: .iterator();
132:
133: while (itemsToCheck.hasNext()) {
134: FileSystemElement currentElement = (FileSystemElement) itemsToCheck
135: .next();
136:
137: if (currentElement.isDirectory()) {
138: selectionGroup.initialCheckTreeItem(currentElement);
139: } else {
140: selectionGroup.initialCheckListItem(currentElement);
141: }
142: }
143: }
144:
145: /* (non-Javadoc)
146: * Method declared in Window.
147: */
148: protected void configureShell(Shell shell) {
149: super .configureShell(shell);
150: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
151: IIDEHelpContextIds.FILE_SELECTION_DIALOG);
152: }
153:
154: public void create() {
155: super .create();
156: initializeDialog();
157: }
158:
159: /* (non-Javadoc)
160: * Method declared on Dialog.
161: */
162: protected Control createDialogArea(Composite parent) {
163: // page group
164: Composite composite = (Composite) super
165: .createDialogArea(parent);
166:
167: createMessageArea(composite);
168:
169: // Create a fake parent of the root to be the dialog input element.
170: // Use an empty label so that display of the element's full name
171: // doesn't include a confusing label
172: FileSystemElement input = new FileSystemElement("", null, true);//$NON-NLS-1$
173: input.addChild(root);
174: root.setParent(input);
175:
176: selectionGroup = new CheckboxTreeAndListGroup(composite, input,
177: getFolderProvider(), new WorkbenchLabelProvider(),
178: getFileProvider(), new WorkbenchLabelProvider(),
179: SWT.NONE, SIZING_SELECTION_WIDGET_WIDTH, // since this page has no other significantly-sized
180: SIZING_SELECTION_WIDGET_HEIGHT); // widgets we need to hardcode the combined widget's
181: // size, otherwise it will open too small
182:
183: ICheckStateListener listener = new ICheckStateListener() {
184: public void checkStateChanged(CheckStateChangedEvent event) {
185: getOkButton().setEnabled(
186: selectionGroup.getCheckedElementCount() > 0);
187: }
188: };
189:
190: WorkbenchViewerComparator comparator = new WorkbenchViewerComparator();
191: selectionGroup.setTreeComparator(comparator);
192: selectionGroup.setListComparator(comparator);
193: selectionGroup.addCheckStateListener(listener);
194:
195: addSelectionButtons(composite);
196:
197: return composite;
198: }
199:
200: /**
201: * Returns whether the tree view of the file system element
202: * will be fully expanded when the dialog is opened.
203: *
204: * @return true to expand all on dialog open, false otherwise.
205: */
206: public boolean getExpandAllOnOpen() {
207: return expandAllOnOpen;
208: }
209:
210: /**
211: * Returns a content provider for <code>FileSystemElement</code>s that returns
212: * only files as children.
213: */
214: private ITreeContentProvider getFileProvider() {
215: return new WorkbenchContentProvider() {
216: public Object[] getChildren(Object o) {
217: if (o instanceof FileSystemElement) {
218: return ((FileSystemElement) o).getFiles()
219: .getChildren(o);
220: }
221: return new Object[0];
222: }
223: };
224: }
225:
226: /**
227: * Returns a content provider for <code>FileSystemElement</code>s that returns
228: * only folders as children.
229: */
230: private ITreeContentProvider getFolderProvider() {
231: return new WorkbenchContentProvider() {
232: public Object[] getChildren(Object o) {
233: if (o instanceof FileSystemElement) {
234: return ((FileSystemElement) o).getFolders()
235: .getChildren(o);
236: }
237: return new Object[0];
238: }
239: };
240: }
241:
242: /**
243: * Initializes this dialog's controls.
244: */
245: private void initializeDialog() {
246: // initialize page
247: if (getInitialElementSelections().isEmpty()) {
248: getOkButton().setEnabled(false);
249: } else {
250: checkInitialSelections();
251: }
252: selectionGroup.aboutToOpen();
253: if (expandAllOnOpen) {
254: selectionGroup.expandAll();
255: }
256: }
257:
258: /**
259: * The <code>FileSelectionDialog</code> implementation of this
260: * <code>Dialog</code> method builds a list of the selected files for later
261: * retrieval by the client and closes this dialog.
262: */
263: protected void okPressed() {
264: Iterator resultEnum = selectionGroup.getAllCheckedListItems();
265: ArrayList list = new ArrayList();
266: while (resultEnum.hasNext()) {
267: list.add(resultEnum.next());
268: }
269: setResult(list);
270: super .okPressed();
271: }
272:
273: /**
274: * Set whether the tree view of the file system element
275: * will be fully expanded when the dialog is opened.
276: *
277: * @param expandAll true to expand all on dialog open, false otherwise.
278: */
279: public void setExpandAllOnOpen(boolean expandAll) {
280: expandAllOnOpen = expandAll;
281: }
282: }
|