KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.debug.ui.heapwalking;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.ui.IDebugView;
18 import org.eclipse.debug.ui.InspectPopupDialog;
19 import org.eclipse.jdt.debug.core.IJavaObject;
20 import org.eclipse.jdt.debug.core.IJavaVariable;
21 import org.eclipse.jdt.internal.debug.core.model.JDIReferenceListValue;
22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
23 import org.eclipse.jdt.internal.debug.ui.actions.ObjectActionDelegate;
24 import org.eclipse.jdt.internal.debug.ui.actions.PopupInspectAction;
25 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Tree;
33 import org.eclipse.swt.widgets.TreeItem;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
37
38 import com.ibm.icu.text.MessageFormat;
39
40 /**
41  * Action to browse all references to selected object.
42  *
43  * @since 3.3
44  */

45 public class AllReferencesActionDelegate extends ObjectActionDelegate implements IWorkbenchWindowActionDelegate{
46
47     protected IWorkbenchWindow fWindow;
48     
49     public void run(IAction action) {
50         IStructuredSelection currentSelection = getCurrentSelection();
51         if (currentSelection != null && (currentSelection.getFirstElement() instanceof IJavaVariable)){
52             IJavaVariable var = (IJavaVariable) currentSelection.getFirstElement();
53             try {
54                 JDIReferenceListValue referenceList = new JDIReferenceListValue((IJavaObject) var.getValue());
55                 InspectPopupDialog ipd = new InspectPopupDialog(getShell(),
56                         getAnchor((IDebugView) getPart().getAdapter(IDebugView.class)),
57                         PopupInspectAction.ACTION_DEFININITION_ID,
58                         new JavaInspectExpression(MessageFormat.format(Messages.AllReferencesActionDelegate_1,new String JavaDoc[]{var.getName()}),referenceList));
59                 ipd.open();
60             } catch (DebugException e) {
61                 JDIDebugUIPlugin.statusDialog(e.getStatus());
62             }
63         } else {
64             JDIDebugUIPlugin.statusDialog(new Status(IStatus.WARNING,JDIDebugUIPlugin.getUniqueIdentifier(),Messages.AllReferencesActionDelegate_0));
65         }
66     }
67     
68     /**
69      * Compute an anchor based on selected item in the tree.
70      *
71      * @param view anchor view
72      * @return anchor point
73      */

74     protected static Point getAnchor(IDebugView view) {
75         Control control = view.getViewer().getControl();
76         if (control instanceof Tree) {
77             Tree tree = (Tree) control;
78             TreeItem[] selection = tree.getSelection();
79             if (selection.length > 0) {
80                 Rectangle bounds = selection[0].getBounds();
81                 return tree.toDisplay(new Point(bounds.x, bounds.y + bounds.height));
82             }
83         }
84         return control.toDisplay(0, 0);
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
89      */

90     public void init(IWorkbenchWindow window) {
91         fWindow = window;
92     }
93
94     /**
95      * @return the shell to use for new popups or <code>null</code>
96      */

97     private Shell getShell(){
98         if (fWindow != null){
99             return fWindow.getShell();
100         }
101         if (getWorkbenchWindow() != null){
102             return getWorkbenchWindow().getShell();
103         }
104         return null;
105     }
106     
107     /* (non-Javadoc)
108      * @see org.eclipse.jdt.internal.debug.ui.actions.ObjectActionDelegate#getPart()
109      */

110     protected IWorkbenchPart getPart() {
111         IWorkbenchPart part = super.getPart();
112         if (part != null){
113             return part;
114         } else if (fWindow != null){
115             return fWindow.getActivePage().getActivePart();
116         }
117         return null;
118     }
119     
120 }
121
Popular Tags