KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > heapwalking > AllInstancesActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.heapwalking;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.model.IValue;
16 import org.eclipse.debug.ui.DebugUITools;
17 import org.eclipse.debug.ui.IDebugView;
18 import org.eclipse.debug.ui.InspectPopupDialog;
19 import org.eclipse.jdt.core.ICodeAssist;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
25 import org.eclipse.jdt.debug.core.IJavaType;
26 import org.eclipse.jdt.debug.core.IJavaValue;
27 import org.eclipse.jdt.debug.core.IJavaVariable;
28 import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIAllInstancesValue;
29 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
30 import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType;
31 import org.eclipse.jdt.internal.debug.ui.DebugWorkingCopyManager;
32 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
33 import org.eclipse.jdt.internal.debug.ui.JavaWordFinder;
34 import org.eclipse.jdt.internal.debug.ui.actions.ObjectActionDelegate;
35 import org.eclipse.jdt.internal.debug.ui.actions.PopupInspectAction;
36 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression;
37 import org.eclipse.jdt.ui.JavaUI;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.text.IDocument;
40 import org.eclipse.jface.text.IRegion;
41 import org.eclipse.jface.text.ITextSelection;
42 import org.eclipse.jface.viewers.ISelection;
43 import org.eclipse.jface.viewers.ISelectionProvider;
44 import org.eclipse.jface.viewers.IStructuredSelection;
45 import org.eclipse.swt.custom.StyledText;
46 import org.eclipse.swt.graphics.GC;
47 import org.eclipse.swt.graphics.Point;
48 import org.eclipse.swt.graphics.Rectangle;
49 import org.eclipse.swt.widgets.Control;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeItem;
53 import org.eclipse.ui.IEditorActionDelegate;
54 import org.eclipse.ui.IEditorInput;
55 import org.eclipse.ui.IEditorPart;
56 import org.eclipse.ui.IWorkbenchPage;
57 import org.eclipse.ui.IWorkbenchPart;
58 import org.eclipse.ui.IWorkbenchWindow;
59 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
60 import org.eclipse.ui.part.IPage;
61 import org.eclipse.ui.part.PageBookView;
62 import org.eclipse.ui.texteditor.IDocumentProvider;
63 import org.eclipse.ui.texteditor.IEditorStatusLine;
64 import org.eclipse.ui.texteditor.ITextEditor;
65
66 import com.ibm.icu.text.MessageFormat;
67
68 /**
69  * Class to provide new function of viewing all live objects of the selected type in the current VM
70  * Feature of 1.6 VMs
71  *
72  * @since 3.3
73  */

74 public class AllInstancesActionDelegate extends ObjectActionDelegate implements IEditorActionDelegate, IWorkbenchWindowActionDelegate {
75
76     private IWorkbenchWindow fWindow;
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
80      */

81     public void run(IAction action) {
82         if (getPart() != null){
83             ISelectionProvider provider = getPart().getSite().getSelectionProvider();
84             if (provider != null){
85                 ISelection selection = provider.getSelection();
86
87                 // If in an editor, get the text selection and check if a type is selected
88
if (getPart() instanceof IEditorPart && selection instanceof ITextSelection){
89                     ITextEditor editor = getTextEditor(getPart());
90                     IDocumentProvider documentProvider = editor.getDocumentProvider();
91                     if (documentProvider != null) {
92                         IDocument document = documentProvider.getDocument(editor.getEditorInput());
93                         IRegion selectedWord = JavaWordFinder.findWord(document,((ITextSelection)selection).getOffset());
94                         if (selectedWord != null){
95                             IJavaElement element = getJavaElement(editor.getEditorInput());
96                             if (element instanceof ICodeAssist){
97                                 try{
98                                     IJavaElement[] selectedTypes = ((ICodeAssist)element).codeSelect(selectedWord.getOffset(), selectedWord.getLength());
99                                     // findWord() will only return one element, so only check the first element
100
if (selectedTypes.length > 0){
101                                         runAllInstancesForType(selectedTypes[0]);
102                                         return;
103                                     }
104                                 } catch (JavaModelException e){
105                                     JDIDebugUIPlugin.log(e.getStatus());
106                                     report(Messages.AllInstancesActionDelegate_0,getPart());
107                                 }
108                             }
109                         }
110                     }
111                     
112                 // Otherwise, get the first selected element and check if it is a type
113
} else if (selection instanceof IStructuredSelection){
114                     runAllInstancesForType(((IStructuredSelection)selection).getFirstElement());
115                     return;
116                 }
117             }
118         }
119         report(Messages.AllInstancesActionDelegate_3,getPart());
120     }
121     
122     /**
123      * Checks if the passed element is a java type or constructor that all instances can
124      * be retrieved for. If so, retrieves the instances and displays them in a popup dialog.
125      *
126      * @param selectedElement The element to obtain all instances for
127      */

128     protected void runAllInstancesForType(Object JavaDoc selectedElement){
129         if (selectedElement != null){
130                         
131             IJavaType type = null;
132             try {
133                 // If the element is a constructor, get instances of its declaring type
134
if (selectedElement instanceof IMethod){
135                     if (((IMethod)selectedElement).isConstructor()){
136                         selectedElement = ((IMethod)selectedElement).getDeclaringType();
137                     }
138                 }
139                 // If the element is an IType, get the corresponding java variable from the VM
140
if(selectedElement instanceof IType) {
141                     IAdaptable adapt = DebugUITools.getDebugContext();
142                     if(adapt != null) {
143                         IJavaDebugTarget target = (IJavaDebugTarget) adapt.getAdapter(IJavaDebugTarget.class);
144                         if(target != null) {
145                             IType itype = (IType) selectedElement;
146                             IJavaType[] types = target.getJavaTypes(itype.getFullyQualifiedName());
147                             if(types != null && types.length > 0) {
148                                 type = types[0];
149                             } else {
150                                 // If the type is not known the the VM, open a popup dialog with 0 instances
151
JDIAllInstancesValue aiv = new JDIAllInstancesValue((JDIDebugTarget)target, null);
152                                 InspectPopupDialog ipd = new InspectPopupDialog(getShell(),
153                                         getAnchor(),
154                                         PopupInspectAction.ACTION_DEFININITION_ID,
155                                         new JavaInspectExpression(MessageFormat.format(Messages.AllInstancesActionDelegate_2, new String JavaDoc[]{itype.getElementName()}), aiv));
156                                 ipd.open();
157                                 return;
158                             }
159                         }
160                     }
161                 }
162                 // If the selected element is a java variable, just get the type
163
if (selectedElement instanceof IJavaVariable) {
164                     IJavaVariable var = (IJavaVariable) selectedElement;
165                     IValue val = var.getValue();
166                     if (val instanceof IJavaValue){
167                         type = ((IJavaValue)val).getJavaType();
168                     }
169                     if (type == null){
170                         type = var.getJavaType();
171                     }
172                 }
173                 
174             } catch (JavaModelException e){
175                 JDIDebugUIPlugin.log(e.getStatus());
176             } catch (DebugException e) {
177                 JDIDebugUIPlugin.log(e.getStatus());
178             }
179                     
180             if(type instanceof JDIReferenceType) {
181                 JDIReferenceType rtype = (JDIReferenceType) type;
182                 try{
183                     JDIAllInstancesValue aiv = new JDIAllInstancesValue((JDIDebugTarget)rtype.getDebugTarget(), rtype);
184                     InspectPopupDialog ipd = new InspectPopupDialog(getShell(),
185                             getAnchor(),
186                             PopupInspectAction.ACTION_DEFININITION_ID,
187                             new JavaInspectExpression(MessageFormat.format(Messages.AllInstancesActionDelegate_2, new String JavaDoc[]{type.getName()}), aiv));
188                     ipd.open();
189                     return;
190                 } catch (DebugException e) {
191                     JDIDebugUIPlugin.log(e);
192                     report(Messages.AllInstancesActionDelegate_0,getPart());
193                 }
194             }
195         }
196         report(Messages.AllInstancesActionDelegate_3,getPart());
197     }
198     
199      /**
200      * Convenience method for printing messages to the status line
201      * @param message the message to be displayed
202      * @param part the currently active workbench part
203      */

204     protected void report(final String JavaDoc message, final IWorkbenchPart part) {
205         JDIDebugUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
206             public void run() {
207                 IEditorStatusLine statusLine = (IEditorStatusLine) part.getAdapter(IEditorStatusLine.class);
208                 if (statusLine != null) {
209                     if (message != null) {
210                         statusLine.setMessage(true, message, null);
211                     } else {
212                         statusLine.setMessage(true, null, null);
213                     }
214                 }
215                 if (message != null && JDIDebugUIPlugin.getActiveWorkbenchShell() != null) {
216                     JDIDebugUIPlugin.getActiveWorkbenchShell().getDisplay().beep();
217                 }
218             }
219         });
220     }
221
222     /**
223      * Compute an anchor based on selected item in the tree.
224      *
225      * @return anchor point or <code>null</code> if one could not be obtained
226      */

227     protected Point getAnchor() {
228         
229         // If it's a debug view (variables or expressions), get the location of the selected item
230
IDebugView debugView = (IDebugView)getPart().getAdapter(IDebugView.class);
231         if (debugView != null){
232             Control control = debugView.getViewer().getControl();
233             if (control instanceof Tree) {
234                 Tree tree = (Tree) control;
235                 TreeItem[] selection = tree.getSelection();
236                 if (selection.length > 0) {
237                     Rectangle bounds = selection[0].getBounds();
238                     return tree.toDisplay(new Point(bounds.x, bounds.y + bounds.height));
239                 }
240             }
241         }
242         
243         //resolve the current control
244
Control widget = (Control)getPart().getAdapter(Control.class);
245         if (widget == null){
246             if(getPart() instanceof PageBookView) {
247                 //could be the outline view
248
PageBookView view = (PageBookView) getPart();
249                 IPage page = view.getCurrentPage();
250                 if(page != null) {
251                     widget = page.getControl();
252                 }
253             }
254         }
255         if (widget instanceof StyledText){
256             StyledText textWidget = (StyledText)widget;
257             Point docRange = textWidget.getSelectionRange();
258             int midOffset = docRange.x + (docRange.y / 2);
259             Point point = textWidget.getLocationAtOffset(midOffset);
260             point = textWidget.toDisplay(point);
261     
262             GC gc = new GC(textWidget);
263             gc.setFont(textWidget.getFont());
264             int height = gc.getFontMetrics().getHeight();
265             gc.dispose();
266             point.y += height;
267             return point;
268         }
269         if (widget instanceof Tree) {
270             Tree tree = (Tree) widget;
271             TreeItem[] selection = tree.getSelection();
272             if (selection.length > 0) {
273                 Rectangle bounds = selection[0].getBounds();
274                 return tree.toDisplay(new Point(bounds.x, bounds.y + bounds.height));
275             }
276         }
277         return null;
278     }
279         
280     /**
281      * Gets the <code>IJavaElement</code> from the editor input
282      * @param input the current editor input
283      * @return the corresponding <code>IJavaElement</code>
284      */

285     private IJavaElement getJavaElement(IEditorInput input) {
286         IJavaElement je = JavaUI.getEditorInputJavaElement(input);
287         if(je != null) {
288             return je;
289         }
290         //try to get from the working copy manager
291
return DebugWorkingCopyManager.getWorkingCopy(input, false);
292     }
293     
294     
295     /**
296      * Returns the text editor associated with the given part or <code>null</code>
297      * if none. In case of a multi-page editor, this method should be used to retrieve
298      * the correct editor to perform the operation on.
299      *
300      * @param part workbench part
301      * @return text editor part or <code>null</code>
302      */

303     private ITextEditor getTextEditor(IWorkbenchPart part) {
304         if (part instanceof ITextEditor) {
305             return (ITextEditor) part;
306         }
307         return (ITextEditor) part.getAdapter(ITextEditor.class);
308     }
309     
310     /* (non-Javadoc)
311      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
312      */

313     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
314         setActivePart(action, targetEditor);
315     }
316
317     /* (non-Javadoc)
318      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
319      */

320     public void init(IWorkbenchWindow window) {
321         fWindow = window;
322     }
323     
324     /**
325      * @return the shell to use for new popups or <code>null</code>
326      */

327     private Shell getShell(){
328         if (fWindow != null){
329             return fWindow.getShell();
330         }
331         if (getWorkbenchWindow() != null){
332             return getWorkbenchWindow().getShell();
333         }
334         return null;
335     }
336     
337     /* (non-Javadoc)
338      * @see org.eclipse.jdt.internal.debug.ui.actions.ObjectActionDelegate#getPart()
339      */

340     protected IWorkbenchPart getPart() {
341         IWorkbenchPart part = super.getPart();
342         if (part != null){
343             return part;
344         } else if (fWindow != null){
345             IWorkbenchPage page = fWindow.getActivePage();
346             if(page != null) {
347                 return page.getActivePart();
348             }
349         }
350         return null;
351     }
352     
353 }
354
Popular Tags