KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > JARFileSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.wizards.buildpaths;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.runtime.IStatus;
16
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Shell;
19
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24 import org.eclipse.jface.viewers.ViewerFilter;
25
26 import org.eclipse.ui.ISharedImages;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
29 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.JavaPluginImages;
33 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
34
35 /**
36  * Selection dialog to select a JAR on the file system.
37  * Set input to a java.io.File that point to folder.
38  */

39 public class JARFileSelectionDialog extends ElementTreeSelectionDialog {
40     
41     /**
42      * Constructor for JARFileSelectionDialog.
43      * @param parent parent shell
44      * @param multiSelect specifies if selecting multiple elements is allowed
45      * @param acceptFolders specifies if folders can be selected as well
46      */

47     public JARFileSelectionDialog(Shell parent, boolean multiSelect, boolean acceptFolders) {
48         super(parent, new FileLabelProvider(), new FileContentProvider());
49         setComparator(new FileViewerComparator());
50         addFilter(new FileArchiveFileFilter(acceptFolders));
51         setValidator(new FileSelectionValidator(multiSelect, acceptFolders));
52         setHelpAvailable(false);
53     }
54     
55     private static boolean isArchive(File JavaDoc file) {
56         String JavaDoc name= file.getName();
57         int detIndex= name.lastIndexOf('.');
58         return (detIndex != -1 && ArchiveFileFilter.isArchiveFileExtension(name.substring(detIndex + 1)));
59     }
60
61     private static class FileLabelProvider extends LabelProvider {
62         private final Image IMG_FOLDER= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
63         private final Image IMG_JAR= JavaPlugin.getDefault().getImageRegistry().get(JavaPluginImages.IMG_OBJS_EXTJAR);
64     
65         public Image getImage(Object JavaDoc element) {
66             if (element instanceof File JavaDoc) {
67                 File JavaDoc curr= (File JavaDoc) element;
68                 if (curr.isDirectory()) {
69                     return IMG_FOLDER;
70                 } else {
71                     return IMG_JAR;
72                 }
73             }
74             return null;
75         }
76     
77         public String JavaDoc getText(Object JavaDoc element) {
78             if (element instanceof File JavaDoc) {
79                 return ((File JavaDoc) element).getName();
80             }
81             return super.getText(element);
82         }
83     }
84     
85     private static class FileContentProvider implements ITreeContentProvider {
86         
87         private final Object JavaDoc[] EMPTY= new Object JavaDoc[0];
88     
89         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
90             if (parentElement instanceof File JavaDoc) {
91                 File JavaDoc[] children= ((File JavaDoc) parentElement).listFiles();
92                 if (children != null) {
93                     return children;
94                 }
95             }
96             return EMPTY;
97         }
98     
99         public Object JavaDoc getParent(Object JavaDoc element) {
100             if (element instanceof File JavaDoc) {
101                 return ((File JavaDoc) element).getParentFile();
102             }
103             return null;
104         }
105     
106         public boolean hasChildren(Object JavaDoc element) {
107             return getChildren(element).length > 0;
108         }
109     
110         public Object JavaDoc[] getElements(Object JavaDoc element) {
111             return getChildren(element);
112         }
113     
114         public void dispose() {
115         }
116     
117         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
118         }
119     
120     }
121     
122     private static class FileArchiveFileFilter extends ViewerFilter {
123         private final boolean fAcceptFolders;
124
125         public FileArchiveFileFilter(boolean acceptFolders) {
126             fAcceptFolders= acceptFolders;
127         }
128
129         public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
130             if (element instanceof File JavaDoc) {
131                 File JavaDoc file= (File JavaDoc) element;
132                 if (file.isFile()) {
133                     return isArchive(file);
134                 } else if (fAcceptFolders) {
135                     return true;
136                 } else {
137                     File JavaDoc[] listFiles= file.listFiles();
138                     if (listFiles != null) {
139                         for (int i= 0; i < listFiles.length; i++) {
140                             if (select(viewer, file, listFiles[i])) {
141                                 return true;
142                             }
143                         }
144                     }
145                 }
146             }
147             return false;
148         }
149     }
150     
151     private static class FileViewerComparator extends ViewerComparator {
152         public int category(Object JavaDoc element) {
153             if (element instanceof File JavaDoc) {
154                 if (((File JavaDoc) element).isFile()) {
155                     return 1;
156                 }
157             }
158             return 0;
159         }
160     }
161     
162     private static class FileSelectionValidator implements ISelectionStatusValidator {
163         private boolean fMultiSelect;
164         private boolean fAcceptFolders;
165         
166         public FileSelectionValidator(boolean multiSelect, boolean acceptFolders) {
167             fMultiSelect= multiSelect;
168             fAcceptFolders= acceptFolders;
169         }
170         
171         public IStatus validate(Object JavaDoc[] selection) {
172             int nSelected= selection.length;
173             if (nSelected == 0 || (nSelected > 1 && !fMultiSelect)) {
174                 return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
175
}
176             for (int i= 0; i < selection.length; i++) {
177                 Object JavaDoc curr= selection[i];
178                 if (curr instanceof File JavaDoc) {
179                     File JavaDoc file= (File JavaDoc) curr;
180                     if (!fAcceptFolders && !file.isFile()) {
181                         return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
182
}
183                 }
184             }
185             return new StatusInfo();
186         }
187     }
188
189
190 }
191
Popular Tags