KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13  
14 import java.text.MessageFormat JavaDoc;
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.views.variables.VariablesView;
25 import org.eclipse.debug.ui.IDebugUIConstants;
26 import org.eclipse.debug.ui.actions.IVariableValueEditor;
27 import org.eclipse.jface.dialogs.IInputValidator;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.actions.SelectionProviderAction;
33
34 /**
35  * Action for changing the value of primitives and <code>String</code> variables.
36  * This action will attempt to delegate the editing operation to a registered
37  * variable value editor, if any is provided for the variable's debug model.
38  * @see org.eclipse.debug.ui.actions.VariableValueEditorManager
39  */

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

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

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

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

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

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

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

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

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