KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > filters > NoPackageContainingFoldersFilter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.filters;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.core.resources.IFolder;
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.viewers.ViewerFilter;
20
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.JavaCore;
24
25 public class NoPackageContainingFoldersFilter extends ViewerFilter {
26
27     /*
28      * @see ViewerFilter
29      */

30     public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
31         if (element instanceof IFolder) {
32             try {
33                 if (containsPackage((IFolder)element))
34                     return true;
35                 else
36                     return false;
37             } catch (CoreException e) {
38                 return true;
39             }
40         }
41
42         return true;
43     }
44     
45     private boolean containsPackage(IFolder folder) throws CoreException {
46         IJavaElement element= JavaCore.create(folder);
47         if (element instanceof IPackageFragment)
48             return true;
49         IResource[] resources= folder.members();
50         for (int i= 0; i < resources.length; i++) {
51             if (resources[i] instanceof IFolder && containsPackage((IFolder)resources[i]))
52                 return true;
53         }
54         return false;
55     }
56
57 }
58
Popular Tags