KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > FileFolderSelectionDialog


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.internal.ide.dialogs;
12
13 import org.eclipse.core.filesystem.IFileStore;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ViewerComparator;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.ISharedImages;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
27 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
29
30 /**
31  * Selection dialog to select files and/or folders on the file system. Use
32  * setInput to set input to an IFileStore that points to a folder.
33  *
34  * @since 2.1
35  */

36 public class FileFolderSelectionDialog extends ElementTreeSelectionDialog {
37
38     /**
39      * Label provider for IFileStore objects.
40      */

41     private static class FileLabelProvider extends LabelProvider {
42         private static final Image IMG_FOLDER = PlatformUI.getWorkbench()
43                 .getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
44
45         private static final Image IMG_FILE = PlatformUI.getWorkbench()
46                 .getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
47
48         /*
49          * (non-Javadoc)
50          *
51          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
52          */

53         public Image getImage(Object JavaDoc element) {
54             if (element instanceof IFileStore) {
55                 IFileStore curr = (IFileStore) element;
56                 if (curr.fetchInfo().isDirectory()) {
57                     return IMG_FOLDER;
58                 }
59                 return IMG_FILE;
60             }
61             return null;
62         }
63
64         /*
65          * (non-Javadoc)
66          *
67          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
68          */

69         public String JavaDoc getText(Object JavaDoc element) {
70             if (element instanceof IFileStore) {
71                 return ((IFileStore) element).getName();
72             }
73             return super.getText(element);
74         }
75     }
76
77     /**
78      * Content provider for IFileStore objects.
79      */

80     private static class FileContentProvider implements ITreeContentProvider {
81         private static final Object JavaDoc[] EMPTY = new Object JavaDoc[0];
82
83         private IFileStoreFilter fileFilter;
84
85         /**
86          * Creates a new instance of the receiver.
87          *
88          * @param showFiles
89          * <code>true</code> files and folders are returned by the
90          * receiver. <code>false</code> only folders are returned.
91          */

92         public FileContentProvider(final boolean showFiles) {
93             fileFilter = new IFileStoreFilter() {
94
95                 /*
96                  * (non-Javadoc)
97                  *
98                  * @see org.eclipse.ui.internal.ide.dialogs.IFileStoreFilter#accept(org.eclipse.core.filesystem.IFileStore)
99                  */

100                 public boolean accept(IFileStore file) {
101                     if (!file.fetchInfo().isDirectory() && showFiles == false) {
102                         return false;
103                     }
104                     return true;
105                 }
106             };
107         }
108
109         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
110             if (parentElement instanceof IFileStore) {
111                 IFileStore[] children = IDEResourceInfoUtils.listFileStores(
112                         (IFileStore) parentElement, fileFilter,
113                         new NullProgressMonitor());
114                 if (children != null) {
115                     return children;
116                 }
117             }
118             return EMPTY;
119         }
120
121         /*
122          * (non-Javadoc)
123          *
124          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
125          */

126         public Object JavaDoc getParent(Object JavaDoc element) {
127             if (element instanceof IFileStore) {
128                 return ((IFileStore) element).getParent();
129             }
130             return null;
131         }
132
133         public boolean hasChildren(Object JavaDoc element) {
134             return getChildren(element).length > 0;
135         }
136
137         public Object JavaDoc[] getElements(Object JavaDoc element) {
138             return getChildren(element);
139         }
140
141         public void dispose() {
142         }
143
144         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
145         }
146     }
147
148     /**
149      * Viewer sorter that places folders first, then files.
150      */

151     private static class FileViewerSorter extends ViewerComparator {
152         /*
153          * (non-Javadoc)
154          *
155          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
156          */

157         public int category(Object JavaDoc element) {
158             if (element instanceof IFileStore
159                     && !((IFileStore) element).fetchInfo().isDirectory()) {
160                 return 1;
161             }
162             return 0;
163         }
164     }
165
166     /**
167      * Validates the selection based on the multi select and folder setting.
168      */

169     private static class FileSelectionValidator implements
170             ISelectionStatusValidator {
171         private boolean multiSelect;
172
173         private boolean acceptFolders;
174
175         /**
176          * Creates a new instance of the receiver.
177          *
178          * @param multiSelect
179          * <code>true</code> if multi selection is allowed.
180          * <code>false</code> if only single selection is allowed.
181          * @param acceptFolders
182          * <code>true</code> if folders can be selected in the
183          * dialog. <code>false</code> only files and be selected.
184          */

185         public FileSelectionValidator(boolean multiSelect, boolean acceptFolders) {
186             this.multiSelect = multiSelect;
187             this.acceptFolders = acceptFolders;
188         }
189
190         /*
191          * (non-Javadoc)
192          *
193          * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[])
194          */

195         public IStatus validate(Object JavaDoc[] selection) {
196             int nSelected = selection.length;
197             String JavaDoc pluginId = IDEWorkbenchPlugin.IDE_WORKBENCH;
198
199             if (nSelected == 0 || (nSelected > 1 && multiSelect == false)) {
200                 return new Status(IStatus.ERROR, pluginId, IStatus.ERROR,
201                         IDEResourceInfoUtils.EMPTY_STRING, null);
202             }
203             for (int i = 0; i < selection.length; i++) {
204                 Object JavaDoc curr = selection[i];
205                 if (curr instanceof IFileStore) {
206                     IFileStore file = (IFileStore) curr;
207                     if (acceptFolders == false
208                             && file.fetchInfo().isDirectory()) {
209                         return new Status(IStatus.ERROR, pluginId,
210                                 IStatus.ERROR,
211                                 IDEResourceInfoUtils.EMPTY_STRING, null);
212                     }
213
214                 }
215             }
216             return Status.OK_STATUS;
217         }
218     }
219
220     /**
221      * Creates a new instance of the receiver.
222      *
223      * @param parent
224      * @param multiSelect
225      * <code>true</code> if multi selection is allowed.
226      * <code>false</code> if only single selection is allowed.
227      * @param type
228      * one or both of <code>IResource.FILE</code> and
229      * <code>IResource.FOLDER</code>, ORed together. If
230      * <code>IResource.FILE</code> is specified files and folders
231      * are displayed in the dialog. Otherwise only folders are
232      * displayed. If <code>IResource.FOLDER</code> is specified
233      * folders can be selected in addition to files.
234      */

235     public FileFolderSelectionDialog(Shell parent, boolean multiSelect, int type) {
236         super(parent, new FileLabelProvider(), new FileContentProvider(
237                 (type & IResource.FILE) != 0));
238         setComparator(new FileViewerSorter());
239         setValidator(new FileSelectionValidator(multiSelect,
240                 (type & IResource.FOLDER) != 0));
241     }
242 }
243
Popular Tags