KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > PropertyFileFilter


1 /**********************************************************************
2 Copyright (c) 2003 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7 **********************************************************************/

8
9 package org.eclipse.ant.internal.ui.preferences;
10
11 import java.util.HashSet JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import org.eclipse.ant.internal.ui.model.AntUIPlugin;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerFilter;
23 import org.eclipse.swt.custom.BusyIndicator;
24
25 public class PropertyFileFilter extends ViewerFilter {
26
27     /**
28      * Objects to filter
29      */

30     protected List JavaDoc fFilter;
31     
32     /**
33      * Collection of property files and containers to display
34      */

35     private Set JavaDoc fPropertyFiles;
36
37     /**
38      * Creates a new filter that filters the given
39      * objects.
40      */

41     public PropertyFileFilter(List JavaDoc objects) {
42         fFilter = objects;
43         init();
44     }
45
46     /**
47      * @see ViewerFilter#select(Viewer, Object, Object)
48      */

49     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
50         return fPropertyFiles.contains(element) && !fFilter.contains(element);
51     }
52     
53     /**
54      * Search for all archives in the workspace.
55      */

56     private void init() {
57         BusyIndicator.showWhile(AntUIPlugin.getStandardDisplay(), new Runnable JavaDoc() {
58             public void run() {
59                 fPropertyFiles = new HashSet JavaDoc();
60                 traverse(ResourcesPlugin.getWorkspace().getRoot(), fPropertyFiles);
61             }
62         });
63     }
64
65     /**
66      * Traverse the given container, adding property file to the given set.
67      * Returns whether any files were added
68      */

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