Lines 14-21
Link Here
|
14 |
|
14 |
|
15 |
import java.lang.reflect.InvocationTargetException; |
15 |
import java.lang.reflect.InvocationTargetException; |
16 |
|
16 |
|
17 |
import org.eclipse.swt.SWT; |
|
|
18 |
|
19 |
import org.eclipse.core.runtime.CoreException; |
17 |
import org.eclipse.core.runtime.CoreException; |
20 |
import org.eclipse.core.runtime.IProgressMonitor; |
18 |
import org.eclipse.core.runtime.IProgressMonitor; |
21 |
|
19 |
|
Lines 25-36
Link Here
|
25 |
import org.eclipse.core.resources.IResource; |
23 |
import org.eclipse.core.resources.IResource; |
26 |
import org.eclipse.core.resources.IStorage; |
24 |
import org.eclipse.core.resources.IStorage; |
27 |
|
25 |
|
|
|
26 |
import org.eclipse.swt.SWT; |
27 |
|
28 |
import org.eclipse.jface.action.Action; |
28 |
import org.eclipse.jface.action.Action; |
29 |
import org.eclipse.jface.action.IAction; |
29 |
import org.eclipse.jface.action.IAction; |
30 |
import org.eclipse.jface.viewers.ISelectionProvider; |
30 |
import org.eclipse.jface.viewers.ISelectionProvider; |
31 |
|
31 |
|
32 |
import org.eclipse.jface.text.Assert; |
32 |
import org.eclipse.jface.text.Assert; |
33 |
import org.eclipse.jface.text.IRegion; |
33 |
import org.eclipse.jface.text.IRegion; |
|
|
34 |
import org.eclipse.jface.text.ITextSelection; |
34 |
import org.eclipse.jface.text.TextSelection; |
35 |
import org.eclipse.jface.text.TextSelection; |
35 |
|
36 |
|
36 |
import org.eclipse.ui.IEditorDescriptor; |
37 |
import org.eclipse.ui.IEditorDescriptor; |
Lines 41-59
Link Here
|
41 |
import org.eclipse.ui.IWorkbenchPage; |
42 |
import org.eclipse.ui.IWorkbenchPage; |
42 |
import org.eclipse.ui.PartInitException; |
43 |
import org.eclipse.ui.PartInitException; |
43 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
44 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
44 |
import org.eclipse.ui.ide.IDE; |
|
|
45 |
import org.eclipse.ui.ide.IGotoMarker; |
46 |
import org.eclipse.ui.part.FileEditorInput; |
45 |
import org.eclipse.ui.part.FileEditorInput; |
47 |
import org.eclipse.ui.texteditor.ITextEditor; |
46 |
import org.eclipse.ui.texteditor.ITextEditor; |
48 |
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; |
47 |
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; |
49 |
import org.eclipse.ui.texteditor.TextEditorAction; |
48 |
import org.eclipse.ui.texteditor.TextEditorAction; |
50 |
|
49 |
|
|
|
50 |
import org.eclipse.ui.ide.IDE; |
51 |
import org.eclipse.ui.ide.IGotoMarker; |
52 |
|
51 |
import org.eclipse.jdt.core.IClassFile; |
53 |
import org.eclipse.jdt.core.IClassFile; |
52 |
import org.eclipse.jdt.core.ICompilationUnit; |
54 |
import org.eclipse.jdt.core.ICompilationUnit; |
|
|
55 |
import org.eclipse.jdt.core.IImportDeclaration; |
53 |
import org.eclipse.jdt.core.IJavaElement; |
56 |
import org.eclipse.jdt.core.IJavaElement; |
54 |
import org.eclipse.jdt.core.IJavaProject; |
57 |
import org.eclipse.jdt.core.IJavaProject; |
55 |
import org.eclipse.jdt.core.ILocalVariable; |
58 |
import org.eclipse.jdt.core.ILocalVariable; |
56 |
import org.eclipse.jdt.core.IMember; |
59 |
import org.eclipse.jdt.core.IMember; |
|
|
60 |
import org.eclipse.jdt.core.IPackageDeclaration; |
57 |
import org.eclipse.jdt.core.ISourceRange; |
61 |
import org.eclipse.jdt.core.ISourceRange; |
58 |
import org.eclipse.jdt.core.ISourceReference; |
62 |
import org.eclipse.jdt.core.ISourceReference; |
59 |
import org.eclipse.jdt.core.ITypeParameter; |
63 |
import org.eclipse.jdt.core.ITypeParameter; |
Lines 63-68
Link Here
|
63 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
67 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
64 |
import org.eclipse.jdt.internal.corext.util.Messages; |
68 |
import org.eclipse.jdt.internal.corext.util.Messages; |
65 |
|
69 |
|
|
|
70 |
import org.eclipse.jdt.ui.PreferenceConstants; |
71 |
|
66 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
72 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
67 |
|
73 |
|
68 |
/** |
74 |
/** |
Lines 128-138
Link Here
|
128 |
return openInEditor((IFile) inputElement, activate); |
134 |
return openInEditor((IFile) inputElement, activate); |
129 |
|
135 |
|
130 |
IEditorInput input= getEditorInput(inputElement); |
136 |
IEditorInput input= getEditorInput(inputElement); |
131 |
if (input instanceof IFileEditorInput) { |
|
|
132 |
IFileEditorInput fileInput= (IFileEditorInput) input; |
133 |
return openInEditor(fileInput.getFile(), activate); |
134 |
} |
135 |
|
136 |
if (input != null) |
137 |
if (input != null) |
137 |
return openInEditor(input, getEditorID(input, inputElement), activate); |
138 |
return openInEditor(input, getEditorID(input, inputElement), activate); |
138 |
|
139 |
|
Lines 268-274
Link Here
|
268 |
private static void initializeHighlightRange(IEditorPart editorPart) { |
269 |
private static void initializeHighlightRange(IEditorPart editorPart) { |
269 |
if (editorPart instanceof ITextEditor) { |
270 |
if (editorPart instanceof ITextEditor) { |
270 |
IAction toggleAction= editorPart.getEditorSite().getActionBars().getGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY); |
271 |
IAction toggleAction= editorPart.getEditorSite().getActionBars().getGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY); |
271 |
if (toggleAction != null && toggleAction.isEnabled() && toggleAction.isChecked()) { |
272 |
boolean enable= toggleAction != null; |
|
|
273 |
if (enable && editorPart instanceof JavaEditor) |
274 |
enable= JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS); |
275 |
else |
276 |
enable= enable && toggleAction.isEnabled() && toggleAction.isChecked(); |
277 |
if (enable) { |
272 |
if (toggleAction instanceof TextEditorAction) { |
278 |
if (toggleAction instanceof TextEditorAction) { |
273 |
// Reset the action |
279 |
// Reset the action |
274 |
((TextEditorAction)toggleAction).setEditor(null); |
280 |
((TextEditorAction)toggleAction).setEditor(null); |
Lines 301-317
Link Here
|
301 |
return null; |
307 |
return null; |
302 |
} |
308 |
} |
303 |
|
309 |
|
304 |
private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException { |
310 |
private static IEditorInput getEditorInput(IJavaElement je) throws JavaModelException { |
|
|
311 |
final ITextSelection textSelection= getTextSelection(je); |
312 |
IJavaElement element= je; |
305 |
while (element != null) { |
313 |
while (element != null) { |
306 |
if (element instanceof ICompilationUnit) { |
314 |
if (element instanceof ICompilationUnit) { |
307 |
ICompilationUnit unit= JavaModelUtil.toOriginal((ICompilationUnit) element); |
315 |
ICompilationUnit unit= JavaModelUtil.toOriginal((ICompilationUnit) element); |
308 |
IResource resource= unit.getResource(); |
316 |
IResource resource= unit.getResource(); |
309 |
if (resource instanceof IFile) |
317 |
if (resource instanceof IFile) { |
310 |
return new FileEditorInput((IFile) resource); |
318 |
return new FileEditorInput((IFile) resource) { |
|
|
319 |
public Object getAdapter(Class adapter) { |
320 |
if (adapter == ITextSelection.class) |
321 |
return textSelection; |
322 |
return super.getAdapter(adapter); |
323 |
} |
324 |
}; |
325 |
} |
311 |
} |
326 |
} |
312 |
|
327 |
|
313 |
if (element instanceof IClassFile) |
328 |
if (element instanceof IClassFile) { |
314 |
return new InternalClassFileEditorInput((IClassFile) element); |
329 |
if (textSelection != null) |
|
|
330 |
return new InternalClassFileEditorInput((IClassFile) element, textSelection); |
331 |
else |
332 |
return new InternalClassFileEditorInput((IClassFile) element); |
333 |
} |
315 |
|
334 |
|
316 |
element= element.getParent(); |
335 |
element= element.getParent(); |
317 |
} |
336 |
} |
Lines 319-324
Link Here
|
319 |
return null; |
338 |
return null; |
320 |
} |
339 |
} |
321 |
|
340 |
|
|
|
341 |
/** |
342 |
* Returns the text selection for the given Java element. |
343 |
* |
344 |
* @param je the Java element |
345 |
* @return the text selection or <code>null</code> |
346 |
*/ |
347 |
public static ITextSelection getTextSelection(IJavaElement je) { |
348 |
if (je instanceof ISourceReference) |
349 |
return getTextSelection((ISourceReference)je); |
350 |
return null; |
351 |
} |
352 |
|
353 |
/** |
354 |
* Returns the text selection for the given source reference. |
355 |
* |
356 |
* @param reference the source reference |
357 |
* @return the text selection or <code>null</code> |
358 |
*/ |
359 |
public static ITextSelection getTextSelection(ISourceReference reference) { |
360 |
if (reference == null) |
361 |
return null; |
362 |
|
363 |
try { |
364 |
ISourceRange range= null; |
365 |
if (reference instanceof ILocalVariable) { |
366 |
IJavaElement je2= ((ILocalVariable)reference).getParent(); |
367 |
if (je2 instanceof ISourceReference) |
368 |
range= ((ISourceReference)je2).getSourceRange(); |
369 |
} else |
370 |
range= reference.getSourceRange(); |
371 |
|
372 |
if (range == null) |
373 |
return null; |
374 |
|
375 |
int offset= range.getOffset(); |
376 |
int length= range.getLength(); |
377 |
|
378 |
if (offset < 0 || length < 0) |
379 |
return null; |
380 |
|
381 |
offset= -1; |
382 |
length= -1; |
383 |
|
384 |
if (reference instanceof IMember) { |
385 |
range= ((IMember) reference).getNameRange(); |
386 |
if (range != null) { |
387 |
offset= range.getOffset(); |
388 |
length= range.getLength(); |
389 |
} |
390 |
} else if (reference instanceof ITypeParameter) { |
391 |
range= ((ITypeParameter) reference).getNameRange(); |
392 |
if (range != null) { |
393 |
offset= range.getOffset(); |
394 |
length= range.getLength(); |
395 |
} |
396 |
} else if (reference instanceof ILocalVariable) { |
397 |
range= ((ILocalVariable)reference).getNameRange(); |
398 |
if (range != null) { |
399 |
offset= range.getOffset(); |
400 |
length= range.getLength(); |
401 |
} |
402 |
} else if (reference instanceof IImportDeclaration) { |
403 |
String name= ((IImportDeclaration) reference).getElementName(); |
404 |
if (name != null && name.length() > 0) { |
405 |
String content= reference.getSource(); |
406 |
if (content != null) { |
407 |
offset= range.getOffset() + content.indexOf(name); |
408 |
length= name.length(); |
409 |
} |
410 |
} |
411 |
} else if (reference instanceof IPackageDeclaration) { |
412 |
String name= ((IPackageDeclaration) reference).getElementName(); |
413 |
if (name != null && name.length() > 0) { |
414 |
String content= reference.getSource(); |
415 |
if (content != null) { |
416 |
offset= range.getOffset() + content.indexOf(name); |
417 |
length= name.length(); |
418 |
} |
419 |
} |
420 |
} |
421 |
if (offset >= 0 && length >= 0) |
422 |
return new TextSelection(offset, length); |
423 |
|
424 |
} catch (JavaModelException x) { |
425 |
} catch (IllegalArgumentException x) { |
426 |
} |
427 |
|
428 |
return null; |
429 |
} |
430 |
|
322 |
public static IEditorInput getEditorInput(Object input) throws JavaModelException { |
431 |
public static IEditorInput getEditorInput(Object input) throws JavaModelException { |
323 |
if (input instanceof IJavaElement) |
432 |
if (input instanceof IJavaElement) |
324 |
return getEditorInput((IJavaElement) input); |
433 |
return getEditorInput((IJavaElement) input); |