KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > 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.jdt.internal.debug.ui.actions;
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.jdt.internal.debug.ui.JDIDebugUIPlugin;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.swt.custom.BusyIndicator;
25
26 /**
27  * ArchiveFilter
28  */

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

34     private Set JavaDoc fArchives;
35
36     /* (non-Javadoc)
37      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
38      */

39     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
40         return fArchives.contains(element) && super.select(viewer, parentElement, element);
41     }
42
43     /**
44      * Constructs a new filter to display archives and their containers,
45      * excluding the resources in the given list.
46      *
47      * @param objects resources to exclude
48      */

49     public ArchiveFilter(List JavaDoc objects) {
50         super(objects);
51         init();
52     }
53     
54     /**
55      * Search for all archives in the workspace.
56      */

57     private void init() {
58         BusyIndicator.showWhile(JDIDebugUIPlugin.getStandardDisplay(), new Runnable JavaDoc() {
59             public void run() {
60                 fArchives = new HashSet JavaDoc();
61                 traverse(ResourcesPlugin.getWorkspace().getRoot(), fArchives);
62             }
63         });
64     }
65
66     /**
67      * Traverse the given container, adding archives to the given set.
68      * Returns whether any files were added
69      *
70      * @param root
71      */

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