KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > AvailableLogicalStructuresAction


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.views.variables;
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.IExpression;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.debug.core.model.IVariable;
19 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.ActionContributionItem;
22 import org.eclipse.jface.action.IMenuCreator;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.ui.PlatformUI;
28
29 /**
30  * Drop down action that displays available logical structures for a selected
31  * variable or expression.
32  */

33 public class AvailableLogicalStructuresAction extends Action implements IMenuCreator {
34     
35     private VariablesView fView;
36     private Menu fMenu;
37     private IValue fValue;
38     private ILogicalStructureType[] fTypes;
39
40     public AvailableLogicalStructuresAction(VariablesView view) {
41         setView(view);
42         setToolTipText(VariablesViewMessages.AvailableLogicalStructuresAction_0);
43         setText(VariablesViewMessages.AvailableLogicalStructuresAction_1);
44         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.VARIABLES_SELECT_LOGICAL_STRUCTURE);
45         setEnabled(false);
46         setMenuCreator(this);
47         init();
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.action.IAction#run()
52      */

53     public void run() {
54     }
55     
56     protected VariablesView getView() {
57         return fView;
58     }
59
60     protected void setView(VariablesView view) {
61         fView = view;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.action.IMenuCreator#dispose()
66      */

67     public void dispose() {
68         if (fMenu != null) {
69             fMenu.dispose();
70         }
71         fView= null;
72         fValue = null;
73         fTypes = null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
78      */

79     public Menu getMenu(Control parent) {
80         return null;
81     }
82
83     protected void addActionToMenu(Menu parent, Action action) {
84         ActionContributionItem item= new ActionContributionItem(action);
85         item.fill(parent, -1);
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
90      */

91     public Menu getMenu(Menu parent) {
92         if (fMenu != null) {
93             fMenu.dispose();
94         }
95         
96         fMenu= new Menu(parent);
97         ILogicalStructureType[] types = getTypes();
98         ILogicalStructureType enabledType = DebugPlugin.getDefaultStructureType(types);
99         if (types != null && types.length > 0) {
100             for (int i = 0; i < types.length; i++) {
101                 ILogicalStructureType type= types[i];
102                 Action action = new SelectLogicalStructureAction(getView(), type, getValue(), types);
103                 action.setChecked(enabledType == type);
104                 StringBuffer JavaDoc label= new StringBuffer JavaDoc();
105                 //add the numerical accelerator
106
if (i < 9) {
107                     label.append('&');
108                     label.append(i + 1);
109                     label.append(' ');
110                 }
111                 label.append(action.getText());
112                 action.setText(label.toString());
113                 addActionToMenu(fMenu, action);
114             }
115         }
116         return fMenu;
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
121      */

122     public void init() {
123         setValue(null);
124         setTypes(null);
125         if (getView().isShowLogicalStructure()) {
126             ISelection s = getView().getViewer().getSelection();
127             if (s instanceof IStructuredSelection) {
128                 IStructuredSelection selection = (IStructuredSelection) s;
129                 if (selection.size() == 1) {
130                     Object JavaDoc obj = selection.getFirstElement();
131                     IValue value = null;
132                     if (obj instanceof IVariable) {
133                         IVariable var = (IVariable) obj;
134                         try {
135                             value = var.getValue();
136                         } catch (DebugException e) {
137                         }
138                     } else if (obj instanceof IExpression) {
139                         IExpression expression = (IExpression)obj;
140                         value = expression.getValue();
141                     }
142                     if (value != null) {
143                         ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value);
144                         if (types.length > 0) {
145                             setTypes(types);
146                             setValue(value);
147                             setEnabled(true);
148                             return;
149                         }
150                     }
151                 }
152             }
153         }
154         setEnabled(false);
155     }
156     
157     protected ILogicalStructureType[] getTypes() {
158         return fTypes;
159     }
160     
161     private void setTypes(ILogicalStructureType[] types) {
162         fTypes = types;
163     }
164     
165     protected IValue getValue() {
166         return fValue;
167     }
168     
169     private void setValue(IValue value) {
170         fValue = value;
171     }
172 }
173
Popular Tags