| 1 11 package com.mountainminds.eclemma.internal.ui.coverageview; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.jdt.core.IClassFile; 15 import org.eclipse.jdt.core.ICompilationUnit; 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.JavaModelException; 18 import org.eclipse.jface.text.IMarkSelection; 19 import org.eclipse.jface.text.ITextSelection; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.jface.viewers.StructuredViewer; 24 import org.eclipse.ui.IEditorPart; 25 import org.eclipse.ui.ISelectionListener; 26 import org.eclipse.ui.IViewPart; 27 import org.eclipse.ui.IWorkbenchPart; 28 29 37 class SelectionTracker { 38 39 private final IViewPart targetview; 40 private final StructuredViewer target; 41 42 private boolean enabled = false; 43 private IJavaElement currentSelection = null; 44 45 private final ISelectionListener listener = new ISelectionListener() { 46 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 47 if (part != targetview) { 48 if (selection instanceof IStructuredSelection) { 49 IStructuredSelection ssel = (IStructuredSelection) selection; 50 if (ssel.size() == 1) { 51 IJavaElement element = getJavaElement(ssel.getFirstElement()); 52 if (element != null) applySelection(element, false); 53 } 54 return; 55 } 56 if (part instanceof IEditorPart) { 57 IJavaElement element = getJavaElement(((IEditorPart) part).getEditorInput()); 58 if (element != null) { 59 element = findElementAtCursor(element, selection); 60 applySelection(element, false); 61 } 62 } 63 } 64 } 65 }; 66 67 73 private IJavaElement getJavaElement(Object object) { 74 if (object instanceof IJavaElement) { 75 return (IJavaElement) object; 76 } 77 if (object instanceof IAdaptable) { 78 IAdaptable a = (IAdaptable) object; 79 return (IJavaElement) a.getAdapter(IJavaElement.class); 80 } 81 return null; 82 } 83 84 93 private IJavaElement findElementAtCursor(IJavaElement unit, ISelection selection) { 94 int pos = -1; 95 if (selection instanceof ITextSelection) { 96 pos = ((ITextSelection) selection).getOffset(); 97 } 98 if (selection instanceof IMarkSelection) { 99 pos = ((IMarkSelection) selection).getOffset(); 100 } 101 if (pos == -1) return unit; 102 IJavaElement element = null; 103 try { 104 switch (unit.getElementType()) { 105 case IJavaElement.COMPILATION_UNIT: 106 element = ((ICompilationUnit) unit).getElementAt(pos); 107 break; 108 case IJavaElement.CLASS_FILE: 109 element = ((IClassFile) unit).getElementAt(pos); 110 break; 111 } 112 } catch (JavaModelException e) { 113 } 115 return element == null ? unit : element; 116 } 117 118 126 private void applySelection(IJavaElement element, boolean force) { 127 currentSelection = element; 128 if (force || (enabled && targetview != targetview.getSite().getPage().getActivePart())) { 129 target.setSelection(new StructuredSelection(element), true); 130 } 131 } 132 133 141 public SelectionTracker(IViewPart targetview, StructuredViewer target) { 142 this.targetview = targetview; 143 this.target = target; 144 targetview.getSite().getPage().addPostSelectionListener(listener); 145 } 146 147 153 public void setEnabled(boolean enabled) { 154 this.enabled = enabled; 155 if (enabled && currentSelection != null) { 156 applySelection(currentSelection, true); 157 } 158 } 159 160 164 public void dispose() { 165 targetview.getSite().getPage().removePostSelectionListener(listener); 166 } 167 168 } 169 | Popular Tags |