KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > ui > coverageview > SelectionTracker


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: SelectionTracker.java 393 2007-08-28 13:05:34Z mtnminds $
7  *
8  * Contributors:
9  * Brock Janiczak - link with selection option (SF #1774547)
10  ******************************************************************************/

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 /**
30  * Tracks the current selection of Java elements in a workbench page and
31  * synchronizes the selection of a target view. The selection is either taken
32  * from a structured viewer of from an editor input.
33  *
34  * @author Marc R. Hoffmann
35  * @version $Revision: 393 $
36  */

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   /**
68    * Try to derive a java element handle from the given object.
69    *
70    * @param object base object
71    * @return java element handle or <code>null</code>
72    */

73   private IJavaElement getJavaElement(Object JavaDoc 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   /**
85    * Try to identify a nested java element of the given element from a textual
86    * selection in its source code. This might be possible if the given element
87    * is a compilation unit or class file.
88    *
89    * @param unit unit to search
90    * @param selection selection within this unit
91    * @return nested element or the unit itself
92    */

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       // we ignore this
114
}
115     return element == null ? unit : element;
116   }
117
118   /**
119    * Selects the given element in the taget viewer when the tracker is enabled
120    * and the target view is not active. This conditions can be overruled by the
121    * <code>force</code> parameter.
122    *
123    * @param element element to select
124    * @param force if <code>true</code> the selection is applied in any case
125    */

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   /**
134    * Creates a new tracker for the given target view and viewer. The tracker
135    * registers itself with the workbench and must be {@link #dispose()}d when it
136    * is no longer used.
137    *
138    * @param targetview view to wich the workbench selections are applied
139    * @param target viewer to wich the workbench selections are applied
140    */

141   public SelectionTracker(IViewPart targetview, StructuredViewer target) {
142     this.targetview = targetview;
143     this.target = target;
144     targetview.getSite().getPage().addPostSelectionListener(listener);
145   }
146   
147   /**
148    * Enables or disables the tracker. If the tracker becomes enabled the last
149    * workbench selection is immediately applied.
150    *
151    * @param enabled flag whether the tracker should become enabled
152    */

153   public void setEnabled(boolean enabled) {
154     this.enabled = enabled;
155     if (enabled && currentSelection != null) {
156       applySelection(currentSelection, true);
157     }
158   }
159
160   /**
161    * Disposes the tracking functionality. This must be called before the target
162    * view and viewer get destroyed.
163    */

164   public void dispose() {
165     targetview.getSite().getPage().removePostSelectionListener(listener);
166   }
167
168 }
169
Popular Tags