KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 package org.eclipse.ui.dialogs;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.eclipse.jface.viewers.CheckStateChangedEvent;
20 import org.eclipse.jface.viewers.ICheckStateListener;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
34 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
35 import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup;
36 import org.eclipse.ui.model.WorkbenchContentProvider;
37 import org.eclipse.ui.model.WorkbenchLabelProvider;
38 import org.eclipse.ui.model.WorkbenchViewerComparator;
39
40 /**
41  * A standard file selection dialog which solicits a list of files from the user.
42  * The <code>getResult</code> method returns the selected files.
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  * <p>
47  * Example:
48  * <pre>
49  * FileSelectionDialog dialog =
50  * new FileSelectionDialog(getShell(), rootElement, msg);
51  * dialog.setInitialSelections(selectedResources);
52  * dialog.open();
53  * return dialog.getResult();
54  * </pre>
55  * </p>
56  * @deprecated Use org.eclipse.swt.widgets.FileDialog,
57  */

58 public class FileSelectionDialog extends SelectionDialog {
59     // the root file representative to populate the viewer with
60
private FileSystemElement root;
61
62     // the visual selection widget group
63
CheckboxTreeAndListGroup selectionGroup;
64
65     // expand all items in the tree view on dialog open
66
private boolean expandAllOnOpen = false;
67
68     // sizing constants
69
private static final int SIZING_SELECTION_WIDGET_WIDTH = 500;
70
71     private static final int SIZING_SELECTION_WIDGET_HEIGHT = 250;
72
73     /**
74      * Creates a file selection dialog rooted at the given file system element.
75      *
76      * @param parentShell the parent shell
77      * @param fileSystemElement the root element to populate this dialog with
78      * @param message the message to be displayed at the top of this dialog, or
79      * <code>null</code> to display a default message
80      */

81     public FileSelectionDialog(Shell parentShell,
82             FileSystemElement fileSystemElement, String JavaDoc message) {
83         super(parentShell);
84         setTitle(IDEWorkbenchMessages.FileSelectionDialog_title);
85         root = fileSystemElement;
86         if (message != null) {
87             setMessage(message);
88         } else {
89             setMessage(IDEWorkbenchMessages.FileSelectionDialog_message);
90         }
91     }
92
93     /**
94      * Add the selection and deselection buttons to the dialog.
95      * @param composite org.eclipse.swt.widgets.Composite
96      */

97     private void addSelectionButtons(Composite composite) {
98
99         Composite buttonComposite = new Composite(composite, SWT.RIGHT);
100         GridLayout layout = new GridLayout();
101         layout.numColumns = 2;
102         buttonComposite.setLayout(layout);
103         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END);
104         composite.setData(data);
105
106         Button selectButton = new Button(buttonComposite, SWT.PUSH);
107         selectButton.setText(SELECT_ALL_TITLE);
108         SelectionListener listener = new SelectionAdapter() {
109             public void widgetSelected(SelectionEvent e) {
110                 selectionGroup.setAllSelections(true);
111             }
112         };
113         selectButton.addSelectionListener(listener);
114
115         Button deselectButton = new Button(buttonComposite, SWT.PUSH);
116         deselectButton.setText(DESELECT_ALL_TITLE);
117         listener = new SelectionAdapter() {
118             public void widgetSelected(SelectionEvent e) {
119                 selectionGroup.setAllSelections(false);
120
121             }
122         };
123         deselectButton.addSelectionListener(listener);
124
125     }
126
127     /**
128      * Visually checks the previously-specified elements in the container (left)
129      * portion of this dialog's file selection viewer.
130      */

131     private void checkInitialSelections() {
132         Iterator JavaDoc itemsToCheck = getInitialElementSelections().iterator();
133
134         while (itemsToCheck.hasNext()) {
135             FileSystemElement currentElement = (FileSystemElement) itemsToCheck
136                     .next();
137
138             if (currentElement.isDirectory()) {
139                 selectionGroup.initialCheckTreeItem(currentElement);
140             } else {
141                 selectionGroup.initialCheckListItem(currentElement);
142             }
143         }
144     }
145
146     /* (non-Javadoc)
147      * Method declared in Window.
148      */

149     protected void configureShell(Shell shell) {
150         super.configureShell(shell);
151         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
152                 IIDEHelpContextIds.FILE_SELECTION_DIALOG);
153     }
154
155     public void create() {
156         super.create();
157         initializeDialog();
158     }
159
160     /* (non-Javadoc)
161      * Method declared on Dialog.
162      */

163     protected Control createDialogArea(Composite parent) {
164         // page group
165
Composite composite = (Composite) super.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(), SWT.NONE,
179                 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 JavaDoc[] getChildren(Object JavaDoc o) {
217                 if (o instanceof FileSystemElement) {
218                     return ((FileSystemElement) o).getFiles().getChildren(o);
219                 }
220                 return new Object JavaDoc[0];
221             }
222         };
223     }
224
225     /**
226      * Returns a content provider for <code>FileSystemElement</code>s that returns
227      * only folders as children.
228      */

229     private ITreeContentProvider getFolderProvider() {
230         return new WorkbenchContentProvider() {
231             public Object JavaDoc[] getChildren(Object JavaDoc o) {
232                 if (o instanceof FileSystemElement) {
233                     return ((FileSystemElement) o).getFolders().getChildren(o);
234                 }
235                 return new Object JavaDoc[0];
236             }
237         };
238     }
239
240     /**
241      * Initializes this dialog's controls.
242      */

243     private void initializeDialog() {
244         // initialize page
245
if (getInitialElementSelections().isEmpty()) {
246             getOkButton().setEnabled(false);
247         } else {
248             checkInitialSelections();
249         }
250         selectionGroup.aboutToOpen();
251         if (expandAllOnOpen) {
252             selectionGroup.expandAll();
253         }
254     }
255
256     /**
257      * The <code>FileSelectionDialog</code> implementation of this
258      * <code>Dialog</code> method builds a list of the selected files for later
259      * retrieval by the client and closes this dialog.
260      */

261     protected void okPressed() {
262         Iterator JavaDoc resultEnum = selectionGroup.getAllCheckedListItems();
263         ArrayList JavaDoc list = new ArrayList JavaDoc();
264         while (resultEnum.hasNext()) {
265             list.add(resultEnum.next());
266         }
267         setResult(list);
268         super.okPressed();
269     }
270
271     /**
272      * Set whether the tree view of the file system element
273      * will be fully expanded when the dialog is opened.
274      *
275      * @param expandAll true to expand all on dialog open, false otherwise.
276      */

277     public void setExpandAllOnOpen(boolean expandAll) {
278         expandAllOnOpen = expandAll;
279     }
280 }
281
Popular Tags