KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.debug.core.DebugEvent;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.model.IStackFrame;
17 import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupManager;
18 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
19 import org.eclipse.jdt.debug.core.IJavaReferenceType;
20 import org.eclipse.jdt.debug.core.IJavaStackFrame;
21 import org.eclipse.jface.action.IAction;
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.SWT;
26 import org.eclipse.swt.events.MenuAdapter;
27 import org.eclipse.swt.events.MenuEvent;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Menu;
32 import org.eclipse.swt.widgets.MenuItem;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IWorkbenchPart;
35
36 /**
37  * Allows the user to choose what source to display from available strata
38  */

39 public class ShowStratumAction implements IObjectActionDelegate, IMenuCreator {
40     
41     private IStructuredSelection fSelection;
42     private IWorkbenchPart fPart;
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         fPart = targetPart;
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
53      */

54     public void run(IAction action) {
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
59      */

60     public void selectionChanged(IAction action, ISelection selection) {
61         if (selection instanceof IStructuredSelection) {
62             fSelection = (IStructuredSelection) selection;
63             action.setMenuCreator(this);
64         } else {
65             action.setMenuCreator(null);
66         }
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
71      */

72     public Menu getMenu(Control parent) {
73         return null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
78      */

79     public Menu getMenu(Menu parent) {
80         //Create the new menu. The menu will get filled when it is about to be shown. see fillMenu(Menu).
81
Menu menu = new Menu(parent);
82         menu.addMenuListener(new MenuAdapter() {
83             public void menuShown(MenuEvent e) {
84                 Menu m = (Menu)e.widget;
85                 MenuItem[] items = m.getItems();
86                 for (int i=0; i < items.length; i++) {
87                     items[i].dispose();
88                 }
89                 fillMenu(m);
90             }
91         });
92         return menu;
93     }
94
95     /**
96      * Fills the given menu with available stratum.
97      *
98      * @param m
99      */

100     private void fillMenu(Menu m) {
101         IStackFrame frame = (IStackFrame) fSelection.getFirstElement();
102         final IJavaStackFrame javaStackFrame = (IJavaStackFrame) frame.getAdapter(IJavaStackFrame.class);
103         try {
104             IJavaReferenceType declaringType = javaStackFrame.getReferenceType();
105             final IJavaDebugTarget target = (IJavaDebugTarget) javaStackFrame.getDebugTarget();
106             String JavaDoc currentStratum = target.getDefaultStratum();
107             String JavaDoc[] strata = declaringType.getAvailableStrata();
108             for (int i = 0; i < strata.length; i++) {
109                 final String JavaDoc stratum = strata[i];
110                 MenuItem item = createMenuItem(m, stratum, javaStackFrame, target);
111                 item.setSelection(stratum.equals(currentStratum));
112             }
113             MenuItem item = createMenuItem(m, null, javaStackFrame, target);
114             item.setSelection(currentStratum == null);
115         } catch (DebugException e) {
116         }
117     }
118     
119     private MenuItem createMenuItem(Menu m, final String JavaDoc stratum, final IJavaStackFrame frame, final IJavaDebugTarget target) {
120         MenuItem item = new MenuItem(m, SWT.CHECK);
121         if (stratum == null) {
122             item.setText(ActionMessages.ShowStratumAction_0);
123         } else {
124             item.setText(stratum);
125         }
126         item.addSelectionListener(new SelectionAdapter() {
127             public void widgetSelected(SelectionEvent e) {
128                 if (((MenuItem)e.getSource()).getSelection()) {
129                     target.setDefaultStratum(stratum);
130                 } else {
131                     target.setDefaultStratum(null);
132                 }
133                 DebugEvent event = new DebugEvent(frame, DebugEvent.CHANGE, DebugEvent.CONTENT);
134                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{event});
135                 // TODO: post 3.2 - make API to force source lookup
136
SourceLookupManager.getDefault().displaySource(frame, fPart.getSite().getPage(), true);
137             }
138         });
139         return item;
140     }
141
142     /* (non-Javadoc)
143      * @see org.eclipse.jface.action.IMenuCreator#dispose()
144      */

145     public void dispose() {
146     }
147     
148
149 }
150
Popular Tags