1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Wind River Systems - Hide action for non standard debug models (298217)
14  *******************************************************************************/
15 package org.eclipse.debug.internal.ui.actions.variables;
16 
17 
18 import java.text.MessageFormat;
19 import java.util.Iterator;
20 
21 import org.eclipse.debug.core.DebugException;
22 import org.eclipse.debug.core.model.IValueModification;
23 import org.eclipse.debug.core.model.IVariable;
24 import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
25 import org.eclipse.debug.internal.ui.DebugPluginImages;
26 import org.eclipse.debug.internal.ui.DebugUIPlugin;
27 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
28 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
29 import org.eclipse.debug.internal.ui.VariableValueEditorManager;
30 import org.eclipse.debug.internal.ui.actions.ActionMessages;
31 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
32 import org.eclipse.debug.ui.IDebugUIConstants;
33 import org.eclipse.debug.ui.actions.IVariableValueEditor;
34 import org.eclipse.jface.dialogs.IInputValidator;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.actions.SelectionProviderAction;
40 
41 /**
42  * Action for changing the value of primitives and <code>String</code> variables.
43  * This action will attempt to delegate the editing operation to a registered
44  * variable value editor, if any is provided for the variable's debug model.
45  */
46 public class ChangeVariableValueAction extends SelectionProviderAction {
47 
48 	protected IVariable fVariable;
49 	private VariablesView fView;
50 	private boolean fEditing= false;
51 	private boolean isApplicable = false;
52 
53 	/**
54 	 * Creates a new ChangeVariableValueAction for the given variables view
55 	 * @param view the variables view in which this action will appear
56 	 */
ChangeVariableValueAction(VariablesView view)57 	public ChangeVariableValueAction(VariablesView view) {
58 		super(view.getViewer(), ActionMessages.ChangeVariableValue_title);
59 		setDescription(ActionMessages.ChangeVariableValue_toolTipText);
60 		setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_CHANGE_VARIABLE_VALUE));
61 		setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_LCL_CHANGE_VARIABLE_VALUE));
62 		setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_CHANGE_VARIABLE_VALUE));
63 		PlatformUI.getWorkbench().getHelpSystem().setHelp(
64 			this,
65 			IDebugHelpContextIds.CHANGE_VALUE_ACTION);
66 		fView= view;
67 	}
68 
69 	/**
70 	 * Indicates whether this action is applicable for the current selection.
71 	 * If the element selected in the viewer is not a standard debug model
72 	 * element this action is not applicable.
73 	 * @return if this action applies to the current selection
74 	 */
isApplicable()75 	public boolean isApplicable() {
76 		return isApplicable;
77 	}
78 
79 	/**
80 	 * Edit the variable value with an in-line text editor.
81 	 * @param variable run the action on the given variable
82 	 */
doActionPerformed(final IVariable variable)83 	protected void doActionPerformed(final IVariable variable) {
84 		Shell shell = fView.getViewSite().getShell();
85 		// If a previous edit is still in progress, don't start another
86 		if (fEditing) {
87 			return;
88 		}
89 		fEditing= true;
90 		fVariable = variable;
91 		if (!delegateEdit(shell)) {
92 			doDefaultEdit(shell);
93 		}
94 		fEditing= false;
95 	}
96 
97 	/**
98 	 * Attempts to edit the variable by delegating to anyone who's
99 	 * contributed a variable value editor via extension. Returns
100 	 * <code>true</code> if a delegate handled the edit, <code>false</code>
101 	 * if the variable still needs to be edited.
102 	 *
103 	 * @param shell a shell for prompting the user
104 	 * @return whether or not a delegate attempted to edit the variable
105 	 */
delegateEdit(Shell shell)106 	private boolean delegateEdit(Shell shell) {
107 		String modelIdentifier = fVariable.getModelIdentifier();
108 		IVariableValueEditor editor= VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
109 		if (editor != null) {
110 			return editor.editVariable(fVariable, shell);
111 		}
112 		return false;
113 	}
114 
115 	/**
116 	 * Edits the variable using the default variable editor
117 	 * @param shell a shell for prompting the user
118 	 */
doDefaultEdit(Shell shell)119 	protected void doDefaultEdit(Shell shell) {
120 		String name= IInternalDebugCoreConstants.EMPTY_STRING;
121 		String value= IInternalDebugCoreConstants.EMPTY_STRING;
122 		try {
123 			name= fVariable.getName();
124 			value= fVariable.getValue().getValueString();
125 		} catch (DebugException exception) {
126 			DebugUIPlugin.errorDialog(shell, ActionMessages.ChangeVariableValue_errorDialogTitle,ActionMessages.ChangeVariableValue_errorDialogMessage, exception);	//
127 			return;
128 		}
129 		ChangeVariableValueInputDialog inputDialog = new ChangeVariableValueInputDialog(shell, ActionMessages.ChangeVariableValue_1, MessageFormat.format(ActionMessages.ChangeVariableValue_2, new Object[] { name }), value, new IInputValidator() { //
130 			/**
131 			 * Returns an error string if the input is invalid
132 			 */
133 			@Override
134 			public String isValid(String input) {
135 				try {
136 					if (fVariable.verifyValue(input)) {
137 						return null; // null means valid
138 					}
139 				} catch (DebugException exception) {
140 					return ActionMessages.ChangeVariableValue_3;
141 				}
142 				return ActionMessages.ChangeVariableValue_4;
143 			}
144 		});
145 
146 		inputDialog.open();
147 		String newValue= inputDialog.getValue();
148 		if (newValue != null) {
149 			// null value means cancel was pressed
150 			try {
151 				fVariable.setValue(newValue);
152 				getSelectionProvider().setSelection(new StructuredSelection(fVariable));
153 			} catch (DebugException de) {
154 				DebugUIPlugin.errorDialog(shell, ActionMessages.ChangeVariableValue_errorDialogTitle,ActionMessages.ChangeVariableValue_errorDialogMessage, de);	//
155 			}
156 		}
157 	}
158 
159 	/**
160 	 * Updates the enabled state of this action based
161 	 * on the selection
162 	 * @param sel the selection to update
163 	 */
update(IStructuredSelection sel)164 	protected void update(IStructuredSelection sel) {
165 		isApplicable = false;
166 		Iterator<Object> iter = sel.iterator();
167 		if (iter.hasNext()) {
168 			Object object= iter.next();
169 			if (object instanceof IValueModification) {
170 				isApplicable = true;
171 				IValueModification varMod= (IValueModification)object;
172 				if (!varMod.supportsValueModification()) {
173 					setEnabled(false);
174 					return;
175 				}
176 				setEnabled(!iter.hasNext());
177 				return;
178 			}
179 		}
180 		setEnabled(false);
181 	}
182 
183 	@Override
run()184 	public void run() {
185 		Iterator<Object> iterator = getStructuredSelection().iterator();
186 		doActionPerformed((IVariable)iterator.next());
187 	}
188 
189 	@Override
selectionChanged(IStructuredSelection sel)190 	public void selectionChanged(IStructuredSelection sel) {
191 		update(sel);
192 	}
193 }
194 
195