KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > EditVariableLogicalStructureAction


1 /*******************************************************************************
2  * Copyright (c) 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 implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.debug.core.DebugException;
14 import org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILogicalStructureType;
16 import org.eclipse.debug.core.model.IValue;
17 import org.eclipse.jdt.debug.core.IJavaVariable;
18 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructure;
19 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructures;
20 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaStructureErrorValue;
21 import org.eclipse.jdt.internal.debug.ui.EditLogicalStructureDialog;
22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IObjectActionDelegate;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.actions.ActionDelegate;
31
32 /**
33  * Action which prompts the user to edit the logical structure that
34  * is currently active on the given object.
35  */

36 public class EditVariableLogicalStructureAction extends ActionDelegate implements IObjectActionDelegate {
37     
38     /**
39      * The editable structure for the currently selected variable or
40      * <code>null</code> if none.
41      */

42     private JavaLogicalStructure fStructure= null;
43
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
46      */

47     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
48     }
49
50     /**
51      * Prompt the user to edit the logical structure associated with the currently
52      * selected variable.
53      */

54     public void run(IAction action) {
55         if (fStructure == null) {
56             return;
57         }
58         Shell shell= JDIDebugUIPlugin.getActiveWorkbenchShell();
59         if (shell != null) {
60             EditLogicalStructureDialog dialog= new EditLogicalStructureDialog(shell, fStructure);
61             if (dialog.open() == Window.OK) {
62                 JavaLogicalStructures.saveUserDefinedJavaLogicalStructures();
63             }
64         }
65     }
66     
67     /**
68      * @see ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
69      */

70     public void selectionChanged(IAction action, ISelection selection) {
71         fStructure= null;
72         Object JavaDoc element = ((IStructuredSelection) selection).getFirstElement();
73         if (element instanceof IJavaVariable) {
74             try {
75                 IValue value= ((IJavaVariable) element).getValue();
76                 if (value instanceof JavaStructureErrorValue) {
77                     value= ((JavaStructureErrorValue) value).getParentValue();
78                 }
79                 ILogicalStructureType type= getLogicalStructure(value);
80                 if (type instanceof JavaLogicalStructure) {
81                     JavaLogicalStructure javaStructure= (JavaLogicalStructure) type;
82                     if (!javaStructure.isContributed()) {
83                         fStructure= javaStructure;
84                     }
85                 }
86             } catch (DebugException e) {
87                 JDIDebugUIPlugin.log(e.getStatus());
88             }
89         }
90         action.setEnabled(fStructure != null);
91     }
92     
93     /**
94      * Returns the logical structure currently associated with the given
95      * value or <code>null</code> if none.
96      * @param value the value
97      * @return the logical structure currently associated with the given
98      * value or <code>null</code> if none.
99      */

100     public static ILogicalStructureType getLogicalStructure(IValue value) {
101         // This code is based on VariablesViewContentProvider#getLogicalValue(IValue)
102
ILogicalStructureType type = null;
103         ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value);
104         if (types.length > 0) {
105             type= DebugPlugin.getDefaultStructureType(types);
106         }
107         return type;
108     }
109 }
110
Popular Tags