Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90ed7dc0d633d8c1b0fc5f065c11dd9890964592 (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
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.actions.variables.details;

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.IDebugHelpContextIds;
import org.eclipse.debug.internal.ui.VariableValueEditorManager;
import org.eclipse.debug.internal.ui.actions.ActionMessages;
import org.eclipse.debug.internal.ui.actions.StatusInfo;
import org.eclipse.debug.ui.actions.IVariableValueEditor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.commands.ActionHandler;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerActivation;
import org.eclipse.ui.handlers.IHandlerService;

import com.ibm.icu.text.MessageFormat;

/**
 * Action which assigns a value to a variable from the detail pane
 * of the variables view.
 */
public class DetailPaneAssignValueAction extends Action{

    private IHandlerActivation fHandlerActivation;
	private IViewSite fViewSite;
	private ITextViewer fTextViewer;
	private IStructuredSelection fCurrentSelection;

	/**
	 * Attempts to evaluate the given string expression and assign the resulting value to the
	 * specified variable.  Displays error dialogs to the user if a problem is encountered.
	 *
	 * @see DetailPaneAssignValueAction
	 * @see org.eclipse.debug.internal.ui.elements.adapters.DefaultVariableCellModifier
	 *
	 * @param shell the shell to use to open dialogs
	 * @param variable the variable that is getting a new value
	 * @param newValueExpression the expression to evaluate and set as the new value
	 * @since 3.3.0
	 */
	public static void assignValue(Shell shell, IVariable variable, String newValueExpression){
		String modelIdentifier = variable.getModelIdentifier();
		IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
		if (editor != null) {
		    if (editor.saveVariable(variable, newValueExpression, shell)) {
		        // 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(newValueExpression)) {
				variable.setValue(newValueExpression);
			} else {
			    if (shell != null) {
					DebugUIPlugin.errorDialog(shell, ActionMessages.DetailPaneAssignValueAction_2, MessageFormat.format(ActionMessages.DetailPaneAssignValueAction_3, new Object[] {
							newValueExpression, variable.getName() }), new StatusInfo(IStatus.ERROR, ActionMessages.DetailPaneAssignValueAction_4)); //
			    }
			}
		} catch (DebugException e) {
            MessageDialog.openError(shell, ActionMessages.DetailPaneAssignValueAction_0, e.getStatus().getMessage());
		}
	}

	public DetailPaneAssignValueAction(ITextViewer textViewer, IViewSite viewSite) {
		super(ActionMessages.DetailPaneAssignValueAction_1);

        PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.DETAIL_PANE_ASSIGN_VALUE_ACTION);

		fTextViewer = textViewer;
		fViewSite = viewSite;

		setEnabled(false);
        IHandlerService service = fViewSite.getService(IHandlerService.class);
        ActionHandler handler = new ActionHandler(this);
        fHandlerActivation = service.activateHandler(getActionDefinitionId(), handler);
	}

	public void dispose() {
        IHandlerService service = fViewSite.getService(IHandlerService.class);
        service.deactivateHandler(fHandlerActivation);
    }

	public void updateCurrentVariable(IStructuredSelection selection) {
		boolean enabled = false;
		if ((selection.size() == 1) && (selection.getFirstElement() instanceof IValueModification)) {
			IValueModification valMod = (IValueModification) selection.getFirstElement();
			if (valMod.supportsValueModification()) {
				fCurrentSelection = selection;
				enabled = true;
			}
		}
		setEnabled(enabled);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	@Override
	public void run() {
		IVariable variable = (IVariable) fCurrentSelection.getFirstElement();

		Point selection = fTextViewer.getSelectedRange();
		String value = null;
		if (selection.y == 0) {
			value = fTextViewer.getDocument().get();
		} else {
			try {
				value = fTextViewer.getDocument().get(selection.x, selection.y);
			} catch (BadLocationException e1) {
			}
		}
		IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
		Shell activeShell= null;
		if (window != null) {
			activeShell= window.getShell();
		}

		assignValue(activeShell, variable, value);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#getActionDefinitionId()
	 */
	@Override
	public String getActionDefinitionId() {
		return IWorkbenchCommandConstants.FILE_SAVE;
	}

}

Back to the top