KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > DestinationContentProvider


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.refactoring.reorg;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaModel;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28
29 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestinationValidator;
30
31 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35
36 public final class DestinationContentProvider extends StandardJavaElementContentProvider {
37     
38     private IReorgDestinationValidator fValidator;
39     
40     public DestinationContentProvider(IReorgDestinationValidator validator) {
41         super(true);
42         fValidator= validator;
43     }
44     
45     public boolean hasChildren(Object JavaDoc element) {
46         if (element instanceof IJavaElement){
47             IJavaElement javaElement= (IJavaElement) element;
48             if (! fValidator.canChildrenBeDestinations(javaElement))
49                 return false;
50             if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT){
51                 if (((IPackageFragmentRoot)javaElement).isArchive())
52                     return false;
53             }
54         } else if (element instanceof IResource) {
55             IResource resource= (IResource) element;
56             if (! fValidator.canChildrenBeDestinations(resource))
57                 return false;
58         }
59         return super.hasChildren(element);
60     }
61     
62     public Object JavaDoc[] getChildren(Object JavaDoc element) {
63         try {
64             if (element instanceof IJavaModel) {
65                 return concatenate(getJavaProjects((IJavaModel)element), getOpenNonJavaProjects((IJavaModel)element));
66             } else {
67                 Object JavaDoc[] children= doGetChildren(element);
68                 ArrayList JavaDoc result= new ArrayList JavaDoc(children.length);
69                 for (int i= 0; i < children.length; i++) {
70                     if (children[i] instanceof IJavaElement) {
71                         IJavaElement javaElement= (IJavaElement) children[i];
72                         if (fValidator.canElementBeDestination(javaElement) || fValidator.canChildrenBeDestinations(javaElement))
73                             result.add(javaElement);
74                     } else if (children[i] instanceof IResource) {
75                         IResource resource= (IResource) children[i];
76                         if (fValidator.canElementBeDestination(resource) || fValidator.canChildrenBeDestinations(resource))
77                             result.add(resource);
78                     }
79                 }
80                 return result.toArray();
81             }
82         } catch (JavaModelException e) {
83             JavaPlugin.log(e);
84             return new Object JavaDoc[0];
85         }
86     }
87
88     private Object JavaDoc[] doGetChildren(Object JavaDoc parentElement) {
89         if (parentElement instanceof IContainer) {
90             final IContainer container= (IContainer) parentElement;
91             return getResources(container);
92         }
93         return super.getChildren(parentElement);
94     }
95     
96     // Copied from supertype
97
private Object JavaDoc[] getResources(IContainer container) {
98         try {
99             IResource[] members= container.members();
100             IJavaProject javaProject= JavaCore.create(container.getProject());
101             if (javaProject == null || !javaProject.exists())
102                 return members;
103             boolean isFolderOnClasspath = javaProject.isOnClasspath(container);
104             List JavaDoc nonJavaResources= new ArrayList JavaDoc();
105             // Can be on classpath but as a member of non-java resource folder
106
for (int i= 0; i < members.length; i++) {
107                 IResource member= members[i];
108                 // A resource can also be a java element
109
// in the case of exclusion and inclusion filters.
110
// We therefore exclude Java elements from the list
111
// of non-Java resources.
112
if (isFolderOnClasspath) {
113                     if (javaProject.findPackageFragmentRoot(member.getFullPath()) == null) {
114                         nonJavaResources.add(member);
115                     }
116                 } else if (!javaProject.isOnClasspath(member)) {
117                     nonJavaResources.add(member);
118                 }
119             }
120             return nonJavaResources.toArray();
121         } catch(CoreException e) {
122             return NO_CHILDREN;
123         }
124     }
125     
126     private static Object JavaDoc[] getOpenNonJavaProjects(IJavaModel model) throws JavaModelException {
127         Object JavaDoc[] nonJavaProjects= model.getNonJavaResources();
128         ArrayList JavaDoc result= new ArrayList JavaDoc(nonJavaProjects.length);
129         for (int i= 0; i < nonJavaProjects.length; i++) {
130             IProject project = (IProject) nonJavaProjects[i];
131             if (project.isOpen())
132                 result.add(project);
133         }
134         return result.toArray();
135     }
136
137 }
138
Popular Tags