KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > variables > ChangeVariableValueAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions.variables;
12
13  
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.Iterator JavaDoc;
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.DebugPluginImages;
20 import org.eclipse.debug.internal.ui.DebugUIPlugin;
21 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
22 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
23 import org.eclipse.debug.internal.ui.VariableValueEditorManager;
24 import org.eclipse.debug.internal.ui.actions.ActionMessages;
25 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
26 import org.eclipse.debug.ui.IDebugUIConstants;
27 import org.eclipse.debug.ui.actions.IVariableValueEditor;
28 import org.eclipse.jface.dialogs.IInputValidator;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.actions.SelectionProviderAction;
34
35 /**
36  * Action for changing the value of primitives and <code>String</code> variables.
37  * This action will attempt to delegate the editing operation to a registered
38  * variable value editor, if any is provided for the variable's debug model.
39  * @see org.eclipse.debug.ui.actions.VariableValueEditorManager
40  */

41 public class ChangeVariableValueAction extends SelectionProviderAction {
42     
43     protected IVariable fVariable;
44     private VariablesView fView;
45     private boolean fEditing= false;
46     
47     /**
48      * Creates a new ChangeVariableValueAction for the given variables view
49      * @param view the variables view in which this action will appear
50      */

51     public ChangeVariableValueAction(VariablesView view) {
52         super(view.getViewer(), ActionMessages.ChangeVariableValue_title);
53         setDescription(ActionMessages.ChangeVariableValue_toolTipText);
54         setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_CHANGE_VARIABLE_VALUE));
55         setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_LCL_CHANGE_VARIABLE_VALUE));
56         setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_CHANGE_VARIABLE_VALUE));
57         PlatformUI.getWorkbench().getHelpSystem().setHelp(
58             this,
59             IDebugHelpContextIds.CHANGE_VALUE_ACTION);
60         fView= view;
61     }
62     
63     /**
64      * Edit the variable value with an in-line text editor.
65      */

66     protected void doActionPerformed(final IVariable variable) {
67         Shell shell = fView.getViewSite().getShell();
68         // If a previous edit is still in progress, don't start another
69
if (fEditing) {
70             return;
71         }
72         fEditing= true;
73         fVariable = variable;
74         if (!delegateEdit(shell)) {
75             doDefaultEdit(shell);
76         }
77         fEditing= false;
78     }
79     
80     /**
81      * Attempts to edit the variable by delegating to anyone who's
82      * contributed a variable value editor via extension. Returns
83      * <code>true</code> if a delegate handled the edit, <code>false</code>
84      * if the variable still needs to be edited.
85      *
86      * @param shell a shell for prompting the user
87      * @return whether or not a delegate attempted to edit the variable
88      */

89     private boolean delegateEdit(Shell shell) {
90         String JavaDoc modelIdentifier = fVariable.getModelIdentifier();
91         IVariableValueEditor editor= VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
92         if (editor != null) {
93             return editor.editVariable(fVariable, shell);
94         }
95         return false;
96     }
97
98     /**
99      * Edits the variable using the default variable editor
100      * @param shell a shell for prompting the user
101      */

102     protected void doDefaultEdit(Shell shell) {
103         String JavaDoc name= ""; //$NON-NLS-1$
104
String JavaDoc value= ""; //$NON-NLS-1$
105
try {
106             name= fVariable.getName();
107             value= fVariable.getValue().getValueString();
108         } catch (DebugException exception) {
109             DebugUIPlugin.errorDialog(shell, ActionMessages.ChangeVariableValue_errorDialogTitle,ActionMessages.ChangeVariableValue_errorDialogMessage, exception); //
110
return;
111         }
112         ChangeVariableValueInputDialog inputDialog= new ChangeVariableValueInputDialog(shell, ActionMessages.ChangeVariableValue_1, MessageFormat.format(ActionMessages.ChangeVariableValue_2, new String JavaDoc[] {name}), value, new IInputValidator() { //
113
/**
114              * Returns an error string if the input is invalid
115              */

116             public String JavaDoc isValid(String JavaDoc input) {
117                 try {
118                     if (fVariable.verifyValue(input)) {
119                         return null; // null means valid
120
}
121                 } catch (DebugException exception) {
122                     return ActionMessages.ChangeVariableValue_3;
123                 }
124                 return ActionMessages.ChangeVariableValue_4;
125             }
126         });
127         
128         inputDialog.open();
129         String JavaDoc newValue= inputDialog.getValue();
130         if (newValue != null) {
131             // null value means cancel was pressed
132
try {
133                 fVariable.setValue(newValue);
134                 getSelectionProvider().setSelection(new StructuredSelection(fVariable));
135             } catch (DebugException de) {
136                 DebugUIPlugin.errorDialog(shell, ActionMessages.ChangeVariableValue_errorDialogTitle,ActionMessages.ChangeVariableValue_errorDialogMessage, de); //
137
}
138         }
139     }
140         
141     /**
142      * Updates the enabled state of this action based
143      * on the selection
144      */

145     protected void update(IStructuredSelection sel) {
146         Iterator JavaDoc iter= sel.iterator();
147         if (iter.hasNext()) {
148             Object JavaDoc object= iter.next();
149             if (object instanceof IValueModification) {
150                 IValueModification varMod= (IValueModification)object;
151                 if (!varMod.supportsValueModification()) {
152                     setEnabled(false);
153                     return;
154                 }
155                 setEnabled(!iter.hasNext());
156                 return;
157             }
158         }
159         setEnabled(false);
160     }
161
162     /**
163      * @see IAction#run()
164      */

165     public void run() {
166         Iterator JavaDoc iterator= getStructuredSelection().iterator();
167         doActionPerformed((IVariable)iterator.next());
168     }
169     
170     /**
171      * @see SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
172      */

173     public void selectionChanged(IStructuredSelection sel) {
174         update(sel);
175     }
176 }
177
178
Popular Tags