1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui;
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
17 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IAdapterFactory;
24 import org.eclipse.ui.IContributorResourceAdapter;
25 import org.eclipse.ui.IPersistableElement;
26 import org.eclipse.ui.views.properties.FilePropertySource;
27 import org.eclipse.ui.views.properties.IPropertySource;
28 import org.eclipse.ui.views.properties.ResourcePropertySource;
31 * Implements basic UI support for Java elements. Implements handle to
32 * persistent support for Java elements.
34 public class JavaElementAdapterFactory implements IAdapterFactory,
35 IContributorResourceAdapter {
37 private static Class[] PROPERTIES = new Class[] {
38 IPropertySource.class,
40 // IWorkbenchAdapter.class,
41 // IResourceLocator.class,
42 IPersistableElement.class, IProject.class,
43 IContributorResourceAdapter.class,
44 // ITaskListResourceAdapter.class,
45 // IContainmentAdapter.class
48 // private Object fSearchPageScoreComputer;
49 // private static IResourceLocator fgResourceLocator= new ResourceLocator();
50 // private static JavaWorkbenchAdapter fgJavaWorkbenchAdapter= new
51 // JavaWorkbenchAdapter();
52 // private static ITaskListResourceAdapter fgTaskListAdapter= new
53 // JavaTaskListAdapter();
54 // private static JavaElementContainmentAdapter
55 // fgJavaElementContainmentAdapter= new JavaElementContainmentAdapter();
57 public Class[] getAdapterList() {
58 // updateLazyLoadedAdapters();
62 public Object getAdapter(Object element, Class key) {
63 // updateLazyLoadedAdapters();
64 IJavaElement java = (IJavaElement) element;
66 if (IPropertySource.class.equals(key)) {
67 return getProperties(java);
69 if (IResource.class.equals(key)) {
70 return getResource(java);
72 if (IProject.class.equals(key)) {
73 return getProject(java);
74 // } if (fSearchPageScoreComputer != null &&
75 // ISearchPageScoreComputer.class.equals(key)) {
76 // return fSearchPageScoreComputer;
77 // } if (IWorkbenchAdapter.class.equals(key)) {
78 // return fgJavaWorkbenchAdapter;
79 // } if (IResourceLocator.class.equals(key)) {
80 // return fgResourceLocator;
81 // } if (IPersistableElement.class.equals(key)) {
82 // return new PersistableJavaElementFactory(java);
84 if (IContributorResourceAdapter.class.equals(key)) {
86 // } if (ITaskListResourceAdapter.class.equals(key)) {
87 // return fgTaskListAdapter;
88 // } if (IContainmentAdapter.class.equals(key)) {
89 // return fgJavaElementContainmentAdapter;
94 private IResource getResource(IJavaElement element) {
95 // can't use IJavaElement.getResource directly as we are interrested in
97 // corresponding resource
98 switch (element.getElementType()) {
99 case IJavaElement.TYPE:
100 // top level types behave like the CU
101 IJavaElement parent = element.getParent();
102 if (parent instanceof ICompilationUnit) {
103 return JavaModelUtil.toOriginal((ICompilationUnit) parent)
107 case IJavaElement.COMPILATION_UNIT:
108 return JavaModelUtil.toOriginal((ICompilationUnit) element)
110 case IJavaElement.CLASS_FILE:
111 case IJavaElement.PACKAGE_FRAGMENT:
112 // test if in a archive
113 IPackageFragmentRoot root = (IPackageFragmentRoot) element
114 .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
115 if (!root.isArchive()) {
116 return element.getResource();
119 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
120 case IJavaElement.JAVA_PROJECT:
121 case IJavaElement.JAVA_MODEL:
122 return element.getResource();
129 * @see org.eclipse.ui.IContributorResourceAdapter#getAdaptedResource(org.eclipse.core.runtime.IAdaptable)
131 public IResource getAdaptedResource(IAdaptable adaptable) {
132 return getResource((IJavaElement) adaptable);
135 private IResource getProject(IJavaElement element) {
136 return element.getJavaProject().getProject();
139 private IPropertySource getProperties(IJavaElement element) {
140 IResource resource = getResource(element);
141 if (resource == null)
142 return new JavaElementProperties(element);
143 if (resource.getType() == IResource.FILE)
144 return new FilePropertySource((IFile) resource);
145 return new ResourcePropertySource(resource);
148 // private void updateLazyLoadedAdapters() {
149 // if (fSearchPageScoreComputer == null &&
150 // SearchUtil.isSearchPlugInActivated())
151 // createSearchPageScoreComputer();
154 // private void createSearchPageScoreComputer() {
155 // fSearchPageScoreComputer= new JavaSearchPageScoreComputer();
156 // PROPERTIES= new Class[] {
157 // IPropertySource.class,
159 // ISearchPageScoreComputer.class,
160 // IWorkbenchAdapter.class,
161 // IResourceLocator.class,
162 // IPersistableElement.class,
164 // IContributorResourceAdapter.class,
165 // ITaskListResourceAdapter.class,
166 // IContainmentAdapter.class