KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.viewers.ViewerFilter;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27
28 /**
29  * Viewer filter for archive selection dialogs.
30  * Archives are files with file extension 'jar' and 'zip'.
31  * The filter is not case sensitive.
32  */

33 public class ArchiveFileFilter extends ViewerFilter {
34
35     public static final String JavaDoc[] FILTER_EXTENSIONS= new String JavaDoc[] {"*.jar;*.zip"}; //$NON-NLS-1$
36

37     private static final String JavaDoc[] fgArchiveExtensions= { "jar", "zip" }; //$NON-NLS-1$ //$NON-NLS-2$
38

39     private List JavaDoc fExcludes;
40     private boolean fRecursive;
41     
42     /**
43      * @param excludedFiles Excluded files will not pass the filter.
44      * <code>null</code> is allowed if no files should be excluded.
45      * @param recusive Folders are only shown if, searched recursively, contain
46      * an archive
47      */

48     public ArchiveFileFilter(IFile[] excludedFiles, boolean recusive) {
49         if (excludedFiles != null) {
50             fExcludes= Arrays.asList(excludedFiles);
51         } else {
52             fExcludes= null;
53         }
54         fRecursive= recusive;
55     }
56     
57     public ArchiveFileFilter(List JavaDoc excludedFiles, boolean recusive) {
58         fExcludes= excludedFiles;
59         fRecursive= recusive;
60     }
61     
62     /*
63      * @see ViewerFilter#select
64      */

65     public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
66         if (element instanceof IFile) {
67             if (fExcludes != null && fExcludes.contains(element)) {
68                 return false;
69             }
70             return isArchivePath(((IFile)element).getFullPath());
71         } else if (element instanceof IContainer) { // IProject, IFolder
72
if (!fRecursive) {
73                 return true;
74             }
75             // Ignore closed projects
76
if (element instanceof IProject && !((IProject)element).isOpen())
77                 return false;
78             try {
79                 IResource[] resources= ((IContainer)element).members();
80                 for (int i= 0; i < resources.length; i++) {
81                     // recursive! Only show containers that contain an archive
82
if (select(viewer, parent, resources[i])) {
83                         return true;
84                     }
85                 }
86             } catch (CoreException e) {
87                 JavaPlugin.log(e.getStatus());
88             }
89         }
90         return false;
91     }
92     
93     public static boolean isArchivePath(IPath path) {
94         String JavaDoc ext= path.getFileExtension();
95         if (ext != null && ext.length() != 0) {
96             return isArchiveFileExtension(ext);
97         }
98         return false;
99     }
100     
101     public static boolean isArchiveFileExtension(String JavaDoc ext) {
102         for (int i= 0; i < fgArchiveExtensions.length; i++) {
103             if (ext.equalsIgnoreCase(fgArchiveExtensions[i])) {
104                 return true;
105             }
106         }
107         return false;
108     }
109             
110     
111             
112 }
113
Popular Tags