]>
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.browsing; | |
12 | ||
13 | import java.util.Arrays; | |
14 | import java.util.List; | |
15 | ||
16 | import org.eclipse.core.runtime.Assert; | |
17 | ||
18 | import org.eclipse.jface.viewers.ISelection; | |
19 | import org.eclipse.jface.viewers.IStructuredSelection; | |
20 | import org.eclipse.jface.viewers.StructuredSelection; | |
21 | import org.eclipse.jface.viewers.TableViewer; | |
22 | ||
23 | import org.eclipse.ui.IActionBars; | |
24 | import org.eclipse.ui.IWorkbenchPart; | |
25 | import org.eclipse.ui.actions.ActionFactory; | |
26 | import org.eclipse.ui.part.IShowInTargetList; | |
27 | ||
28 | import org.eclipse.jdt.core.IClassFile; | |
29 | import org.eclipse.jdt.core.ICompilationUnit; | |
30 | import org.eclipse.jdt.core.IJavaElement; | |
31 | import org.eclipse.jdt.core.IMember; | |
32 | import org.eclipse.jdt.core.IPackageFragment; | |
33 | import org.eclipse.jdt.core.IType; | |
34 | ||
35 | import org.eclipse.jdt.ui.JavaElementLabels; | |
36 | import org.eclipse.jdt.ui.JavaUI; | |
37 | import org.eclipse.jdt.ui.PreferenceConstants; | |
38 | ||
39 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
40 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
41 | import org.eclipse.jdt.internal.ui.actions.SelectAllAction; | |
42 | import org.eclipse.jdt.internal.ui.filters.NonJavaElementFilter; | |
43 | import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; | |
44 | import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; | |
45 | import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; | |
46 | ||
47 | public class TypesView extends JavaBrowsingPart { | |
48 | ||
49 | private SelectAllAction fSelectAllAction; | |
50 | ||
51 | /** | |
52 | * Creates and returns the label provider for this part. | |
53 | * | |
54 | * @return the label provider | |
55 | * @see org.eclipse.jface.viewers.ILabelProvider | |
56 | */ | |
57 | @Override | |
58 | protected JavaUILabelProvider createLabelProvider() { | |
59 | return new AppearanceAwareLabelProvider( | |
60 | AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.T_CATEGORY, | |
61 | AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS); | |
62 | } | |
63 | ||
64 | /* (non-Javadoc) | |
65 | * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class) | |
66 | */ | |
67 | @Override | |
68 | public Object getAdapter(Class key) { | |
69 | if (key == IShowInTargetList.class) { | |
70 | return new IShowInTargetList() { | |
71 | public String[] getShowInTargetIds() { | |
72 | return new String[] { JavaUI.ID_PACKAGES, JavaPlugin.ID_RES_NAV }; | |
73 | } | |
74 | ||
75 | }; | |
76 | } | |
77 | return super.getAdapter(key); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Adds filters the viewer of this part. | |
82 | */ | |
83 | @Override | |
84 | protected void addFilters() { | |
85 | super.addFilters(); | |
86 | getViewer().addFilter(new NonJavaElementFilter()); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Answers if the given <code>element</code> is a valid | |
91 | * input for this part. | |
92 | * | |
93 | * @param element the object to test | |
94 | * @return <true> if the given element is a valid input | |
95 | */ | |
96 | @Override | |
97 | protected boolean isValidInput(Object element) { | |
98 | return element instanceof IPackageFragment; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Answers if the given <code>element</code> is a valid | |
103 | * element for this part. | |
104 | * | |
105 | * @param element the object to test | |
106 | * @return <true> if the given element is a valid element | |
107 | */ | |
108 | @Override | |
109 | protected boolean isValidElement(Object element) { | |
110 | if (element instanceof ICompilationUnit) | |
111 | return super.isValidElement(((ICompilationUnit)element).getParent()); | |
112 | else if (element instanceof IType) { | |
113 | IType type= (IType)element; | |
114 | return type.getDeclaringType() == null && isValidElement(type.getCompilationUnit()); | |
115 | } | |
116 | return false; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Finds the element which has to be selected in this part. | |
121 | * | |
122 | * @param je the Java element which has the focus | |
123 | * @return the element to select | |
124 | */ | |
125 | @Override | |
126 | protected IJavaElement findElementToSelect(IJavaElement je) { | |
127 | if (je == null) | |
128 | return null; | |
129 | ||
130 | switch (je.getElementType()) { | |
131 | case IJavaElement.TYPE: | |
132 | IType type= ((IType)je).getDeclaringType(); | |
133 | if (type == null) | |
134 | type= (IType)je; | |
135 | return type; | |
136 | case IJavaElement.COMPILATION_UNIT: | |
137 | return getTypeForCU((ICompilationUnit)je); | |
138 | case IJavaElement.CLASS_FILE: | |
139 | return findElementToSelect(((IClassFile)je).getType()); | |
140 | case IJavaElement.IMPORT_CONTAINER: | |
141 | case IJavaElement.IMPORT_DECLARATION: | |
142 | case IJavaElement.PACKAGE_DECLARATION: | |
143 | return findElementToSelect(je.getParent()); | |
144 | default: | |
145 | if (je instanceof IMember) | |
146 | return findElementToSelect(((IMember)je).getDeclaringType()); | |
147 | return null; | |
148 | ||
149 | } | |
150 | } | |
151 | ||
152 | /** | |
153 | * Returns the context ID for the Help system | |
154 | * | |
155 | * @return the string used as ID for the Help context | |
156 | */ | |
157 | @Override | |
158 | protected String getHelpContextId() { | |
159 | return IJavaHelpContextIds.TYPES_VIEW; | |
160 | } | |
161 | ||
162 | @Override | |
163 | protected String getLinkToEditorKey() { | |
164 | return PreferenceConstants.LINK_BROWSING_TYPES_TO_EDITOR; | |
165 | } | |
166 | ||
167 | @Override | |
168 | protected void createActions() { | |
169 | super.createActions(); | |
170 | fSelectAllAction= new SelectAllAction((TableViewer)getViewer()); | |
171 | } | |
172 | ||
173 | @Override | |
174 | protected void fillActionBars(IActionBars actionBars) { | |
175 | super.fillActionBars(actionBars); | |
176 | ||
177 | // Add selectAll action handlers. | |
178 | actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction); | |
179 | } | |
180 | ||
181 | /** | |
182 | * Handles selection of LogicalPackage in Packages view. | |
183 | * | |
184 | * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection) | |
185 | * @since 2.1 | |
186 | */ | |
187 | @Override | |
188 | public void selectionChanged(IWorkbenchPart part, ISelection selection) { | |
189 | if (!needsToProcessSelectionChanged(part)) | |
190 | return; | |
191 | ||
192 | if (selection instanceof IStructuredSelection) { | |
193 | IStructuredSelection sel= (IStructuredSelection) selection; | |
194 | Object selectedElement= sel.getFirstElement(); | |
195 | if (sel.size() == 1 && (selectedElement instanceof LogicalPackage)) { | |
196 | IPackageFragment[] fragments= ((LogicalPackage)selectedElement).getFragments(); | |
197 | List<IPackageFragment> selectedElements= Arrays.asList(fragments); | |
198 | if (selectedElements.size() > 1) { | |
199 | adjustInput(selectedElements); | |
200 | fPreviousSelectedElement= selectedElements; | |
201 | fPreviousSelectionProvider= part; | |
202 | } else if (selectedElements.size() == 1) | |
203 | super.selectionChanged(part, new StructuredSelection(selectedElements.get(0))); | |
204 | else | |
205 | Assert.isLegal(false); | |
206 | return; | |
207 | } | |
208 | } | |
209 | super.selectionChanged(part, selection); | |
210 | } | |
211 | ||
212 | private void adjustInput(List<IPackageFragment> selectedElements) { | |
213 | Object currentInput= getViewer().getInput(); | |
214 | if (!selectedElements.equals(currentInput)) | |
215 | setInput(selectedElements); | |
216 | } | |
217 | /* | |
218 | * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createDecoratingLabelProvider(org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider) | |
219 | */ | |
220 | @Override | |
221 | protected DecoratingJavaLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) { | |
222 | DecoratingJavaLabelProvider decoratingLabelProvider= super.createDecoratingLabelProvider(provider); | |
223 | provider.addLabelDecorator(new TopLevelTypeProblemsLabelDecorator(null)); | |
224 | return decoratingLabelProvider; | |
225 | } | |
226 | ||
227 | } |