View | Details | Raw Unified | Return to bug 154781 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorSelectionConverter.java (+90 lines)
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.jface.viewers.ISelection;
13
import org.eclipse.jface.viewers.IStructuredSelection;
14
import org.eclipse.jface.viewers.StructuredSelection;
15
16
import org.eclipse.jface.text.ITextSelection;
17
18
import org.eclipse.ui.IEditorPart;
19
import org.eclipse.ui.IWorkbenchPage;
20
21
import org.eclipse.ui.views.properties.tabbed.ISelectionConverter;
22
23
import org.eclipse.jdt.core.IJavaElement;
24
import org.eclipse.jdt.core.ISourceRange;
25
import org.eclipse.jdt.core.ISourceReference;
26
import org.eclipse.jdt.core.JavaModelException;
27
28
import org.eclipse.jdt.internal.ui.JavaPlugin;
29
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
30
31
/**
32
 * An  implementation of the tabbed properties ISelectionConverter for the JavaEditor. 
33
 * It takes an ISelection which from the JavaEditor will be an ITextSelection and converts
34
 * it into an IStructuredSelection containing an IJavaElement.
35
 * 
36
 * @since 3.3
37
 */
38
public class JavaEditorSelectionConverter implements ISelectionConverter {
39
	
40
	public IStructuredSelection structuredSelection(ISelection selection) {
41
		if (selection instanceof IStructuredSelection)
42
			return (IStructuredSelection) selection;
43
		
44
		if (!(selection instanceof ITextSelection))
45
			return StructuredSelection.EMPTY;
46
		
47
		ITextSelection textSelection= (ITextSelection)selection;
48
		
49
		IJavaElement javaElement= getJavaElementAt((textSelection));
50
		if (javaElement == null)
51
			return StructuredSelection.EMPTY;
52
53
		if (javaElement instanceof ISourceReference) {
54
			//make sure only one JavaElement is in the selection.  Compare its length
55
			//to the length of the selection.
56
			try {
57
				ISourceRange range= ((ISourceReference) javaElement).getSourceRange();
58
				if (range != null && textSelection.getLength() > range.getLength())
59
					return StructuredSelection.EMPTY;
60
			}
61
			catch (JavaModelException e) {
62
				JavaPlugin.log(e);
63
				return StructuredSelection.EMPTY;
64
			}
65
		}
66
67
		return new StructuredSelection(javaElement);
68
	}
69
	
70
	private IJavaElement getJavaElementAt(ITextSelection textSelection) {
71
		IEditorPart editor= getActiveEditor();
72
		if (editor instanceof JavaEditor) {
73
			IJavaElement je= ((JavaEditor)editor).getInputJavaElement();
74
			try {
75
				return SelectionConverter.getElementAtOffset(je, textSelection);
76
			} catch (JavaModelException e) {
77
				JavaPlugin.log(e);
78
				// Fall through
79
			}
80
		}
81
		return null;
82
	}
83
	
84
	private IEditorPart getActiveEditor() {
85
		IWorkbenchPage activePage= JavaPlugin.getActivePage();
86
		if (activePage == null)
87
			return null;
88
		return activePage.getActiveEditor();
89
	}
90
}

Return to bug 154781