KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.details;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.model.IValueModification;
16 import org.eclipse.debug.core.model.IVariable;
17 import org.eclipse.debug.internal.ui.DebugUIPlugin;
18 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
19 import org.eclipse.debug.internal.ui.VariableValueEditorManager;
20 import org.eclipse.debug.internal.ui.actions.ActionMessages;
21 import org.eclipse.debug.internal.ui.actions.StatusInfo;
22 import org.eclipse.debug.ui.actions.IVariableValueEditor;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.commands.ActionHandler;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IViewSite;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.handlers.IHandlerActivation;
35 import org.eclipse.ui.handlers.IHandlerService;
36 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
37
38 import com.ibm.icu.text.MessageFormat;
39
40 /**
41  * Action which assigns a value to a variable from the detail pane
42  * of the variables view.
43  */

44 public class DetailPaneAssignValueAction extends Action{
45
46     private IHandlerActivation fHandlerActivation;
47     private IViewSite fViewSite;
48     private ITextViewer fTextViewer;
49     private IStructuredSelection fCurrentSelection;
50     
51     /**
52      * Attempts to evaluate the given string expression and assign the resulting value to the
53      * specified variable. Displays error dialogs to the user if a problem is encountered.
54      *
55      * @see DetailPaneAssignValueAction
56      * @see org.eclipse.debug.internal.ui.elements.adapters.DefaultVariableCellModifier
57      *
58      * @param shell the shell to use to open dialogs
59      * @param variable the variable that is getting a new value
60      * @param newValueExpression the expression to evaluate and set as the new value
61      * @since 3.3.0
62      */

63     public static void assignValue(Shell shell, IVariable variable, String JavaDoc newValueExpression){
64         String JavaDoc modelIdentifier = variable.getModelIdentifier();
65         IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
66         if (editor != null) {
67             if (editor.saveVariable(variable, newValueExpression, shell)) {
68                 // If we successfully delegate to an editor which performs the save,
69
// don't do any more work.
70
return;
71             }
72         }
73         
74         try {
75             // If we failed to delegate to anyone, perform the default assignment.
76
if (variable.verifyValue(newValueExpression)) {
77                 variable.setValue(newValueExpression);
78             } else {
79                 if (shell != null) {
80                     DebugUIPlugin.errorDialog(shell, ActionMessages.DetailPaneAssignValueAction_2, MessageFormat.format(ActionMessages.DetailPaneAssignValueAction_3, new String JavaDoc[] {newValueExpression, variable.getName()}), new StatusInfo(IStatus.ERROR, ActionMessages.DetailPaneAssignValueAction_4)); //
81
}
82             }
83         } catch (DebugException e) {
84             MessageDialog.openError(shell, ActionMessages.DetailPaneAssignValueAction_0, e.getStatus().getMessage());
85         }
86     }
87     
88     public DetailPaneAssignValueAction(ITextViewer textViewer, IViewSite viewSite) {
89         super(ActionMessages.DetailPaneAssignValueAction_1);
90         
91         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.DETAIL_PANE_ASSIGN_VALUE_ACTION);
92
93         fTextViewer = textViewer;
94         fViewSite = viewSite;
95         
96         setEnabled(false);
97         IHandlerService service = (IHandlerService) fViewSite.getService(IHandlerService.class);
98         ActionHandler handler = new ActionHandler(this);
99         fHandlerActivation = service.activateHandler(getActionDefinitionId(), handler);
100     }
101         
102     public void dispose() {
103         IHandlerService service = (IHandlerService) fViewSite.getService(IHandlerService.class);
104         service.deactivateHandler(fHandlerActivation);
105     }
106
107     public void updateCurrentVariable(IStructuredSelection selection) {
108         boolean enabled = false;
109         if ((selection.size() == 1) && (selection.getFirstElement() instanceof IValueModification)) {
110             IValueModification valMod = (IValueModification) selection.getFirstElement();
111             if (valMod.supportsValueModification()) {
112                 fCurrentSelection = selection;
113                 enabled = true;
114             }
115         }
116         setEnabled(enabled);
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.action.IAction#run()
121      */

122     public void run() {
123         IVariable variable = (IVariable) fCurrentSelection.getFirstElement();
124         
125         Point selection = fTextViewer.getSelectedRange();
126         String JavaDoc value = null;
127         if (selection.y == 0) {
128             value = fTextViewer.getDocument().get();
129         } else {
130             try {
131                 value = fTextViewer.getDocument().get(selection.x, selection.y);
132             } catch (BadLocationException e1) {
133             }
134         }
135         IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
136         Shell activeShell= null;
137         if (window != null) {
138             activeShell= window.getShell();
139         }
140         
141         assignValue(activeShell, variable, value);
142     }
143     
144     /* (non-Javadoc)
145      * @see org.eclipse.jface.action.IAction#getActionDefinitionId()
146      */

147     public String JavaDoc getActionDefinitionId() {
148         return IWorkbenchActionDefinitionIds.SAVE;
149     }
150
151 }
152
Popular Tags