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.IWorkbenchWindow; |
22 |
import org.eclipse.ui.views.properties.tabbed.ISelectionConverter; |
23 |
import org.eclipse.jdt.core.ICompilationUnit; |
24 |
import org.eclipse.jdt.core.IJavaElement; |
25 |
import org.eclipse.jdt.core.JavaCore; |
26 |
import org.eclipse.jdt.core.JavaModelException; |
27 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
28 |
|
29 |
/** |
30 |
* An implementation of the tabbed properties ISelectionConverter for the JavaEditor. |
31 |
* It takes an ISelection which from the JavaEditor will be an ITextSelection and converts |
32 |
* it into an IStructuredSelection containing an IJavaElement. |
33 |
* |
34 |
* An empty StructuredSelection is returned if multiple IJavaElements are included |
35 |
* in the ITextSelection |
36 |
* |
37 |
* @author Karen Moore |
38 |
*/ |
39 |
public class JavaEditorSelectionConverter |
40 |
implements ISelectionConverter |
41 |
{ |
42 |
public IStructuredSelection structuredSelection(ISelection selection) { |
43 |
if (selection instanceof IStructuredSelection) { |
44 |
return (IStructuredSelection) selection; |
45 |
} |
46 |
int offset = ((ITextSelection) selection).getOffset(); |
47 |
int length = ((ITextSelection) selection).getLength(); |
48 |
|
49 |
ICompilationUnit unit = getCompilationUnit(); |
50 |
IJavaElement firstJavaElement = javaElementAt(offset, unit); |
51 |
if (firstJavaElement == null) { |
52 |
return StructuredSelection.EMPTY; |
53 |
} |
54 |
for (int i = offset+1; i < offset + length; i++) { |
55 |
IJavaElement nextJavaElement = javaElementAt(i, unit); |
56 |
if (!firstJavaElement.equals(nextJavaElement)) { |
57 |
return StructuredSelection.EMPTY; |
58 |
} |
59 |
} |
60 |
return new StructuredSelection(firstJavaElement); |
61 |
} |
62 |
|
63 |
private IJavaElement javaElementAt(int offset, ICompilationUnit compilationUnit) { |
64 |
IJavaElement javaElement = null; |
65 |
if (compilationUnit != null) { |
66 |
try { |
67 |
javaElement = compilationUnit.getElementAt(offset); |
68 |
} catch (JavaModelException ex) { |
69 |
// fall through |
70 |
} |
71 |
} |
72 |
return javaElement; |
73 |
} |
74 |
|
75 |
private ICompilationUnit getCompilationUnit() { |
76 |
IEditorPart editor = getActiveEditor(); |
77 |
if (editor == null) { |
78 |
return null; |
79 |
} |
80 |
IEditorInput editorInput = editor.getEditorInput(); |
81 |
if (editorInput instanceof IFileEditorInput) { |
82 |
IFileEditorInput fileInput = (IFileEditorInput) editorInput; |
83 |
IFile file = fileInput.getFile(); |
84 |
return JavaCore.createCompilationUnitFrom(file); |
85 |
} |
86 |
return null; |
87 |
} |
88 |
|
89 |
private IEditorPart getActiveEditor() { |
90 |
IWorkbenchWindow activeWorkbenchWindow = JavaPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow(); |
91 |
if (activeWorkbenchWindow == null) { |
92 |
return null; |
93 |
} |
94 |
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage(); |
95 |
if (activePage == null) { |
96 |
return null; |
97 |
} |
98 |
return activePage.getActiveEditor(); |
99 |
} |
100 |
|
101 |
// |
102 |
// public IJpaFile persistenceFileFor(IEditorInput input) { |
103 |
// IFile file = null; |
104 |
// if (input instanceof IFileEditorInput) { |
105 |
// IFileEditorInput fileInput = (IFileEditorInput) input; |
106 |
// file = fileInput.getFile(); |
107 |
// } |
108 |
// else if (input instanceof IClassFileEditorInput) { |
109 |
// IClassFileEditorInput classFileEditorInput = (IClassFileEditorInput) input; |
110 |
// IClassFile classFile = classFileEditorInput.getClassFile(); |
111 |
// classFile.getResource(); |
112 |
// |
113 |
// } |
114 |
// if (file == null) { |
115 |
// return null; |
116 |
// } |
117 |
// return JpaCorePlugin.getJpaFile(file); |
118 |
// } |
119 |
} |