1 11 package org.eclipse.debug.internal.ui.actions; 12 13 import java.text.MessageFormat ; 14 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.debug.core.DebugException; 17 import org.eclipse.debug.core.model.IValueModification; 18 import org.eclipse.debug.core.model.IVariable; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 21 import org.eclipse.debug.internal.ui.VariableValueEditorManager; 22 import org.eclipse.debug.internal.ui.views.variables.VariablesView; 23 import org.eclipse.debug.ui.actions.IVariableValueEditor; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.text.BadLocationException; 26 import org.eclipse.jface.text.source.ISourceViewer; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.widgets.Shell; 30 import org.eclipse.ui.IWorkbenchWindow; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.actions.SelectionProviderAction; 33 34 38 public class AssignValueAction extends SelectionProviderAction { 39 private VariablesView variablesView; 40 private ISourceViewer detailsViewer; 41 42 public AssignValueAction(VariablesView varView, ISourceViewer detailViewer) { 43 super(varView.getViewer(), ActionMessages.AssignValueAction_1); PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.ASSIGN_VALUE_ACTION); 45 variablesView = varView; 46 detailsViewer = detailViewer; 47 setEnabled(false); 48 variablesView.getSite().getKeyBindingService().registerAction(this); 49 } 50 51 54 public void selectionChanged(IStructuredSelection selection) { 55 boolean enabled = false; 56 if ((selection.size() == 1) && (selection.getFirstElement() instanceof IValueModification)) { 57 IValueModification valMod = (IValueModification) selection.getFirstElement(); 58 if (valMod.supportsValueModification()) { 59 super.selectionChanged(selection); 60 enabled = true; 61 } 62 } 63 setEnabled(enabled); 64 } 65 66 69 public void run() { 70 IVariable variable = (IVariable) getStructuredSelection().getFirstElement(); 71 72 Point selection = detailsViewer.getSelectedRange(); 73 String value = null; 74 if (selection.y == 0) { 75 value = detailsViewer.getDocument().get(); 76 } else { 77 try { 78 value = detailsViewer.getDocument().get(selection.x, selection.y); 79 } catch (BadLocationException e1) { 80 } 81 } 82 IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow(); 83 Shell activeShell= null; 84 if (window != null) { 85 activeShell= window.getShell(); 86 } 87 88 String modelIdentifier = variable.getModelIdentifier(); 89 IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier); 90 if (editor != null) { 91 if (editor.saveVariable(variable, value, activeShell)) { 92 return; 95 } 96 } 97 98 try { 99 if (variable.verifyValue(value)) { 101 variable.setValue(value); 102 } else { 103 if (activeShell != null) { 104 DebugUIPlugin.errorDialog(activeShell, ActionMessages.AssignValueAction_2, MessageFormat.format(ActionMessages.AssignValueAction_3, new String [] {value, variable.getName()}), new StatusInfo(IStatus.ERROR, ActionMessages.AssignValueAction_4)); } 106 } 107 } catch (DebugException e) { 108 MessageDialog.openError(activeShell, ActionMessages.AssignValueAction_0, ActionMessages.AssignValueAction_5); 109 } 110 111 } 112 113 116 public String getActionDefinitionId() { 117 return "org.eclipse.ui.file.save"; } 119 120 } 121 | Popular Tags |