Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006 Oracle. All rights reserved. |
3 |
* This program and the accompanying materials are made available under the terms of |
4 |
* the Eclipse Public License v1.0, which accompanies this distribution and is available at |
5 |
* http://www.eclipse.org/legal/epl-v10.html. |
6 |
* |
7 |
* Contributors: |
8 |
* Oracle - initial API and implementation |
9 |
******************************************************************************/ |
10 |
package org.eclipse.jdt.internal.ui.javaeditor; |
11 |
|
12 |
import org.eclipse.core.resources.IFile; |
13 |
import org.eclipse.jface.viewers.ISelection; |
14 |
import org.eclipse.jface.viewers.IStructuredSelection; |
15 |
import org.eclipse.jface.viewers.StructuredSelection; |
16 |
import org.eclipse.jface.text.ITextSelection; |
17 |
import org.eclipse.ui.IEditorInput; |
18 |
import org.eclipse.ui.IEditorPart; |
19 |
import org.eclipse.ui.IFileEditorInput; |
20 |
import org.eclipse.ui.IWorkbenchPage; |
21 |
import org.eclipse.ui.views.properties.tabbed.ISelectionConverter; |
22 |
import org.eclipse.jdt.core.IClassFile; |
23 |
import org.eclipse.jdt.core.ICompilationUnit; |
24 |
import org.eclipse.jdt.core.IJavaElement; |
25 |
import org.eclipse.jdt.core.ISourceRange; |
26 |
import org.eclipse.jdt.core.ISourceReference; |
27 |
import org.eclipse.jdt.core.JavaCore; |
28 |
import org.eclipse.jdt.core.JavaModelException; |
29 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
30 |
import org.eclipse.jdt.internal.ui.actions.SelectionConverter; |
31 |
|
32 |
/** |
33 |
* An implementation of the tabbed properties ISelectionConverter for the JavaEditor. |
34 |
* It takes an ISelection which from the JavaEditor will be an ITextSelection and converts |
35 |
* it into an IStructuredSelection containing an IJavaElement. |
36 |
* |
37 |
* |
38 |
* @author Karen Moore |
39 |
*/ |
40 |
public class JavaEditorSelectionConverter |
41 |
implements ISelectionConverter |
42 |
{ |
43 |
public IStructuredSelection structuredSelection(ISelection selection) { |
44 |
if (selection instanceof IStructuredSelection) { |
45 |
return (IStructuredSelection) selection; |
46 |
} |
47 |
|
48 |
int length = ((ITextSelection) selection).getLength(); |
49 |
IJavaElement javaElement = getJavaElementAt(((ITextSelection) selection)); |
50 |
|
51 |
if (javaElement == null) { |
52 |
return StructuredSelection.EMPTY; |
53 |
} |
54 |
if (javaElement instanceof ISourceReference) { |
55 |
//make sure only one JavaElement is in the selection. Compare its length |
56 |
//to the length of the selection. |
57 |
ISourceRange range = null; |
58 |
try { |
59 |
range = ((ISourceReference) javaElement).getSourceRange(); |
60 |
} |
61 |
catch (JavaModelException e) { |
62 |
JavaPlugin.log(e); |
63 |
} |
64 |
if (length > range.getLength()) { |
65 |
return StructuredSelection.EMPTY; |
66 |
} |
67 |
} |
68 |
|
69 |
return new StructuredSelection(javaElement); |
70 |
} |
71 |
|
72 |
|
73 |
private IJavaElement getJavaElementAt(ITextSelection textSelection) { |
74 |
IEditorPart editor = getActiveEditor(); |
75 |
if (editor == null) { |
76 |
return null; |
77 |
} |
78 |
IEditorInput editorInput = editor.getEditorInput(); |
79 |
if (editorInput instanceof IFileEditorInput) { |
80 |
IFile file = ((IFileEditorInput) editorInput).getFile(); |
81 |
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file); |
82 |
try { |
83 |
return SelectionConverter.getElementAtOffset(unit, textSelection); |
84 |
} |
85 |
catch (JavaModelException e) { |
86 |
JavaPlugin.log(e); |
87 |
} |
88 |
} |
89 |
else if (editorInput instanceof IClassFileEditorInput) { |
90 |
IClassFile classFile = ((IClassFileEditorInput)editorInput).getClassFile(); |
91 |
int offset = textSelection.getOffset(); |
92 |
try { |
93 |
return classFile.getElementAt(offset); |
94 |
} |
95 |
catch (JavaModelException e) { |
96 |
JavaPlugin.log(e); |
97 |
} |
98 |
} |
99 |
return null; |
100 |
} |
101 |
|
102 |
private IEditorPart getActiveEditor() { |
103 |
IWorkbenchPage activePage = JavaPlugin.getActivePage(); |
104 |
if (activePage == null) { |
105 |
return null; |
106 |
} |
107 |
return activePage.getActiveEditor(); |
108 |
} |
109 |
} |