|
Lines 11-94
Link Here
|
| 11 |
package org.eclipse.cdt.debug.internal.ui.actions; |
11 |
package org.eclipse.cdt.debug.internal.ui.actions; |
| 12 |
|
12 |
|
| 13 |
|
13 |
|
| 14 |
import org.eclipse.cdt.debug.internal.core.model.CVariable; |
14 |
import org.eclipse.cdt.debug.internal.core.CRequest; |
| 15 |
import org.eclipse.cdt.debug.internal.ui.actions.AddWatchpointDialog; |
15 |
import org.eclipse.cdt.debug.internal.core.ICWatchpointTarget; |
| 16 |
import org.eclipse.cdt.debug.ui.CDebugUIPlugin; |
16 |
import org.eclipse.cdt.debug.ui.CDebugUIPlugin; |
| 17 |
import org.eclipse.debug.core.DebugException; |
17 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
|
18 |
import org.eclipse.core.runtime.IStatus; |
| 19 |
import org.eclipse.core.runtime.Status; |
| 20 |
import org.eclipse.debug.core.DebugPlugin; |
| 18 |
import org.eclipse.jface.action.IAction; |
21 |
import org.eclipse.jface.action.IAction; |
| 19 |
import org.eclipse.jface.viewers.ISelection; |
22 |
import org.eclipse.jface.viewers.ISelection; |
| 20 |
import org.eclipse.jface.viewers.IStructuredSelection; |
23 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 21 |
import org.eclipse.jface.viewers.TreeSelection; |
24 |
import org.eclipse.jface.viewers.TreeSelection; |
| 22 |
import org.eclipse.jface.window.Window; |
25 |
import org.eclipse.jface.window.Window; |
| 23 |
import org.eclipse.ui.IActionDelegate; |
|
|
| 24 |
import org.eclipse.ui.IObjectActionDelegate; |
26 |
import org.eclipse.ui.IObjectActionDelegate; |
| 25 |
import org.eclipse.ui.IWorkbenchPart; |
27 |
import org.eclipse.ui.IWorkbenchPart; |
|
|
28 |
import org.eclipse.ui.progress.WorkbenchJob; |
| 26 |
|
29 |
|
|
|
30 |
/** |
| 31 |
* Invoked when user right clicks on an element in the Variables or Expressions |
| 32 |
* view and selects 'Add Watchpoint (C/C++)' |
| 33 |
*/ |
| 34 |
public class AddWatchpointOnVariableActionDelegate extends AddWatchpointActionDelegate implements IObjectActionDelegate { |
| 27 |
|
35 |
|
| 28 |
public class AddWatchpointOnVariableActionDelegate extends AddWatchpointActionDelegate { |
|
|
| 29 |
|
| 30 |
/** |
36 |
/** |
| 31 |
* Constructor for Action1. |
37 |
* The target variable/expression |
|
|
38 |
*/ |
| 39 |
private ICWatchpointTarget fVar; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 32 |
*/ |
43 |
*/ |
| 33 |
public AddWatchpointOnVariableActionDelegate() { |
44 |
public AddWatchpointOnVariableActionDelegate() { |
| 34 |
super(); |
45 |
super(); |
| 35 |
} |
46 |
} |
| 36 |
|
47 |
|
| 37 |
/** |
48 |
/* (non-Javadoc) |
| 38 |
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) |
49 |
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart) |
| 39 |
*/ |
50 |
*/ |
| 40 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) {} |
51 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
|
|
52 |
// Don't care. Our logic is agnostic to the view we're invoked from. |
| 53 |
} |
| 41 |
|
54 |
|
| 42 |
/** |
55 |
private static class GetSizeRequest extends CRequest implements ICWatchpointTarget.GetSizeRequest { |
| 43 |
* @see IActionDelegate#run(IAction) |
56 |
int fSize; |
|
|
57 |
public int getSize() { |
| 58 |
return fSize; |
| 59 |
} |
| 60 |
public void setSize(int size) { |
| 61 |
fSize = size; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
/* (non-Javadoc) |
| 66 |
* @see org.eclipse.cdt.debug.internal.ui.actions.AddWatchpointActionDelegate#run(org.eclipse.jface.action.IAction) |
| 44 |
*/ |
67 |
*/ |
| 45 |
public void run(IAction action) { |
68 |
public void run(IAction action) { |
| 46 |
IStructuredSelection selection = getSelection(); |
69 |
if (fVar == null) { |
| 47 |
|
|
|
| 48 |
if (selection == null || selection.isEmpty()) { |
| 49 |
return; |
70 |
return; |
| 50 |
} |
71 |
} |
| 51 |
|
72 |
|
| 52 |
Object obj = ((TreeSelection)selection).getFirstElement(); |
73 |
final String expr = fVar.getExpression(); |
| 53 |
if (obj != null && obj instanceof CVariable) { |
74 |
if (expr == null) { |
| 54 |
CVariable var = (CVariable)obj; |
75 |
assert false : "how are we getting an empty expression?"; //$NON-NLS-1$ |
| 55 |
|
76 |
return; |
| 56 |
String expr = ""; |
|
|
| 57 |
|
| 58 |
try { |
| 59 |
expr = var.getExpressionString(); |
| 60 |
} catch (DebugException e) {} |
| 61 |
|
| 62 |
AddWatchpointDialog dlg = new AddWatchpointDialog(CDebugUIPlugin.getActiveWorkbenchShell(), |
| 63 |
getMemorySpaceManagement()); //$NON-NLS-1$ |
| 64 |
dlg.setExpression(expr); |
| 65 |
dlg.initializeRange(false, Integer.toString(var.sizeof())); |
| 66 |
if (dlg.open() == Window.OK) { |
| 67 |
addWatchpoint(dlg.getWriteAccess(), dlg.getReadAccess(), dlg.getExpression(), dlg.getMemorySpace(), dlg.getRange()); |
| 68 |
} |
| 69 |
} |
77 |
} |
|
|
78 |
|
| 79 |
// Getting the size of the variable/expression is an asynchronous |
| 80 |
// operation...or at least the API is (the CDI implementation reacts |
| 81 |
// synchronously) |
| 82 |
final ICWatchpointTarget.GetSizeRequest request = new GetSizeRequest() { |
| 83 |
public void done() { |
| 84 |
// Now that we have the size, put up a dialog to create the watchpoint |
| 85 |
final int size = getSize(); |
| 86 |
assert size > 0 : "unexpected variale/expression size"; //$NON-NLS-1$ |
| 87 |
WorkbenchJob job = new WorkbenchJob("open watchpoint dialog") { //$NON-NLS-1$ |
| 88 |
@Override |
| 89 |
public IStatus runInUIThread(IProgressMonitor monitor) { |
| 90 |
AddWatchpointDialog dlg = new AddWatchpointDialog(CDebugUIPlugin.getActiveWorkbenchShell(), |
| 91 |
getMemorySpaceManagement()); |
| 92 |
dlg.setExpression(expr); |
| 93 |
dlg.initializeRange(false, Integer.toString(size)); |
| 94 |
if (dlg.open() == Window.OK) { |
| 95 |
addWatchpoint(dlg.getWriteAccess(), dlg.getReadAccess(), dlg.getExpression(), dlg.getMemorySpace(), dlg.getRange()); |
| 96 |
} |
| 97 |
return Status.OK_STATUS; |
| 98 |
} |
| 99 |
}; |
| 100 |
job.setSystem(true); |
| 101 |
job.schedule(); |
| 102 |
} |
| 103 |
|
| 104 |
}; |
| 105 |
fVar.getSize(request); |
| 70 |
} |
106 |
} |
| 71 |
|
107 |
|
|
|
108 |
private class CanCreateWatchpointRequest extends CRequest implements ICWatchpointTarget.CanCreateWatchpointRequest { |
| 109 |
boolean fCanCreate; |
| 110 |
public boolean getCanCreate() { |
| 111 |
return fCanCreate; |
| 112 |
} |
| 113 |
public void setCanCreate(boolean value) { |
| 114 |
fCanCreate = value; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 72 |
/** |
118 |
/** |
| 73 |
* @see IActionDelegate#selectionChanged(IAction, ISelection) |
119 |
* Record the target variable/expression. |
|
|
120 |
* |
| 121 |
* @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, |
| 122 |
* org.eclipse.jface.viewers.ISelection) |
| 74 |
*/ |
123 |
*/ |
| 75 |
public void selectionChanged(IAction action, ISelection selection) { |
124 |
public void selectionChanged(final IAction action, ISelection selection) { |
| 76 |
if (selection == null || selection.isEmpty()) { |
125 |
if (selection == null || selection.isEmpty()) { |
| 77 |
action.setEnabled(false); |
126 |
action.setEnabled(false); |
| 78 |
return; |
127 |
return; |
| 79 |
} |
128 |
} |
| 80 |
if (selection instanceof TreeSelection) { |
129 |
if (selection instanceof TreeSelection) { |
| 81 |
Object obj = ((TreeSelection)selection).getFirstElement(); |
130 |
Object obj = ((TreeSelection)selection).getFirstElement(); |
| 82 |
if (obj != null && obj instanceof CVariable) { |
131 |
fVar = (ICWatchpointTarget)DebugPlugin.getAdapter(obj, ICWatchpointTarget.class); |
| 83 |
action.setEnabled(true); |
132 |
if (fVar != null) { |
| 84 |
} else { |
133 |
final ICWatchpointTarget.CanCreateWatchpointRequest request = new CanCreateWatchpointRequest() { |
| 85 |
action.setEnabled(false); |
134 |
public void done() { |
|
|
135 |
action.setEnabled(getCanCreate()); |
| 136 |
} |
| 137 |
}; |
| 138 |
fVar.canSetWatchpoint(request); |
| 139 |
return; |
| 86 |
} |
140 |
} |
|
|
141 |
assert false : "action should not have been available for object " + obj; //$NON-NLS-1$ |
| 87 |
} |
142 |
} |
|
|
143 |
else if (selection instanceof StructuredSelection) { |
| 144 |
// Not sure why, but sometimes we get an extraneous empty StructuredSelection. Seems harmless enough |
| 145 |
assert ((StructuredSelection)selection).getFirstElement() == null : "action installed in unexpected type of view/part"; //$NON-NLS-1$ |
| 146 |
} |
| 147 |
else { |
| 148 |
assert false : "action installed in unexpected type of view/part"; //$NON-NLS-1$ |
| 149 |
} |
| 150 |
action.setEnabled(false); |
| 88 |
} |
151 |
} |
| 89 |
|
|
|
| 90 |
private IStructuredSelection getSelection() { |
| 91 |
return (IStructuredSelection)getView().getViewSite().getSelectionProvider().getSelection(); |
| 92 |
} |
| 93 |
|
| 94 |
} |
152 |
} |