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