Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e99ca4f00501942ed094f4414fe1ddcf72cbf8e6 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.actions;

import java.text.MessageFormat;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IValueModification;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.views.variables.VariablesView;
import org.eclipse.debug.ui.actions.IVariableValueEditor;
import org.eclipse.debug.ui.actions.VariableValueEditorManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.SelectionProviderAction;

/**
 * Action which assigns a value to a variable from the detail pane
 * of the variables view.
 */
public class AssignValueAction extends SelectionProviderAction {
	private VariablesView variablesView;
	private ISourceViewer detailsViewer;

	public AssignValueAction(VariablesView varView, ISourceViewer detailViewer) {
		super(varView.getViewer(), ActionMessages.getString("AssignValueAction.1")); //$NON-NLS-1$
		variablesView = varView;
		detailsViewer = detailViewer;
		setEnabled(false);
		variablesView.getSite().getKeyBindingService().registerAction(this);
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public void selectionChanged(IStructuredSelection selection) {
		boolean enabled = false;
		if ((selection.size() == 1) && (selection.getFirstElement() instanceof IValueModification)) {
			IValueModification valMod = (IValueModification) selection.getFirstElement();
			if (valMod.supportsValueModification()) {
				super.selectionChanged(selection);
				enabled = true;
			}
		} 
		setEnabled(enabled);		
	}	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	public void run() {
		IVariable variable = (IVariable) getStructuredSelection().getFirstElement();
		
		Point selection = detailsViewer.getSelectedRange();
		String value = null;
		if (selection.y == 0) {
			value = detailsViewer.getDocument().get();
		} else {
			try {
				value = detailsViewer.getDocument().get(selection.x, selection.y);
			} catch (BadLocationException e1) {
			}
		}
		IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
		Shell activeShell= null;
		if (window != null) {
			activeShell= window.getShell();
		}
		
		String modelIdentifier = variable.getModelIdentifier();
		IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
		if (editor != null) {
		    if (editor.saveVariable(variable, value, activeShell)) {
		        // If we successfully delegate to an editor which performs the save,
		        // don't do any more work.
		        return;
		    }
		}
		
		try {
		    // If we failed to delegate to anyone, perform the default assignment.
			if (variable.verifyValue(value)) {
				variable.setValue(value);
			} else {
			    if (activeShell != null) {
			        DebugUIPlugin.errorDialog(activeShell, ActionMessages.getString("AssignValueAction.2"), MessageFormat.format(ActionMessages.getString("AssignValueAction.3"), new String[] {value, variable.getName()}), new StatusInfo(IStatus.ERROR, ActionMessages.getString("AssignValueAction.4")));  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			    }
			}
		} catch (DebugException e) {
			DebugUIPlugin.log(e);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#getActionDefinitionId()
	 */
	public String getActionDefinitionId() {		
		return "org.eclipse.ui.file.save"; //$NON-NLS-1$
	}

}

Back to the top