KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.debug.core.DebugException;
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.ui.DebugUITools;
25 import org.eclipse.debug.ui.IDebugUIConstants;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IObjectActionDelegate;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchPart;
33 import org.eclipse.ui.PartInitException;
34
35 /**
36  *
37  */

38 public class WatchAction implements IObjectActionDelegate {
39
40     private IStructuredSelection fSelection;
41
42     /**
43      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
44      */

45     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
46     }
47
48     /**
49      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
50      */

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

99     public void selectionChanged(IAction action, ISelection selection) {
100         fSelection = null;
101         int enabled = 0;
102         int size = -1;
103         if (selection instanceof IStructuredSelection) {
104             fSelection = (IStructuredSelection) selection;
105             size = fSelection.size();
106             IExpressionManager manager = DebugPlugin.getDefault().getExpressionManager();
107             Iterator JavaDoc iterator = fSelection.iterator();
108             while (iterator.hasNext()) {
109                 IVariable variable = (IVariable) iterator.next();
110                 if (manager.hasWatchExpressionDelegate(variable.getModelIdentifier())) {
111                     enabled++;
112                 } else {
113                     break;
114                 }
115             }
116         }
117         action.setEnabled(enabled == size);
118     }
119
120 }
121
Popular Tags