KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.text.MessageFormat JavaDoc;
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 /**
35  * Action which assigns a value to a variable from the detail pane
36  * of the variables view.
37  */

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); //$NON-NLS-1$
44
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     /* (non-Javadoc)
52      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
53      */

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     /* (non-Javadoc)
67      * @see org.eclipse.jface.action.IAction#run()
68      */

69     public void run() {
70         IVariable variable = (IVariable) getStructuredSelection().getFirstElement();
71         
72         Point selection = detailsViewer.getSelectedRange();
73         String JavaDoc 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 JavaDoc modelIdentifier = variable.getModelIdentifier();
89         IVariableValueEditor editor = VariableValueEditorManager.getDefault().getVariableValueEditor(modelIdentifier);
90         if (editor != null) {
91             if (editor.saveVariable(variable, value, activeShell)) {
92                 // If we successfully delegate to an editor which performs the save,
93
// don't do any more work.
94
return;
95             }
96         }
97         
98         try {
99             // If we failed to delegate to anyone, perform the default assignment.
100
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 JavaDoc[] {value, variable.getName()}), new StatusInfo(IStatus.ERROR, ActionMessages.AssignValueAction_4)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
105
}
106             }
107         } catch (DebugException e) {
108             MessageDialog.openError(activeShell, ActionMessages.AssignValueAction_0, ActionMessages.AssignValueAction_5);
109         }
110         
111     }
112     
113     /* (non-Javadoc)
114      * @see org.eclipse.jface.action.IAction#getActionDefinitionId()
115      */

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