1 11 package org.eclipse.debug.internal.ui.actions.expressions; 12 13 import java.util.Iterator ; 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 42 public class WatchAction implements IObjectActionDelegate { 43 44 private IStructuredSelection fSelection; 45 46 49 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 50 } 51 52 55 public void run(IAction action) { 56 if (fSelection == null) { 57 return; 58 } 59 Iterator 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 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); 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 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 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 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 152 private IWatchExpressionFactoryAdapter getFactory(IVariable variable) { 153 return (IWatchExpressionFactoryAdapter) variable.getAdapter(IWatchExpressionFactoryAdapter.class); 154 } 155 } 156 | Popular Tags |