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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
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;
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;
33 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 public final class DestinationContentProvider extends StandardJavaElementContentProvider {
40 private IReorgDestinationValidator fValidator;
42 public DestinationContentProvider(IReorgDestinationValidator validator) {
44 fValidator= validator;
48 public boolean hasChildren(Object element) {
49 IReorgDestination destination= ReorgDestinationFactory.createDestination(element);
50 if (!fValidator.canChildrenBeDestinations(destination))
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())
62 return super.hasChildren(element);
66 public Object[] getChildren(Object element) {
68 if (element instanceof IJavaModel) {
69 return concatenate(getJavaProjects((IJavaModel)element), getOpenNonJavaProjects((IJavaModel)element));
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]);
78 return result.toArray();
80 } catch (JavaModelException e) {
86 private Object[] doGetChildren(Object parentElement) {
87 if (parentElement instanceof IContainer) {
88 final IContainer container= (IContainer) parentElement;
89 return getResources(container);
91 return super.getChildren(parentElement);
94 // Copied from supertype
95 private Object[] getResources(IContainer container) {
97 IResource[] members= container.members();
98 IJavaProject javaProject= JavaCore.create(container.getProject());
99 if (javaProject == null || !javaProject.exists())
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);
114 } else if (!javaProject.isOnClasspath(member)) {
115 nonJavaResources.add(member);
118 return nonJavaResources.toArray();
119 } catch(CoreException e) {
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())
132 return result.toArray();