KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > JavaElementAdapterFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IAdapterFactory;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
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
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 import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter;
30
31 import org.eclipse.ui.ide.IContributorResourceAdapter2;
32
33 import org.eclipse.search.ui.ISearchPageScoreComputer;
34
35 import org.eclipse.team.ui.history.IHistoryPageSource;
36
37 import org.eclipse.jdt.core.ICompilationUnit;
38 import org.eclipse.jdt.core.IJavaElement;
39 import org.eclipse.jdt.core.IPackageFragmentRoot;
40
41 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
42
43 import org.eclipse.jdt.internal.ui.compare.JavaElementHistoryPageSource;
44 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
45 import org.eclipse.jdt.internal.ui.search.JavaSearchPageScoreComputer;
46 import org.eclipse.jdt.internal.ui.search.SearchUtil;
47
48 /**
49  * Implements basic UI support for Java elements.
50  * Implements handle to persistent support for Java elements.
51  */

52 public class JavaElementAdapterFactory implements IAdapterFactory, IContributorResourceAdapter, IContributorResourceAdapter2 {
53     
54     private static Class JavaDoc[] PROPERTIES= new Class JavaDoc[] {
55         IPropertySource.class,
56         IResource.class,
57         IWorkbenchAdapter.class,
58         IResourceLocator.class,
59         IPersistableElement.class,
60         IContributorResourceAdapter.class,
61         IContributorResourceAdapter2.class,
62         ITaskListResourceAdapter.class,
63         IContainmentAdapter.class,
64         IHistoryPageSource.class
65     };
66     
67     /*
68      * Do not use real type since this would cause
69      * the Search plug-in to be loaded.
70      */

71     private Object JavaDoc fSearchPageScoreComputer;
72     private static IResourceLocator fgResourceLocator;
73     private static JavaWorkbenchAdapter fgJavaWorkbenchAdapter;
74     private static ITaskListResourceAdapter fgTaskListAdapter;
75     private static JavaElementContainmentAdapter fgJavaElementContainmentAdapter;
76     
77     public Class JavaDoc[] getAdapterList() {
78         updateLazyLoadedAdapters();
79         return PROPERTIES;
80     }
81     
82     public Object JavaDoc getAdapter(Object JavaDoc element, Class JavaDoc key) {
83         updateLazyLoadedAdapters();
84         IJavaElement java= getJavaElement(element);
85         
86         if (IPropertySource.class.equals(key)) {
87             return getProperties(java);
88         } if (IResource.class.equals(key)) {
89             return getResource(java);
90         } if (fSearchPageScoreComputer != null && ISearchPageScoreComputer.class.equals(key)) {
91             return fSearchPageScoreComputer;
92         } if (IWorkbenchAdapter.class.equals(key)) {
93             return getJavaWorkbenchAdapter();
94         } if (IResourceLocator.class.equals(key)) {
95             return getResourceLocator();
96         } if (IPersistableElement.class.equals(key)) {
97             return new PersistableJavaElementFactory(java);
98         } if (IContributorResourceAdapter.class.equals(key)) {
99             return this;
100         } if (IContributorResourceAdapter2.class.equals(key)) {
101             return this;
102         } if (ITaskListResourceAdapter.class.equals(key)) {
103             return getTaskListAdapter();
104         } if (IContainmentAdapter.class.equals(key)) {
105             return getJavaElementContainmentAdapter();
106         } if (IHistoryPageSource.class.equals(key) && JavaElementHistoryPageSource.hasEdition(java)) {
107             return JavaElementHistoryPageSource.getInstance();
108         }
109         return null;
110     }
111     
112     private IResource getResource(IJavaElement element) {
113         // can't use IJavaElement.getResource directly as we are interested in the
114
// corresponding resource
115
switch (element.getElementType()) {
116             case IJavaElement.TYPE:
117                 // top level types behave like the CU
118
IJavaElement parent= element.getParent();
119                 if (parent instanceof ICompilationUnit) {
120                     return ((ICompilationUnit) parent).getPrimary().getResource();
121                 }
122                 return null;
123             case IJavaElement.COMPILATION_UNIT:
124                 return ((ICompilationUnit) element).getPrimary().getResource();
125             case IJavaElement.CLASS_FILE:
126             case IJavaElement.PACKAGE_FRAGMENT:
127                 // test if in a archive
128
IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
129                 if (!root.isArchive()) {
130                     return element.getResource();
131                 }
132                 return null;
133             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
134             case IJavaElement.JAVA_PROJECT:
135             case IJavaElement.JAVA_MODEL:
136                 return element.getResource();
137             default:
138                 return null;
139         }
140     }
141
142     public IResource getAdaptedResource(IAdaptable adaptable) {
143         IJavaElement je= getJavaElement(adaptable);
144         if (je != null)
145             return getResource(je);
146
147         return null;
148     }
149     
150     public ResourceMapping getAdaptedResourceMapping(IAdaptable adaptable) {
151         IJavaElement je= getJavaElement(adaptable);
152         if (je != null)
153             return JavaElementResourceMapping.create(je);
154
155         return null;
156     }
157     
158     private IJavaElement getJavaElement(Object JavaDoc element) {
159         if (element instanceof IJavaElement)
160             return (IJavaElement)element;
161         if (element instanceof IClassFileEditorInput)
162             return ((IClassFileEditorInput)element).getClassFile().getPrimaryElement();
163
164         return null;
165     }
166     
167     private IPropertySource getProperties(IJavaElement element) {
168         IResource resource= getResource(element);
169         if (resource == null)
170             return new JavaElementProperties(element);
171         if (resource.getType() == IResource.FILE)
172             return new FilePropertySource((IFile) resource);
173         return new ResourcePropertySource(resource);
174     }
175
176     private void updateLazyLoadedAdapters() {
177         if (fSearchPageScoreComputer == null && SearchUtil.isSearchPlugInActivated())
178             createSearchPageScoreComputer();
179     }
180
181     private void createSearchPageScoreComputer() {
182         fSearchPageScoreComputer= new JavaSearchPageScoreComputer();
183         PROPERTIES= new Class JavaDoc[] {
184             IPropertySource.class,
185             IResource.class,
186             ISearchPageScoreComputer.class,
187             IWorkbenchAdapter.class,
188             IResourceLocator.class,
189             IPersistableElement.class,
190             IProject.class,
191             IContributorResourceAdapter.class,
192             IContributorResourceAdapter2.class,
193             ITaskListResourceAdapter.class,
194             IContainmentAdapter.class
195         };
196     }
197
198     private static IResourceLocator getResourceLocator() {
199         if (fgResourceLocator == null)
200             fgResourceLocator= new ResourceLocator();
201         return fgResourceLocator;
202     }
203     
204     private static JavaWorkbenchAdapter getJavaWorkbenchAdapter() {
205         if (fgJavaWorkbenchAdapter == null)
206             fgJavaWorkbenchAdapter= new JavaWorkbenchAdapter();
207         return fgJavaWorkbenchAdapter;
208     }
209
210     private static ITaskListResourceAdapter getTaskListAdapter() {
211         if (fgTaskListAdapter == null)
212             fgTaskListAdapter= new JavaTaskListAdapter();
213         return fgTaskListAdapter;
214     }
215
216     private static JavaElementContainmentAdapter getJavaElementContainmentAdapter() {
217         if (fgJavaElementContainmentAdapter == null)
218             fgJavaElementContainmentAdapter= new JavaElementContainmentAdapter();
219         return fgJavaElementContainmentAdapter;
220     }
221 }
222
Popular Tags