001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.views;
005:
006: import org.eclipse.core.resources.IProject;
007: import org.eclipse.jdt.core.IField;
008: import org.eclipse.jdt.core.IJavaElement;
009: import org.eclipse.jdt.core.IJavaProject;
010: import org.eclipse.jdt.core.IMember;
011: import org.eclipse.jdt.core.JavaModelException;
012: import org.eclipse.jdt.internal.ui.JavaPlugin;
013: import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
014: import org.eclipse.jdt.ui.JavaUI;
015: import org.eclipse.jface.text.IRegion;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.ui.IEditorPart;
020: import org.eclipse.ui.PartInitException;
021: import org.terracotta.dso.ConfigurationHelper;
022: import org.terracotta.dso.TcPlugin;
023: import org.terracotta.dso.actions.ActionUtil;
024:
025: import com.terracottatech.config.Root;
026:
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: public class ConfigUI {
032: static ISelection convertSelection(ISelection selection) {
033: if (selection.isEmpty()) {
034: return selection;
035: }
036:
037: IJavaProject javaProject = ActionUtil
038: .locateSelectedJavaProject(selection);
039: IProject project = (javaProject != null) ? javaProject
040: .getProject() : null;
041:
042: if (project != null
043: && selection instanceof IStructuredSelection) {
044: IStructuredSelection structuredSelection = (IStructuredSelection) selection;
045: List<IJavaElement> javaElements = new ArrayList<IJavaElement>();
046: for (Iterator iter = structuredSelection.iterator(); iter
047: .hasNext();) {
048: Object element = iter.next();
049: if (element instanceof Root) {
050: String rootField = ((Root) element).getFieldName();
051: if (rootField != null) {
052: TcPlugin plugin = TcPlugin.getDefault();
053: ConfigurationHelper configHelper = plugin
054: .getConfigurationHelper(project);
055: IField field = configHelper.getField(rootField);
056: if (field != null) {
057: javaElements.add(field);
058: }
059: }
060: } else if (element instanceof IMember) {
061: javaElements.add((IMember) element);
062: }
063: }
064: return new StructuredSelection(javaElements);
065: }
066: return StructuredSelection.EMPTY;
067: }
068:
069: public static IEditorPart jumpToMember(IJavaElement element) {
070: if (element != null) {
071: try {
072: IEditorPart editor = EditorUtility.openInEditor(
073: element, false);
074: JavaUI.revealInEditor(editor, element);
075: return editor;
076: } catch (JavaModelException e) {
077: JavaPlugin.log(e);
078: } catch (PartInitException e) {
079: JavaPlugin.log(e);
080: }
081: }
082: return null;
083: }
084:
085: public static IEditorPart jumpToRegion(IJavaElement element,
086: IRegion region) {
087: if (element != null) {
088: try {
089: IEditorPart editor = EditorUtility.openInEditor(
090: element, false);
091: EditorUtility.revealInEditor(editor, region);
092: return editor;
093: } catch (JavaModelException e) {
094: JavaPlugin.log(e);
095: } catch (PartInitException e) {
096: JavaPlugin.log(e);
097: }
098: }
099: return null;
100: }
101: }
|