fix #761 also see #762.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / JavaElementAdapterFactory.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui;
13
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;
18
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;
29
30 /**
31  * Implements basic UI support for Java elements. Implements handle to
32  * persistent support for Java elements.
33  */
34 public class JavaElementAdapterFactory implements IAdapterFactory,
35                 IContributorResourceAdapter {
36
37         private static Class[] PROPERTIES = new Class[] {
38                         IPropertySource.class,
39                         IResource.class,
40                         // IWorkbenchAdapter.class,
41                         // IResourceLocator.class,
42                         IPersistableElement.class, IProject.class,
43                         IContributorResourceAdapter.class,
44         // ITaskListResourceAdapter.class,
45         // IContainmentAdapter.class
46         };
47
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();
56
57         public Class[] getAdapterList() {
58                 // updateLazyLoadedAdapters();
59                 return PROPERTIES;
60         }
61
62         public Object getAdapter(Object element, Class key) {
63                 // updateLazyLoadedAdapters();
64                 IJavaElement java = (IJavaElement) element;
65
66                 if (IPropertySource.class.equals(key)) {
67                         return getProperties(java);
68                 }
69                 if (IResource.class.equals(key)) {
70                         return getResource(java);
71                 }
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);
83                 }
84                 if (IContributorResourceAdapter.class.equals(key)) {
85                         return this;
86                         // } if (ITaskListResourceAdapter.class.equals(key)) {
87                         // return fgTaskListAdapter;
88                         // } if (IContainmentAdapter.class.equals(key)) {
89                         // return fgJavaElementContainmentAdapter;
90                 }
91                 return null;
92         }
93
94         private IResource getResource(IJavaElement element) {
95                 // can't use IJavaElement.getResource directly as we are interrested in
96                 // the
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)
104                                                 .getResource();
105                         }
106                         return null;
107                 case IJavaElement.COMPILATION_UNIT:
108                         return JavaModelUtil.toOriginal((ICompilationUnit) element)
109                                         .getResource();
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();
117                         }
118                         return null;
119                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
120                 case IJavaElement.JAVA_PROJECT:
121                 case IJavaElement.JAVA_MODEL:
122                         return element.getResource();
123                 default:
124                         return null;
125                 }
126         }
127
128         /*
129          * @see org.eclipse.ui.IContributorResourceAdapter#getAdaptedResource(org.eclipse.core.runtime.IAdaptable)
130          */
131         public IResource getAdaptedResource(IAdaptable adaptable) {
132                 return getResource((IJavaElement) adaptable);
133         }
134
135         private IResource getProject(IJavaElement element) {
136                 return element.getJavaProject().getProject();
137         }
138
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);
146         }
147
148         // private void updateLazyLoadedAdapters() {
149         // if (fSearchPageScoreComputer == null &&
150         // SearchUtil.isSearchPlugInActivated())
151         // createSearchPageScoreComputer();
152         // }
153
154         // private void createSearchPageScoreComputer() {
155         // fSearchPageScoreComputer= new JavaSearchPageScoreComputer();
156         // PROPERTIES= new Class[] {
157         // IPropertySource.class,
158         // IResource.class,
159         // ISearchPageScoreComputer.class,
160         // IWorkbenchAdapter.class,
161         // IResourceLocator.class,
162         // IPersistableElement.class,
163         // IProject.class,
164         // IContributorResourceAdapter.class,
165         // ITaskListResourceAdapter.class,
166         // IContainmentAdapter.class
167         // };
168         // }
169 }