1 /*******************************************************************************2 * Copyright (c) 2000, 2005 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials4 * are made available under the terms of the Eclipse Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/epl-v10.html7 * 8 * Contributors:9 * IBM Corporation - initial API and implementation10 *******************************************************************************/11 package org.eclipse.jdt.internal.debug.ui.actions;12 13 14 import java.util.Iterator ;15 16 import org.eclipse.core.runtime.IAdaptable;17 import org.eclipse.debug.core.DebugException;18 import org.eclipse.debug.core.DebugPlugin;19 import org.eclipse.debug.core.ILaunch;20 import org.eclipse.debug.core.model.IDebugElement;21 import org.eclipse.debug.core.model.IWatchExpression;22 import org.eclipse.debug.ui.DebugUITools;23 import org.eclipse.jdt.debug.core.IJavaVariable;24 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;25 import org.eclipse.jface.viewers.IStructuredSelection;26 27 /**28 * Create a watch item from a selected variable29 */30 public class WatchAction extends InspectAction {31 32 public void run() {33 Object selectedObject= getSelectedObject();34 if (selectedObject instanceof IStructuredSelection) {35 IStructuredSelection selection = (IStructuredSelection)selectedObject;36 Iterator elements = selection.iterator();37 while (elements.hasNext()) {38 try {39 createWatchExpression(((IJavaVariable)elements.next()).getName());40 } catch (DebugException e) {41 JDIDebugUIPlugin.log(e);42 return;43 }44 }45 showExpressionView();46 } else if (selectedObject instanceof String ) {47 createWatchExpression((String ) selectedObject);48 showExpressionView();49 }50 }51 52 private void createWatchExpression(String snippet) {53 IWatchExpression expression= DebugPlugin.getDefault().getExpressionManager().newWatchExpression(snippet);54 DebugPlugin.getDefault().getExpressionManager().addExpression(expression);55 IAdaptable object = DebugUITools.getDebugContext();56 IDebugElement context= null;57 if (object instanceof IDebugElement) {58 context= (IDebugElement) object;59 } else if (object instanceof ILaunch) {60 context= ((ILaunch) object).getDebugTarget();61 }62 expression.setExpressionContext(context);63 }64 65 }66