]>
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 | ||
12 | package org.eclipse.jdt.internal.ui.search; | |
13 | ||
14 | import org.eclipse.swt.widgets.Composite; | |
15 | ||
16 | import org.eclipse.jface.action.IMenuManager; | |
17 | import org.eclipse.jface.action.IToolBarManager; | |
18 | import org.eclipse.jface.dialogs.IDialogSettings; | |
19 | import org.eclipse.jface.viewers.ISelection; | |
20 | import org.eclipse.jface.viewers.TableViewer; | |
21 | import org.eclipse.jface.viewers.TreeViewer; | |
22 | import org.eclipse.jface.viewers.Viewer; | |
23 | import org.eclipse.jface.viewers.ViewerComparator; | |
24 | ||
25 | import org.eclipse.jface.text.ITextSelection; | |
26 | ||
27 | import org.eclipse.ui.IActionBars; | |
28 | import org.eclipse.ui.IEditorPart; | |
29 | import org.eclipse.ui.IEditorReference; | |
30 | import org.eclipse.ui.IPartListener2; | |
31 | import org.eclipse.ui.IWorkbenchCommandConstants; | |
32 | import org.eclipse.ui.IWorkbenchPage; | |
33 | import org.eclipse.ui.IWorkbenchPartReference; | |
34 | import org.eclipse.ui.PartInitException; | |
35 | import org.eclipse.ui.part.IPageSite; | |
36 | ||
37 | import org.eclipse.ui.texteditor.ITextEditor; | |
38 | ||
39 | import org.eclipse.search.ui.NewSearchUI; | |
40 | import org.eclipse.search.ui.text.AbstractTextSearchResult; | |
41 | import org.eclipse.search.ui.text.AbstractTextSearchViewPage; | |
42 | import org.eclipse.search.ui.text.Match; | |
43 | ||
44 | import org.eclipse.jdt.core.IJavaElement; | |
45 | import org.eclipse.jdt.core.ITypeRoot; | |
46 | import org.eclipse.jdt.core.JavaModelException; | |
47 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
48 | ||
49 | import org.eclipse.jdt.ui.JavaUI; | |
50 | import org.eclipse.jdt.ui.SharedASTProvider; | |
51 | ||
52 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
53 | import org.eclipse.jdt.internal.ui.actions.AbstractToggleLinkingAction; | |
54 | import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider; | |
55 | import org.eclipse.jdt.internal.ui.viewsupport.ISelectionListenerWithAST; | |
56 | import org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager; | |
57 | ||
58 | ||
59 | public class OccurrencesSearchResultPage extends AbstractTextSearchViewPage { | |
60 | ||
61 | private static final String DIALOGSTORE_LINKEDITORS= "TypeHierarchyViewPart.linkeditors"; //$NON-NLS-1$ | |
62 | ||
63 | private TextSearchTableContentProvider fContentProvider; | |
64 | ||
65 | private boolean fLinkingEnabled; | |
66 | public AbstractToggleLinkingAction fToggleLinkingAction; | |
67 | private LinkWithEditorListener fLinkWithEditorListener; | |
68 | ||
69 | class LinkWithEditorListener implements IPartListener2, ISelectionListenerWithAST { | |
70 | ||
71 | private ITextEditor fActiveEditor; | |
72 | private boolean fIsVisible; | |
73 | ||
74 | public void install(IWorkbenchPage page) { | |
75 | page.addPartListener(this); | |
76 | fIsVisible= page.isPartVisible(getViewPart()); | |
77 | if (fIsVisible) { | |
78 | installOnActiveEditor(page); | |
79 | } | |
80 | } | |
81 | ||
82 | private void installOnActiveEditor(IWorkbenchPage page) { | |
83 | IEditorPart activeEditor= page.getActiveEditor(); | |
84 | if (activeEditor instanceof ITextEditor) { | |
85 | editorActive(activeEditor); | |
86 | ISelection selection= activeEditor.getSite().getSelectionProvider().getSelection(); | |
87 | ITypeRoot typeRoot= JavaUI.getEditorInputTypeRoot(activeEditor.getEditorInput()); | |
88 | if (typeRoot != null && selection instanceof ITextSelection) { | |
89 | CompilationUnit astRoot= SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_ACTIVE_ONLY, null); | |
90 | if (astRoot != null) { | |
91 | preformEditorSelectionChanged((ITextSelection) selection, astRoot); | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | private void uninstallOnActiveEditor() { | |
98 | if (fActiveEditor != null) { | |
99 | SelectionListenerWithASTManager.getDefault().removeListener(fActiveEditor, this); | |
100 | fActiveEditor= null; | |
101 | } | |
102 | } | |
103 | ||
104 | public void uninstall(IWorkbenchPage page) { | |
105 | page.removePartListener(this); | |
106 | uninstallOnActiveEditor(); | |
107 | } | |
108 | ||
109 | public void partActivated(IWorkbenchPartReference partRef) { | |
110 | if (fIsVisible && partRef instanceof IEditorReference) { | |
111 | editorActive(((IEditorReference) partRef).getEditor(true)); | |
112 | } | |
113 | } | |
114 | ||
115 | private void editorActive(IEditorPart editor) { | |
116 | if (editor instanceof ITextEditor) { | |
117 | if (editor != fActiveEditor) { | |
118 | setInput(null, null); | |
119 | } | |
120 | fActiveEditor= (ITextEditor) editor; | |
121 | SelectionListenerWithASTManager.getDefault().addListener(fActiveEditor, this); | |
122 | } | |
123 | } | |
124 | ||
125 | public void partDeactivated(IWorkbenchPartReference partRef) { | |
126 | } | |
127 | ||
128 | public void selectionChanged(IEditorPart part, ITextSelection selection, CompilationUnit astRoot) { | |
129 | preformEditorSelectionChanged(selection, astRoot); | |
130 | } | |
131 | ||
132 | public void partVisible(IWorkbenchPartReference partRef) { | |
133 | if (NewSearchUI.SEARCH_VIEW_ID.equals(partRef.getId()) && partRef.getPart(true) == getViewPart()) { | |
134 | if (fActiveEditor == null) { | |
135 | fIsVisible= true; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | public void partHidden(IWorkbenchPartReference partRef) { | |
141 | if (NewSearchUI.SEARCH_VIEW_ID.equals(partRef.getId()) && partRef.getPart(true) == getViewPart()) { | |
142 | fIsVisible= false; | |
143 | uninstallOnActiveEditor(); | |
144 | } else if (partRef instanceof IEditorReference && partRef.getPart(true) == fActiveEditor) { | |
145 | uninstallOnActiveEditor(); | |
146 | } | |
147 | } | |
148 | ||
149 | public void partBroughtToTop(IWorkbenchPartReference partRef) { | |
150 | } | |
151 | ||
152 | public void partClosed(IWorkbenchPartReference partRef) { | |
153 | if (partRef instanceof IEditorReference && partRef.getPart(true) == fActiveEditor) { | |
154 | uninstallOnActiveEditor(); | |
155 | } | |
156 | } | |
157 | ||
158 | public void partInputChanged(IWorkbenchPartReference partRef) { | |
159 | } | |
160 | ||
161 | public void partOpened(IWorkbenchPartReference partRef) { | |
162 | } | |
163 | ||
164 | public void generated_7586977319482445446(OccurrencesSearchResultPage occurrencessearchresultpage, boolean enabled) { | |
165 | if (enabled) { | |
166 | install(occurrencessearchresultpage.getSite().getPage()); | |
167 | } else { | |
168 | uninstall(occurrencessearchresultpage.getSite().getPage()); | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | private class ToggleLinkingAction extends AbstractToggleLinkingAction { | |
174 | @Override | |
175 | public void run() { | |
176 | setLinkingEnabled(isChecked()); | |
177 | } | |
178 | } | |
179 | ||
180 | public OccurrencesSearchResultPage() { | |
181 | super(AbstractTextSearchViewPage.FLAG_LAYOUT_FLAT); | |
182 | fLinkWithEditorListener= new LinkWithEditorListener(); | |
183 | fLinkingEnabled= false; | |
184 | fToggleLinkingAction= new ToggleLinkingAction(); | |
185 | fToggleLinkingAction.setActionDefinitionId(IWorkbenchCommandConstants.NAVIGATE_TOGGLE_LINK_WITH_EDITOR); | |
186 | } | |
187 | ||
188 | @Override | |
189 | public void init(IPageSite pageSite) { | |
190 | super.init(pageSite); | |
191 | // wait until site is set | |
192 | setLinkingEnabled(getDialogSettings().getBoolean(DIALOGSTORE_LINKEDITORS)); | |
193 | } | |
194 | ||
195 | @Override | |
196 | public void dispose() { | |
197 | if (fLinkingEnabled) { | |
198 | fLinkWithEditorListener.uninstall(getSite().getPage()); | |
199 | } | |
200 | super.dispose(); | |
201 | } | |
202 | ||
203 | @Override | |
204 | protected void fillToolbar(IToolBarManager tbm) { | |
205 | super.fillToolbar(tbm); | |
206 | tbm.add(fToggleLinkingAction); | |
207 | } | |
208 | ||
209 | @Override | |
210 | public void createControl(Composite parent) { | |
211 | super.createControl(parent); | |
212 | ||
213 | IActionBars bars= getSite().getActionBars(); | |
214 | IMenuManager menu= bars.getMenuManager(); | |
215 | fToggleLinkingAction.generated_392288802297276408(this, menu); | |
216 | } | |
217 | ||
218 | /* | |
219 | * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match, int, int) | |
220 | */ | |
221 | @Override | |
222 | protected void showMatch(Match match, int currentOffset, int currentLength, boolean activate) throws PartInitException { | |
223 | JavaElementLine element= (JavaElementLine) match.getElement(); | |
224 | IJavaElement javaElement= element.getJavaElement(); | |
225 | try { | |
226 | IEditorPart editor= JavaUI.openInEditor(javaElement, false, false); | |
227 | if (editor instanceof ITextEditor) { | |
228 | ITextEditor textEditor= (ITextEditor) editor; | |
229 | textEditor.selectAndReveal(currentOffset, currentLength); | |
230 | } | |
231 | // activating at the end avoids an outdated selection event from JavaUI.openInEditor(..): | |
232 | if (editor != null && activate) | |
233 | editor.getEditorSite().getPage().activate(editor); | |
234 | } catch (PartInitException e1) { | |
235 | return; | |
236 | } catch (JavaModelException e1) { | |
237 | return; | |
238 | } | |
239 | ||
240 | } | |
241 | ||
242 | /* | |
243 | * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#elementsChanged(java.lang.Object[]) | |
244 | */ | |
245 | @Override | |
246 | protected void elementsChanged(Object[] objects) { | |
247 | if (fContentProvider != null) | |
248 | fContentProvider.elementsChanged(objects); | |
249 | } | |
250 | ||
251 | /* | |
252 | * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#clear() | |
253 | */ | |
254 | @Override | |
255 | protected void clear() { | |
256 | if (fContentProvider != null) | |
257 | fContentProvider.clear(); | |
258 | } | |
259 | ||
260 | /* | |
261 | * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#configureTreeViewer(org.eclipse.jface.viewers.TreeViewer) | |
262 | */ | |
263 | @Override | |
264 | protected void configureTreeViewer(TreeViewer viewer) { | |
265 | throw new IllegalStateException("Doesn't support tree mode."); //$NON-NLS-1$ | |
266 | } | |
267 | ||
268 | /* | |
269 | * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#configureTableViewer(org.eclipse.jface.viewers.TableViewer) | |
270 | */ | |
271 | @Override | |
272 | protected void configureTableViewer(TableViewer viewer) { | |
273 | viewer.setComparator(new ViewerComparator() { | |
274 | @Override | |
275 | public int compare(Viewer v, Object e1, Object e2) { | |
276 | JavaElementLine jel1= (JavaElementLine) e1; | |
277 | JavaElementLine jel2= (JavaElementLine) e2; | |
278 | return jel1.getLine() - jel2.getLine(); | |
279 | } | |
280 | }); | |
281 | viewer.setLabelProvider(new ColoringLabelProvider(new OccurrencesSearchLabelProvider(this))); | |
282 | fContentProvider= new TextSearchTableContentProvider(); | |
283 | viewer.setContentProvider(fContentProvider); | |
284 | } | |
285 | ||
286 | public boolean isLinkingEnabled() { | |
287 | return fLinkingEnabled; | |
288 | } | |
289 | ||
290 | public void setLinkingEnabled(boolean enabled) { | |
291 | if (fLinkingEnabled != enabled) { | |
292 | fLinkingEnabled= enabled; | |
293 | fToggleLinkingAction.setChecked(enabled); | |
294 | getDialogSettings().put(DIALOGSTORE_LINKEDITORS, enabled); | |
295 | ||
296 | fLinkWithEditorListener.generated_7586977319482445446(this, enabled); | |
297 | } | |
298 | } | |
299 | ||
300 | private IDialogSettings getDialogSettings() { | |
301 | return JavaPlugin.getDefault().getDialogSettingsSection("OccurrencesSearchResultPage"); //$NON-NLS-1$ | |
302 | } | |
303 | ||
304 | ||
305 | private void preformEditorSelectionChanged(ITextSelection selection, CompilationUnit astRoot) { | |
306 | if (!isLinkingEnabled()) { | |
307 | return; | |
308 | } | |
309 | IOccurrencesFinder finder; | |
310 | ||
311 | AbstractTextSearchResult input= getInput(); | |
312 | if (input == null) { | |
313 | finder= new OccurrencesFinder(); | |
314 | } else { | |
315 | String id= ((OccurrencesSearchQuery) input.getQuery()).getFinderId(); | |
316 | if (id == OccurrencesFinder.ID) { | |
317 | finder= new OccurrencesFinder(); | |
318 | } else if (id == ExceptionOccurrencesFinder.ID) { | |
319 | finder= new ExceptionOccurrencesFinder(); | |
320 | } else { | |
321 | finder= new ImplementOccurrencesFinder(); | |
322 | } | |
323 | } | |
324 | ||
325 | int offset= selection.getOffset(); | |
326 | int length= selection.getLength(); | |
327 | if (finder.initialize(astRoot, offset, length) == null) { | |
328 | final OccurrencesSearchQuery query= new OccurrencesSearchQuery(finder, astRoot.getTypeRoot()); | |
329 | query.generated_5361639315803963240(offset, length, this); | |
330 | } | |
331 | } | |
332 | ||
333 | static JavaElementLine getMatchingLine(OccurrencesSearchResult result, int offset, int length) { | |
334 | Object[] elements= result.getElements(); | |
335 | for (int i= 0; i < elements.length; i++) { | |
336 | JavaElementLine line= (JavaElementLine) elements[i]; | |
337 | Match[] matches= result.getMatches(line); | |
338 | for (int j= 0; j < matches.length; j++) { | |
339 | OccurrenceMatch match= (OccurrenceMatch) matches[j]; | |
340 | if (match.getOriginalOffset() <= offset && offset + length <= match.getOriginalOffset() + match.getOriginalLength()) { | |
341 | return line; | |
342 | } | |
343 | } | |
344 | } | |
345 | return null; | |
346 | } | |
347 | } |