KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.ibm.icu.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.actions.ActionMessages;
23 import org.eclipse.debug.internal.ui.actions.StatusInfo;
24 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
25 import org.eclipse.debug.ui.actions.IVariableValueEditor;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.source.ISourceViewer;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.SelectionProviderAction;
35
36 /**
37  * Action which assigns a value to a variable from the detail pane
38  * of the variables view.
39  */

40 public class AssignValueAction extends SelectionProviderAction {
41     private VariablesView variablesView;
42     private ISourceViewer detailsViewer;
43
44     public AssignValueAction(VariablesView varView, ISourceViewer detailViewer) {
45         super(varView.getViewer(), ActionMessages.AssignValueAction_1);
46         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.ASSIGN_VALUE_ACTION);
47         variablesView = varView;
48         detailsViewer = detailViewer;
49         setEnabled(false);
50         variablesView.getSite().getKeyBindingService().registerAction(this);
51     }
52         
53     /* (non-Javadoc)
54      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
55      */

56     public void selectionChanged(IStructuredSelection selection) {
57         boolean enabled = false;
58         if ((selection.size() == 1) && (selection.getFirstElement() instanceof IValueModification)) {
59             IValueModification valMod = (IValueModification) selection.getFirstElement();
60             if (valMod.supportsValueModification()) {
61                 super.selectionChanged(selection);
62                 enabled = true;
63             }
64         }
65         setEnabled(enabled);
66     }
67     
68     /* (non-Javadoc)
69      * @see org.eclipse.jface.action.IAction#run()
70      */

71     public void run() {
72         IVariable variable = (IVariable) getStructuredSelection().getFirstElement();
73         
74         Point selection = detailsViewer.getSelectedRange();
75         String JavaDoc value = null;
76         if (selection.y == 0) {
77             value = detailsViewer.getDocument().get();
78         } else {
79             try {
80                 value = detailsViewer.getDocument().get(selection.x, selection.y);
81             } catch (BadLocationException e1) {
82             }
83         }
84         IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
85         Shell activeShell= null;
86         if (window != null) {
87             activeShell= window.getShell();
88         }
89         
90         String JavaDoc modelIdentifier = variable.getModelIdentifier();
91         IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
92         if (editor != null) {
93             if (editor.saveVariable(variable, value, activeShell)) {
94                 // If we successfully delegate to an editor which performs the save,
95
// don't do any more work.
96
return;
97             }
98         }
99         
100         try {
101             // If we failed to delegate to anyone, perform the default assignment.
102
if (variable.verifyValue(value)) {
103                 variable.setValue(value);
104             } else {
105                 if (activeShell != null) {
106                     DebugUIPlugin.errorDialog(activeShell, ActionMessages.AssignValueAction_2, MessageFormat.format(ActionMessages.AssignValueAction_3, new String JavaDoc[] {value, variable.getName()}), new StatusInfo(IStatus.ERROR, ActionMessages.AssignValueAction_4)); //
107
}
108             }
109         } catch (DebugException e) {
110             MessageDialog.openError(activeShell, ActionMessages.AssignValueAction_0, ActionMessages.AssignValueAction_5);
111         }
112         
113     }
114     
115     /* (non-Javadoc)
116      * @see org.eclipse.jface.action.IAction#getActionDefinitionId()
117      */

118     public String JavaDoc getActionDefinitionId() {
119         return "org.eclipse.ui.file.save"; //$NON-NLS-1$
120
}
121
122 }
123
Popular Tags