]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.osgi.framework.Bundle; | |
14 | ||
15 | import org.eclipse.team.ui.history.IHistoryPageSource; | |
16 | ||
17 | import org.eclipse.core.runtime.IAdaptable; | |
18 | import org.eclipse.core.runtime.IAdapterFactory; | |
19 | import org.eclipse.core.runtime.Platform; | |
20 | ||
21 | import org.eclipse.core.resources.IFile; | |
22 | import org.eclipse.core.resources.IResource; | |
23 | import org.eclipse.core.resources.mapping.ResourceMapping; | |
24 | ||
25 | import org.eclipse.ui.IContainmentAdapter; | |
26 | import org.eclipse.ui.IContributorResourceAdapter; | |
27 | import org.eclipse.ui.IPersistableElement; | |
28 | import org.eclipse.ui.ide.IContributorResourceAdapter2; | |
29 | import org.eclipse.ui.model.IWorkbenchAdapter; | |
30 | import org.eclipse.ui.views.properties.FilePropertySource; | |
31 | import org.eclipse.ui.views.properties.IPropertySource; | |
32 | import org.eclipse.ui.views.properties.ResourcePropertySource; | |
33 | import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter; | |
34 | ||
35 | import org.eclipse.search.ui.ISearchPageScoreComputer; | |
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 | /** | |
50 | * Implements basic UI support for Java elements. | |
51 | * Implements handle to persistent support for Java elements. | |
52 | */ | |
53 | public class JavaElementAdapterFactory implements IAdapterFactory, IContributorResourceAdapter2 { | |
54 | ||
55 | private static Class<?>[] ADAPTER_LIST= new Class[] { | |
56 | IPropertySource.class, | |
57 | IResource.class, | |
58 | IWorkbenchAdapter.class, | |
59 | IResourceLocator.class, | |
60 | IPersistableElement.class, | |
61 | IContributorResourceAdapter.class, | |
62 | IContributorResourceAdapter2.class, | |
63 | ITaskListResourceAdapter.class, | |
64 | IContainmentAdapter.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 fSearchPageScoreComputer; | |
72 | ||
73 | private boolean fIsTeamUILoaded; | |
74 | private static IResourceLocator fgResourceLocator; | |
75 | private static JavaWorkbenchAdapter fgJavaWorkbenchAdapter; | |
76 | private static ITaskListResourceAdapter fgTaskListAdapter; | |
77 | private static JavaElementContainmentAdapter fgJavaElementContainmentAdapter; | |
78 | ||
79 | public Class[] getAdapterList() { | |
80 | updateLazyLoadedAdapters(); | |
81 | return ADAPTER_LIST; | |
82 | } | |
83 | ||
84 | public Object getAdapter(Object element, Class key) { | |
85 | updateLazyLoadedAdapters(); | |
86 | IJavaElement java= getJavaElement(element); | |
87 | ||
88 | if (IPropertySource.class.equals(key)) { | |
89 | return getProperties(java); | |
90 | } if (IResource.class.equals(key)) { | |
91 | return getResource(java); | |
92 | } if (fSearchPageScoreComputer != null && ISearchPageScoreComputer.class.equals(key)) { | |
93 | return fSearchPageScoreComputer; | |
94 | } if (IWorkbenchAdapter.class.equals(key)) { | |
95 | return getJavaWorkbenchAdapter(); | |
96 | } if (IResourceLocator.class.equals(key)) { | |
97 | return getResourceLocator(); | |
98 | } if (IPersistableElement.class.equals(key)) { | |
99 | return new PersistableJavaElementFactory(java); | |
100 | } if (IContributorResourceAdapter.class.equals(key)) { | |
101 | return this; | |
102 | } if (IContributorResourceAdapter2.class.equals(key)) { | |
103 | return this; | |
104 | } if (ITaskListResourceAdapter.class.equals(key)) { | |
105 | return getTaskListAdapter(); | |
106 | } if (IContainmentAdapter.class.equals(key)) { | |
107 | return getJavaElementContainmentAdapter(); | |
108 | } if (fIsTeamUILoaded && IHistoryPageSource.class.equals(key) && JavaElementHistoryPageSource.hasEdition(java)) { | |
109 | return JavaElementHistoryPageSource.getInstance(); | |
110 | } | |
111 | return null; | |
112 | } | |
113 | ||
114 | private IResource getResource(IJavaElement element) { | |
115 | // can't use IJavaElement.getResource directly as we are interested in the | |
116 | // corresponding resource | |
117 | switch (element.getElementType()) { | |
118 | case IJavaElement.TYPE: | |
119 | // top level types behave like the CU | |
120 | IJavaElement parent= element.getParent(); | |
121 | if (parent instanceof ICompilationUnit) { | |
122 | return ((ICompilationUnit) parent).getPrimary().getResource(); | |
123 | } | |
124 | return null; | |
125 | case IJavaElement.COMPILATION_UNIT: | |
126 | return ((ICompilationUnit) element).getPrimary().getResource(); | |
127 | case IJavaElement.CLASS_FILE: | |
128 | case IJavaElement.PACKAGE_FRAGMENT: | |
129 | // test if in a archive | |
130 | IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); | |
131 | if (!root.isArchive() && !root.isExternal()) { | |
132 | return element.getResource(); | |
133 | } | |
134 | return null; | |
135 | case IJavaElement.PACKAGE_FRAGMENT_ROOT: | |
136 | case IJavaElement.JAVA_PROJECT: | |
137 | case IJavaElement.JAVA_MODEL: | |
138 | return element.getResource(); | |
139 | default: | |
140 | return null; | |
141 | } | |
142 | } | |
143 | ||
144 | public IResource getAdaptedResource(IAdaptable adaptable) { | |
145 | IJavaElement je= getJavaElement(adaptable); | |
146 | if (je != null) | |
147 | return getResource(je); | |
148 | ||
149 | return null; | |
150 | } | |
151 | ||
152 | public ResourceMapping getAdaptedResourceMapping(IAdaptable adaptable) { | |
153 | IJavaElement je= getJavaElement(adaptable); | |
154 | if (je != null) | |
155 | return JavaElementResourceMapping.create(je); | |
156 | ||
157 | return null; | |
158 | } | |
159 | ||
160 | private IJavaElement getJavaElement(Object element) { | |
161 | if (element instanceof IJavaElement) | |
162 | return (IJavaElement)element; | |
163 | if (element instanceof IClassFileEditorInput) | |
164 | return ((IClassFileEditorInput)element).getClassFile().getPrimaryElement(); | |
165 | ||
166 | return null; | |
167 | } | |
168 | ||
169 | private IPropertySource getProperties(IJavaElement element) { | |
170 | IResource resource= getResource(element); | |
171 | if (resource == null) | |
172 | return new JavaElementProperties(element); | |
173 | if (resource.getType() == IResource.FILE) | |
174 | return new FilePropertySource((IFile) resource); | |
175 | return new ResourcePropertySource(resource); | |
176 | } | |
177 | ||
178 | private void updateLazyLoadedAdapters() { | |
179 | if (fSearchPageScoreComputer == null && SearchUtil.isSearchPlugInActivated()) | |
180 | createSearchPageScoreComputer(); | |
181 | if (!fIsTeamUILoaded && isTeamUIPlugInActivated()) { | |
182 | addClassToAdapterList(IHistoryPageSource.class); | |
183 | fIsTeamUILoaded= true; | |
184 | } | |
185 | } | |
186 | ||
187 | private void createSearchPageScoreComputer() { | |
188 | fSearchPageScoreComputer= new JavaSearchPageScoreComputer(); | |
189 | addClassToAdapterList(ISearchPageScoreComputer.class); | |
190 | } | |
191 | ||
192 | private static void addClassToAdapterList(Class<?> clazz) { | |
193 | int oldSize= ADAPTER_LIST.length; | |
194 | Class<?>[] oldProperties= ADAPTER_LIST; | |
195 | ADAPTER_LIST= new Class[oldSize + 1]; | |
196 | System.arraycopy(oldProperties, 0, ADAPTER_LIST, 0, oldSize); | |
197 | ADAPTER_LIST[oldSize]= clazz; | |
198 | } | |
199 | ||
200 | private static boolean isTeamUIPlugInActivated() { | |
201 | return Platform.getBundle("org.eclipse.team.ui").getState() == Bundle.ACTIVE; //$NON-NLS-1$ | |
202 | } | |
203 | ||
204 | private static IResourceLocator getResourceLocator() { | |
205 | if (fgResourceLocator == null) | |
206 | fgResourceLocator= new ResourceLocator(); | |
207 | return fgResourceLocator; | |
208 | } | |
209 | ||
210 | private static JavaWorkbenchAdapter getJavaWorkbenchAdapter() { | |
211 | if (fgJavaWorkbenchAdapter == null) | |
212 | fgJavaWorkbenchAdapter= new JavaWorkbenchAdapter(); | |
213 | return fgJavaWorkbenchAdapter; | |
214 | } | |
215 | ||
216 | private static ITaskListResourceAdapter getTaskListAdapter() { | |
217 | if (fgTaskListAdapter == null) | |
218 | fgTaskListAdapter= new JavaTaskListAdapter(); | |
219 | return fgTaskListAdapter; | |
220 | } | |
221 | ||
222 | private static JavaElementContainmentAdapter getJavaElementContainmentAdapter() { | |
223 | if (fgJavaElementContainmentAdapter == null) | |
224 | fgJavaElementContainmentAdapter= new JavaElementContainmentAdapter(); | |
225 | return fgJavaElementContainmentAdapter; | |
226 | } | |
227 | } |