improved include detection
[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
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IAdapterFactory;
20
21 import org.eclipse.ui.IContainmentAdapter;
22 import org.eclipse.ui.IContributorResourceAdapter;
23 import org.eclipse.ui.IPersistableElement;
24 import org.eclipse.ui.model.IWorkbenchAdapter;
25 import org.eclipse.ui.views.properties.FilePropertySource;
26 import org.eclipse.ui.views.properties.IPropertySource;
27 import org.eclipse.ui.views.properties.ResourcePropertySource;
28 import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter;
29
30 import org.eclipse.search.ui.ISearchPageScoreComputer;
31
32 import net.sourceforge.phpdt.core.ICompilationUnit;
33 import net.sourceforge.phpdt.core.IJavaElement;
34 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
35
36 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
37 //import net.sourceforge.phpdt.internal.ui.search.JavaSearchPageScoreComputer;
38 //import net.sourceforge.phpdt.internal.ui.search.SearchUtil;
39
40 /**
41  * Implements basic UI support for Java elements.
42  * Implements handle to persistent support for Java elements.
43  */
44 public class JavaElementAdapterFactory implements IAdapterFactory, IContributorResourceAdapter{
45         
46         private static Class[] PROPERTIES= new Class[] {
47                 IPropertySource.class,
48                 IResource.class,
49 //              IWorkbenchAdapter.class,
50 //              IResourceLocator.class,
51                 IPersistableElement.class,
52                 IProject.class,
53                 IContributorResourceAdapter.class,
54 //              ITaskListResourceAdapter.class,
55 //              IContainmentAdapter.class
56         };
57         
58 //      private Object fSearchPageScoreComputer;
59 //      private static IResourceLocator fgResourceLocator= new ResourceLocator();
60 //      private static JavaWorkbenchAdapter fgJavaWorkbenchAdapter= new JavaWorkbenchAdapter();
61 //      private static ITaskListResourceAdapter fgTaskListAdapter= new JavaTaskListAdapter();
62 //      private static JavaElementContainmentAdapter fgJavaElementContainmentAdapter= new JavaElementContainmentAdapter();
63         
64         public Class[] getAdapterList() {
65 //              updateLazyLoadedAdapters();
66                 return PROPERTIES;
67         }
68         
69         public Object getAdapter(Object element, Class key) {
70 //              updateLazyLoadedAdapters();
71                 IJavaElement java= (IJavaElement) element;
72                 
73                 if (IPropertySource.class.equals(key)) {
74                         return getProperties(java);
75                 } if (IResource.class.equals(key)) {
76                         return getResource(java);
77                 } if (IProject.class.equals(key)) {
78                         return getProject(java);
79 //              } if (fSearchPageScoreComputer != null && ISearchPageScoreComputer.class.equals(key)) {
80 //                      return fSearchPageScoreComputer;
81 //              } if (IWorkbenchAdapter.class.equals(key)) {
82 //                      return fgJavaWorkbenchAdapter;
83 //              } if (IResourceLocator.class.equals(key)) {
84 //                      return fgResourceLocator;
85 //              } if (IPersistableElement.class.equals(key)) {
86 //                      return new PersistableJavaElementFactory(java);
87                 } if (IContributorResourceAdapter.class.equals(key)) {
88                         return this;
89 //              } if (ITaskListResourceAdapter.class.equals(key)) {
90 //                      return fgTaskListAdapter;
91 //              } if (IContainmentAdapter.class.equals(key)) {
92 //                      return fgJavaElementContainmentAdapter;
93                 }
94                 return null; 
95         }
96         
97         private IResource getResource(IJavaElement element) {
98                 // can't use IJavaElement.getResource directly as we are interrested in the
99                 // corresponding resource
100                 switch (element.getElementType()) {
101                         case IJavaElement.TYPE:
102                                 // top level types behave like the CU
103                                 IJavaElement parent= element.getParent();
104                                 if (parent instanceof ICompilationUnit) {
105                                         return JavaModelUtil.toOriginal((ICompilationUnit) parent).getResource();
106                                 }
107                                 return null;
108                         case IJavaElement.COMPILATION_UNIT:
109                                 return JavaModelUtil.toOriginal((ICompilationUnit) element).getResource();
110                         case IJavaElement.CLASS_FILE:
111                         case IJavaElement.PACKAGE_FRAGMENT:
112                                 // test if in a archive
113                                 IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
114                                 if (!root.isArchive()) {
115                                         return element.getResource();
116                                 }
117                                 return null;
118                         case IJavaElement.PACKAGE_FRAGMENT_ROOT:
119                         case IJavaElement.JAVA_PROJECT:
120                         case IJavaElement.JAVA_MODEL:
121                                 return element.getResource();
122                         default:
123                                 return null;
124                 }               
125     }
126
127     /*
128      * @see org.eclipse.ui.IContributorResourceAdapter#getAdaptedResource(org.eclipse.core.runtime.IAdaptable)
129      */
130     public IResource getAdaptedResource(IAdaptable adaptable) {
131         return getResource((IJavaElement)adaptable);
132     }
133         
134         private IResource getProject(IJavaElement element) {
135                 return element.getJavaProject().getProject();
136         }
137
138         private IPropertySource getProperties(IJavaElement element) {
139                 IResource resource= getResource(element);
140                 if (resource == null)
141 //                      return new JavaElementProperties(element);
142                 if (resource.getType() == IResource.FILE)
143                         return new FilePropertySource((IFile) resource);
144                 return new ResourcePropertySource(resource);
145         }
146
147 //      private void updateLazyLoadedAdapters() {
148 //              if (fSearchPageScoreComputer == null && SearchUtil.isSearchPlugInActivated())
149 //                      createSearchPageScoreComputer();
150 //      }
151
152 //      private void createSearchPageScoreComputer() {
153 //              fSearchPageScoreComputer= new JavaSearchPageScoreComputer();
154 //              PROPERTIES= new Class[] {
155 //                      IPropertySource.class,
156 //                      IResource.class,
157 //                      ISearchPageScoreComputer.class,
158 //                      IWorkbenchAdapter.class,
159 //                      IResourceLocator.class,
160 //                      IPersistableElement.class,
161 //                      IProject.class,
162 //                      IContributorResourceAdapter.class,
163 //                      ITaskListResourceAdapter.class,
164 //                      IContainmentAdapter.class
165 //              };
166 //      }
167 }