KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > JPDASessionActionsProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ui.models;
21
22 import org.netbeans.api.debugger.DebuggerEngine;
23 import org.netbeans.spi.viewmodel.NodeActionsProviderFilter;
24 import org.netbeans.spi.viewmodel.NodeActionsProvider;
25 import org.netbeans.spi.viewmodel.UnknownTypeException;
26 import org.netbeans.spi.viewmodel.ModelListener;
27 import org.netbeans.api.debugger.Session;
28 import org.netbeans.api.debugger.jpda.JPDADebugger;
29 import org.netbeans.spi.debugger.ContextProvider;
30
31 import org.openide.util.actions.Presenter;
32 import org.openide.util.NbBundle;
33
34 import javax.swing.*;
35 import java.util.*;
36 import java.awt.event.ActionEvent JavaDoc;
37
38 /**
39  * Provides popup menu for JPDA session nodes: suspend options and language selection.
40  *
41  * @author Maros Sandor
42  */

43 public class JPDASessionActionsProvider implements NodeActionsProviderFilter {
44
45     private HashSet listeners;
46     
47
48     public JPDASessionActionsProvider () {
49     }
50
51     public void performDefaultAction(NodeActionsProvider original, Object JavaDoc node) throws UnknownTypeException {
52         original.performDefaultAction(node);
53     }
54
55     public Action [] getActions(NodeActionsProvider original, Object JavaDoc node) throws UnknownTypeException {
56
57         if (!(node instanceof Session) || !SessionsTableModelFilter.isJPDASession((Session) node)) {
58             return original.getActions(node);
59         }
60         Session session = (Session) node;
61         Action [] actions;
62         try {
63             actions = original.getActions(node);
64         } catch (UnknownTypeException e) {
65             actions = new Action[0];
66         }
67         List myActions = new ArrayList();
68         DebuggerEngine e = session.getCurrentEngine ();
69         if (e != null) {
70             JPDADebugger d = (JPDADebugger) e.lookupFirst(null, JPDADebugger.class);
71             myActions.add(new CustomizeSession(d));
72         }
73         myActions.add(new LanguageSelection(session));
74         myActions.addAll(Arrays.asList(actions));
75         return (Action[]) myActions.toArray(new Action[myActions.size()]);
76     }
77
78     private String JavaDoc localize(String JavaDoc s) {
79         return NbBundle.getBundle(JPDASessionActionsProvider.class).getString(s);
80     }
81
82     private class LanguageSelection extends AbstractAction implements Presenter.Popup {
83
84         private Session session;
85
86         public LanguageSelection(Session session) {
87             this.session = session;
88         }
89
90         public void actionPerformed(ActionEvent JavaDoc e) {
91         }
92
93         public JMenuItem getPopupPresenter() {
94             JMenu displayAsPopup = new JMenu(localize("CTL_Session_Popup_Language"));
95
96             String JavaDoc [] languages = session.getSupportedLanguages();
97             String JavaDoc currentLanguage = session.getCurrentLanguage();
98             for (int i = 0; i < languages.length; i++) {
99                 final String JavaDoc language = languages[i];
100                 JRadioButtonMenuItem langItem = new JRadioButtonMenuItem(new AbstractAction(language) {
101                     public void actionPerformed(ActionEvent JavaDoc e) {
102                         session.setCurrentLanguage(language);
103                     }
104                 });
105                 if (currentLanguage.equals(language)) langItem.setSelected(true);
106                 displayAsPopup.add(langItem);
107             }
108             return displayAsPopup;
109         }
110     }
111
112     private class CustomizeSession extends AbstractAction implements Presenter.Popup {
113
114         private JPDADebugger dbg;
115
116         public CustomizeSession(JPDADebugger dbg) {
117             this.dbg = dbg;
118         }
119
120         public void actionPerformed(ActionEvent JavaDoc e) {
121         }
122
123         public JMenuItem getPopupPresenter() {
124             JMenu displayAsPopup = new JMenu
125                 (localize ("CTL_Session_Resume_Threads"));
126
127             JRadioButtonMenuItem resumeAllItem = new JRadioButtonMenuItem (
128                 new AbstractAction (localize ("CTL_Session_Resume_All_Threads")
129             ) {
130                 public void actionPerformed (ActionEvent JavaDoc e) {
131                     dbg.setSuspend (JPDADebugger.SUSPEND_ALL);
132                 }
133             });
134             JRadioButtonMenuItem resumeCurrentItem = new JRadioButtonMenuItem (
135                 new AbstractAction (localize ("CTL_Session_Resume_Current_Thread")
136             ) {
137                 public void actionPerformed(ActionEvent JavaDoc e) {
138                     dbg.setSuspend (JPDADebugger.SUSPEND_EVENT_THREAD);
139                 }
140             });
141
142             if (dbg.getSuspend() == JPDADebugger.SUSPEND_ALL)
143                 resumeAllItem.setSelected(true);
144             else resumeCurrentItem.setSelected(true);
145
146             displayAsPopup.add(resumeAllItem);
147             displayAsPopup.add(resumeCurrentItem);
148             return displayAsPopup;
149         }
150     }
151
152     public void addModelListener(ModelListener l) {
153         HashSet newListeners = (listeners == null) ? new HashSet() : (HashSet) listeners.clone();
154         newListeners.add(l);
155         listeners = newListeners;
156     }
157
158     public void removeModelListener(ModelListener l) {
159         if (listeners == null) return;
160         HashSet newListeners = (HashSet) listeners.clone();
161         newListeners.remove(l);
162         listeners = newListeners;
163     }
164
165 }
166
Popular Tags