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