Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af77dca4387d7856980a7963ecf335c4c37798f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2004, 2005 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Action for adding a watchpoint at a selection in the active 
 * C/C++ or assembly editor.
 */
public class AddWatchpointActionDelegate extends ActionDelegate implements IWorkbenchWindowActionDelegate, IPartListener {

	private boolean fInitialized = false;

	private IAction fAction = null;

	private ITextEditor fTextEditor = null;

	private IWorkbenchWindow fWorkbenchWindow = null;

	private IResource fResource = null;

	private String fSourceHandle = ""; //$NON-NLS-1$

	/**
	 * Constructor for AddWatchpointActionDelegate.
	 */
	public AddWatchpointActionDelegate() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
	 */
	public void init( IWorkbenchWindow window ) {
		setWorkbenchWindow( window );
		window.getPartService().addPartListener( this );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(IAction)
	 */
	public void run( IAction action ) {
		String expression = getSelectedExpression();
		AddWatchpointDialog dlg = new AddWatchpointDialog( CDebugUIPlugin.getActiveWorkbenchShell(), true, false, expression, true );
		if ( dlg.open() != Window.OK )
			return;
		if ( getTextEditor() != null ) {
			update();
			addWatchpoint( getTextEditor().getEditorInput(), dlg.getWriteAccess(), dlg.getReadAccess(), dlg.getExpression() );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged( IAction action, ISelection selection ) {
		if ( !fInitialized ) {
			setAction( action );
			if ( getWorkbenchWindow() != null ) {
				IWorkbenchPage page = getWorkbenchWindow().getActivePage();
				if ( page != null ) {
					IEditorPart part = page.getActiveEditor();
					if ( part instanceof ITextEditor ) {
						setTextEditor( (ITextEditor)part );
					}
				}
			}
			fInitialized = true;
		}
	}

	protected IAction getAction() {
		return fAction;
	}

	protected void setAction( IAction action ) {
		fAction = action;
	}

	protected IWorkbenchWindow getWorkbenchWindow() {
		return fWorkbenchWindow;
	}

	protected void setWorkbenchWindow( IWorkbenchWindow workbenchWindow ) {
		fWorkbenchWindow = workbenchWindow;
	}

	protected ITextEditor getTextEditor() {
		return fTextEditor;
	}

	protected void setTextEditor( ITextEditor editor ) {
		fTextEditor = editor;
		if ( fTextEditor != null ) {
			IEditorInput input = fTextEditor.getEditorInput();
			setSourceHandle( input );
			setResource( input );
		}
		setEnabledState( editor );
	}

	protected String getSelectedExpression() {
		if ( getTextEditor() != null ) {
			ISelectionProvider sp = getTextEditor().getSelectionProvider();
			if ( sp != null ) {
				ISelection s = sp.getSelection();
				if ( s instanceof ITextSelection ) {
					return ((ITextSelection)s).getText().trim();
				}
			}
		}
		return ""; //$NON-NLS-1$
	}

	protected void update( ISelection selection ) {
		setEnabledState( getTextEditor() );
	}

	protected void update() {
		IAction action = getAction();
		if ( action != null ) {
			action.setEnabled( getTextEditor() != null );
		}
	}

	protected void setEnabledState( ITextEditor editor ) {
		if ( getAction() != null ) {
			getAction().setEnabled( editor != null );
		}
	}

	protected IResource getResource() {
		return fResource;
	}

	protected void setResource( IEditorInput input ) {
		if ( input instanceof IFileEditorInput ) {
			fResource = ((IFileEditorInput)input).getFile().getProject();
		}
		else {
			fResource = ResourcesPlugin.getWorkspace().getRoot();
		}
	}

	protected void addWatchpoint( IEditorInput editorInput, boolean write, boolean read, String expression ) {
		if ( getResource() == null )
			return;
		IDocument document = getTextEditor().getDocumentProvider().getDocument( editorInput );
		WatchpointExpressionVerifier wev = new WatchpointExpressionVerifier();
		if ( wev.isValidExpression( document, expression ) ) {
			try {
				CDIDebugModel.createWatchpoint( getSourceHandle(), getResource(), write, read, expression, true, 0, "", true ); //$NON-NLS-1$
			}
			catch( CoreException ce ) {
				CDebugUIPlugin.errorDialog( ActionMessages.getString( "AddWatchpointActionDelegate.0" ), ce ); //$NON-NLS-1$
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart)
	 */
	public void partActivated( IWorkbenchPart part ) {
		if ( part instanceof ITextEditor ) {
			setTextEditor( (ITextEditor)part );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart)
	 */
	public void partBroughtToTop( IWorkbenchPart part ) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart)
	 */
	public void partClosed( IWorkbenchPart part ) {
		if ( part == getTextEditor() ) {
			setTextEditor( null );
			if ( getAction() != null ) {
				getAction().setEnabled( false );
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart)
	 */
	public void partDeactivated( IWorkbenchPart part ) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart)
	 */
	public void partOpened( IWorkbenchPart part ) {
		if ( part instanceof ITextEditor ) {
			if ( getTextEditor() == null ) {
				setTextEditor( (ITextEditor)part );
			}
		}
	}

	private String getSourceHandle() {
		return fSourceHandle;
	}
	
	private void setSourceHandle( IEditorInput input ) {
		fSourceHandle = ""; //$NON-NLS-1$
		if ( input instanceof IFileEditorInput ) {
			fSourceHandle = ((IFileEditorInput)input).getFile().getFullPath().toOSString();
		}
		else if ( input instanceof IStorageEditorInput ) {
			try {
				IPath path = ((IStorageEditorInput)input).getStorage().getFullPath();
				if ( path != null )
					fSourceHandle = path.toOSString();
			}
			catch( CoreException e ) {
			}
		}
	}
}

Back to the top