KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > browsers > ArchiveFilter


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.debug.internal.ui.sourcelookup.browsers;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.debug.internal.ui.DebugUIPlugin;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.viewers.ViewerFilter;
25 import org.eclipse.swt.custom.BusyIndicator;
26
27 /**
28  * ArchiveFilter
29  */

30 public class ArchiveFilter extends ViewerFilter {
31     
32     /**
33      * Collection of archives and containers to display
34      */

35     private Set JavaDoc fArchives;
36     
37     /**
38      * Collection of already existing archives
39      */

40     private List JavaDoc fExisting;
41
42     /* (non-Javadoc)
43      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
44      */

45     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
46         return fArchives.contains(element) && !fExisting.contains(element);
47     }
48
49     /**
50      * Constructs a new filter to display archives and their containers,
51      * excluding the resources in the given list.
52      *
53      * @param objects resources to exclude
54      */

55     public ArchiveFilter(List JavaDoc objects) {
56         fExisting = objects;
57         init();
58     }
59     
60     /**
61      * Search for all archives in the workspace.
62      */

63     private void init() {
64         BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), new Runnable JavaDoc() {
65             public void run() {
66                 fArchives = new HashSet JavaDoc();
67                 traverse(ResourcesPlugin.getWorkspace().getRoot(), fArchives);
68             }
69         });
70     }
71
72     /**
73      * Traverse the given container, adding archives to the given set.
74      * Returns whether any files were added
75      *
76      * @param root
77      */

78     private boolean traverse(IContainer container, Set JavaDoc set) {
79         boolean added = false;
80         try {
81             IResource[] resources = container.members();
82             for (int i = 0; i < resources.length; i++) {
83                 IResource resource = resources[i];
84                 if (resource instanceof IFile) {
85                     IFile file = (IFile)resource;
86                     String JavaDoc ext = file.getFileExtension();
87                     if (ext != null && (ext.equalsIgnoreCase("jar") || ext.equalsIgnoreCase("zip"))) { //$NON-NLS-1$ //$NON-NLS-2$
88
set.add(file);
89                         added = true;
90                     }
91                 } else if (resource instanceof IContainer) {
92                     if (traverse((IContainer)resource, set)) {
93                         set.add(resource);
94                         added = true;
95                     }
96                 }
97             }
98         } catch (CoreException e) {
99         }
100         return added;
101     }
102 }
103
Popular Tags