KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > expressions > WatchAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.debug.internal.ui.actions.expressions;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.IExpressionManager;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IDebugElement;
21 import org.eclipse.debug.core.model.IVariable;
22 import org.eclipse.debug.core.model.IWatchExpression;
23 import org.eclipse.debug.internal.ui.DebugUIPlugin;
24 import org.eclipse.debug.internal.ui.actions.ActionMessages;
25 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter;
29 import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapterExtension;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IViewPart;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.PartInitException;
38
39 /**
40  *
41  */

42 public class WatchAction implements IObjectActionDelegate {
43
44     private IStructuredSelection fSelection;
45
46     /**
47      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
48      */

49     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
50     }
51
52     /**
53      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
54      */

55     public void run(IAction action) {
56         if (fSelection == null) {
57             return;
58         }
59         Iterator JavaDoc iter = fSelection.iterator();
60         while (iter.hasNext()) {
61             IVariable variable = (IVariable) iter.next();
62             createExpression(variable);
63         }
64     }
65
66     private void showExpressionsView() {
67         IWorkbenchPage page = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
68         IViewPart part = page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
69         if (part == null) {
70             try {
71                 page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW);
72             } catch (PartInitException e) {
73             }
74         } else {
75             page.bringToTop(part);
76         }
77
78     }
79
80     private void createExpression(IVariable variable) {
81         IWatchExpression expression;
82         IWatchExpressionFactoryAdapter factory = getFactory(variable);
83         try {
84             String JavaDoc exp = variable.getName();
85             if (factory != null) {
86                 exp = factory.createWatchExpression(variable);
87             }
88             expression = DebugPlugin.getDefault().getExpressionManager().newWatchExpression(exp);
89         } catch (CoreException e) {
90             DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.WatchAction_0, ActionMessages.WatchAction_1, e); //
91
return;
92         }
93         DebugPlugin.getDefault().getExpressionManager().addExpression(expression);
94         IAdaptable object = DebugUITools.getDebugContext();
95         IDebugElement context = null;
96         if (object instanceof IDebugElement) {
97             context = (IDebugElement) object;
98         } else if (object instanceof ILaunch) {
99             context = ((ILaunch) object).getDebugTarget();
100         }
101         expression.setExpressionContext(context);
102         showExpressionsView();
103     }
104
105     /**
106      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
107      */

108     public void selectionChanged(IAction action, ISelection selection) {
109         fSelection = null;
110         int enabled = 0;
111         int size = -1;
112         if (selection instanceof IStructuredSelection) {
113             fSelection = (IStructuredSelection) selection;
114             size = fSelection.size();
115             IExpressionManager manager = DebugPlugin.getDefault().getExpressionManager();
116             Iterator JavaDoc iterator = fSelection.iterator();
117             while (iterator.hasNext()) {
118                 IVariable variable = (IVariable) iterator.next();
119                 if (variable instanceof IndexedVariablePartition) {
120                     break;
121                 } else if (manager.hasWatchExpressionDelegate(variable.getModelIdentifier()) && isFactoryEnabled(variable)) {
122                     enabled++;
123                 } else {
124                     break;
125                 }
126             }
127         }
128         action.setEnabled(enabled == size);
129     }
130
131     /**
132      * Returns whether the factory adapter for the given variable is currently enabled.
133      *
134      * @param variable
135      * @return whether the factory is enabled
136      */

137     private boolean isFactoryEnabled(IVariable variable) {
138         IWatchExpressionFactoryAdapter factory = getFactory(variable);
139         if (factory instanceof IWatchExpressionFactoryAdapterExtension) {
140             IWatchExpressionFactoryAdapterExtension ext = (IWatchExpressionFactoryAdapterExtension) factory;
141             return ext.canCreateWatchExpression(variable);
142         }
143         return true;
144     }
145
146     /**
147      * Returns the factory adapter for the given variable or <code>null</code> if none.
148      *
149      * @param variable
150      * @return factory or <code>null</code>
151      */

152     private IWatchExpressionFactoryAdapter getFactory(IVariable variable) {
153         return (IWatchExpressionFactoryAdapter) variable.getAdapter(IWatchExpressionFactoryAdapter.class);
154     }
155 }
156
Popular Tags