Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b9682692c4289edb8113103d5a047f88d818953c (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
/**********************************************************************
 * Copyright (c) 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 ***********************************************************************/ 
package org.eclipse.cdt.debug.internal.ui.actions; 

import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.action.IAction;
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.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionDelegate;
 

/**
 * The "Add Expression" action contribution to editors.
 */
public class AddExpressionEditorActionDelegate extends ActionDelegate implements IEditorActionDelegate {

	private IEditorPart fEditorPart;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
	 */
	public void setActiveEditor( IAction action, IEditorPart targetEditor ) {
		setEditorPart( targetEditor );
	}

	private IEditorPart getEditorPart() {
		return fEditorPart;
	}

	private void setEditorPart( IEditorPart editorPart ) {
		fEditorPart = editorPart;
	}

	public void run( IAction action ) {
		String text = getSelectedText();
		ExpressionDialog dlg = new ExpressionDialog( getShell(), text );
		if ( dlg.open() != Window.OK )
			return;
		createExpression( dlg.getExpression() );
	}

	private String getSelectedText() {
		ISelection selection = getTargetSelection();
		if ( selection != null && selection instanceof ITextSelection ) {
			return ((ITextSelection)selection).getText().trim();
		}
		return ""; //$NON-NLS-1$
	}

	protected ISelection getTargetSelection() {
		IWorkbenchPart part = getEditorPart();
		if ( part != null ) {
			ISelectionProvider provider = part.getSite().getSelectionProvider();
			if ( provider != null ) {
				return provider.getSelection();
			}
		}
		return null;
	}

	private void createExpression( String text ) {
		IWatchExpression watchExpression= DebugPlugin.getDefault().getExpressionManager().newWatchExpression( text );
		DebugPlugin.getDefault().getExpressionManager().addExpression( watchExpression );
		IAdaptable context = DebugUITools.getDebugContext();
		if ( context instanceof IDebugElement )
			watchExpression.setExpressionContext( (IDebugElement)context );
	}

	protected Shell getShell() {
		return ( getEditorPart() != null ) ? getEditorPart().getSite().getShell() : CDebugUIPlugin.getActiveWorkbenchShell();
	}
}

Back to the top