001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.packageview;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.IPath;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022:
023: import org.eclipse.jface.resource.ImageDescriptor;
024:
025: import org.eclipse.ui.PlatformUI;
026: import org.eclipse.ui.model.IWorkbenchAdapter;
027:
028: import org.eclipse.ui.ide.IDE;
029:
030: import org.eclipse.jdt.core.ClasspathContainerInitializer;
031: import org.eclipse.jdt.core.IClasspathContainer;
032: import org.eclipse.jdt.core.IClasspathEntry;
033: import org.eclipse.jdt.core.IJavaProject;
034: import org.eclipse.jdt.core.IPackageFragmentRoot;
035: import org.eclipse.jdt.core.JavaCore;
036: import org.eclipse.jdt.core.JavaModelException;
037:
038: import org.eclipse.jdt.internal.corext.util.Messages;
039:
040: import org.eclipse.jdt.internal.ui.JavaPlugin;
041: import org.eclipse.jdt.internal.ui.JavaPluginImages;
042:
043: /**
044: * Representation of class path containers in Java UI.
045: */
046: public class ClassPathContainer extends PackageFragmentRootContainer {
047:
048: private IClasspathEntry fClassPathEntry;
049: private IClasspathContainer fContainer;
050:
051: public static class RequiredProjectWrapper implements IAdaptable,
052: IWorkbenchAdapter {
053:
054: private final ClassPathContainer fParent;
055: private final IJavaProject fProject;
056:
057: public RequiredProjectWrapper(ClassPathContainer parent,
058: IJavaProject project) {
059: fParent = parent;
060: fProject = project;
061: }
062:
063: public IJavaProject getProject() {
064: return fProject;
065: }
066:
067: public ClassPathContainer getParentClassPathContainer() {
068: return fParent;
069: }
070:
071: public Object getAdapter(Class adapter) {
072: if (adapter == IWorkbenchAdapter.class)
073: return this ;
074: return null;
075: }
076:
077: public Object[] getChildren(Object o) {
078: return new Object[0];
079: }
080:
081: public ImageDescriptor getImageDescriptor(Object object) {
082: return PlatformUI.getWorkbench().getSharedImages()
083: .getImageDescriptor(
084: IDE.SharedImages.IMG_OBJ_PROJECT);
085: }
086:
087: public String getLabel(Object o) {
088: return fProject.getElementName();
089: }
090:
091: public Object getParent(Object o) {
092: return fParent;
093: }
094: }
095:
096: public ClassPathContainer(IJavaProject parent, IClasspathEntry entry) {
097: super (parent);
098: fClassPathEntry = entry;
099: try {
100: fContainer = JavaCore.getClasspathContainer(
101: entry.getPath(), parent);
102: } catch (JavaModelException e) {
103: fContainer = null;
104: }
105: }
106:
107: public boolean equals(Object obj) {
108: if (obj instanceof ClassPathContainer) {
109: ClassPathContainer other = (ClassPathContainer) obj;
110: if (getJavaProject().equals(other.getJavaProject())
111: && fClassPathEntry.equals(other.fClassPathEntry)) {
112: return true;
113: }
114:
115: }
116: return false;
117: }
118:
119: public int hashCode() {
120: return getJavaProject().hashCode() * 17
121: + fClassPathEntry.hashCode();
122: }
123:
124: public IPackageFragmentRoot[] getPackageFragmentRoots() {
125: return getJavaProject().findPackageFragmentRoots(
126: fClassPathEntry);
127: }
128:
129: public IAdaptable[] getChildren() {
130: List list = new ArrayList();
131: IPackageFragmentRoot[] roots = getPackageFragmentRoots();
132: for (int i = 0; i < roots.length; i++) {
133: list.add(roots[i]);
134: }
135: if (fContainer != null) {
136: IClasspathEntry[] classpathEntries = fContainer
137: .getClasspathEntries();
138: if (classpathEntries == null) {
139: // invalid implementation of a classpath container
140: JavaPlugin
141: .log(new IllegalArgumentException(
142: "Invalid classpath container implementation: getClasspathEntries() returns null. " + fContainer.getPath())); //$NON-NLS-1$
143: } else {
144: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
145: .getRoot();
146: for (int i = 0; i < classpathEntries.length; i++) {
147: IClasspathEntry entry = classpathEntries[i];
148: if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
149: IResource resource = root.findMember(entry
150: .getPath());
151: if (resource instanceof IProject)
152: list
153: .add(new RequiredProjectWrapper(
154: this ,
155: JavaCore
156: .create((IProject) resource)));
157: }
158: }
159: }
160: }
161: return (IAdaptable[]) list.toArray(new IAdaptable[list.size()]);
162: }
163:
164: public ImageDescriptor getImageDescriptor() {
165: return JavaPluginImages.DESC_OBJS_LIBRARY;
166: }
167:
168: public String getLabel() {
169: if (fContainer != null)
170: return fContainer.getDescription();
171:
172: IPath path = fClassPathEntry.getPath();
173: String containerId = path.segment(0);
174: ClasspathContainerInitializer initializer = JavaCore
175: .getClasspathContainerInitializer(containerId);
176: if (initializer != null) {
177: String description = initializer.getDescription(path,
178: getJavaProject());
179: return Messages.format(
180: PackagesMessages.ClassPathContainer_unbound_label,
181: description);
182: }
183: return Messages.format(
184: PackagesMessages.ClassPathContainer_unknown_label, path
185: .toString());
186: }
187:
188: public IClasspathEntry getClasspathEntry() {
189: return fClassPathEntry;
190: }
191:
192: static boolean contains(IJavaProject project,
193: IClasspathEntry entry, IPackageFragmentRoot root) {
194: IPackageFragmentRoot[] roots = project
195: .findPackageFragmentRoots(entry);
196: for (int i = 0; i < roots.length; i++) {
197: if (roots[i].equals(root))
198: return true;
199: }
200: return false;
201: }
202:
203: }
|