### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.ui Index: ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorSelectionConverter.java =================================================================== RCS file: ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorSelectionConverter.java diff -N ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorSelectionConverter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorSelectionConverter.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2006 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0, which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jdt.internal.ui.javaeditor; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; + +import org.eclipse.jface.text.ITextSelection; + +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; + +import org.eclipse.ui.views.properties.tabbed.ISelectionConverter; + +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.ISourceRange; +import org.eclipse.jdt.core.ISourceReference; +import org.eclipse.jdt.core.JavaModelException; + +import org.eclipse.jdt.internal.ui.JavaPlugin; +import org.eclipse.jdt.internal.ui.actions.SelectionConverter; + +/** + * An implementation of the tabbed properties ISelectionConverter for the JavaEditor. + * It takes an ISelection which from the JavaEditor will be an ITextSelection and converts + * it into an IStructuredSelection containing an IJavaElement. + * + * @since 3.3 + */ +public class JavaEditorSelectionConverter implements ISelectionConverter { + + public IStructuredSelection structuredSelection(ISelection selection) { + if (selection instanceof IStructuredSelection) + return (IStructuredSelection) selection; + + if (!(selection instanceof ITextSelection)) + return StructuredSelection.EMPTY; + + ITextSelection textSelection= (ITextSelection)selection; + + IJavaElement javaElement= getJavaElementAt((textSelection)); + if (javaElement == null) + return StructuredSelection.EMPTY; + + if (javaElement instanceof ISourceReference) { + //make sure only one JavaElement is in the selection. Compare its length + //to the length of the selection. + try { + ISourceRange range= ((ISourceReference) javaElement).getSourceRange(); + if (range != null && textSelection.getLength() > range.getLength()) + return StructuredSelection.EMPTY; + } + catch (JavaModelException e) { + JavaPlugin.log(e); + return StructuredSelection.EMPTY; + } + } + + return new StructuredSelection(javaElement); + } + + private IJavaElement getJavaElementAt(ITextSelection textSelection) { + IEditorPart editor= getActiveEditor(); + if (editor instanceof JavaEditor) { + IJavaElement je= ((JavaEditor)editor).getInputJavaElement(); + try { + return SelectionConverter.getElementAtOffset(je, textSelection); + } catch (JavaModelException e) { + JavaPlugin.log(e); + // Fall through + } + } + return null; + } + + private IEditorPart getActiveEditor() { + IWorkbenchPage activePage= JavaPlugin.getActivePage(); + if (activePage == null) + return null; + return activePage.getActiveEditor(); + } +}