~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to debuggerjpda/ui/src/org/netbeans/modules/debugger/jpda/ui/models/ThreadsActionsProvider.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.debugger.jpda.ui.models;
 
43
 
 
44
import java.awt.event.ActionEvent;
 
45
import java.util.Vector;
 
46
import javax.swing.Action;
 
47
 
 
48
import org.netbeans.api.debugger.DebuggerEngine;
 
49
import org.netbeans.api.debugger.DebuggerManager;
 
50
import org.netbeans.spi.debugger.ContextProvider;
 
51
import org.netbeans.api.debugger.jpda.JPDADebugger;
 
52
import org.netbeans.api.debugger.jpda.JPDAThread;
 
53
import org.netbeans.api.debugger.jpda.JPDAThreadGroup;
 
54
import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
 
55
import org.netbeans.modules.debugger.jpda.ui.SourcePath;
 
56
import org.netbeans.spi.viewmodel.NodeActionsProvider;
 
57
import org.netbeans.spi.viewmodel.ModelListener;
 
58
import org.netbeans.spi.viewmodel.UnknownTypeException;
 
59
import org.netbeans.spi.viewmodel.Models;
 
60
import org.netbeans.spi.viewmodel.TreeModel;
 
61
import org.openide.util.NbBundle;
 
62
 
 
63
 
 
64
/**
 
65
 * @author   Jan Jancura
 
66
 */
 
67
public class ThreadsActionsProvider implements NodeActionsProvider {
 
68
 
 
69
    private Action MAKE_CURRENT_ACTION = Models.createAction (
 
70
        NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_MakeCurrent_Label"),
 
71
        new Models.ActionPerformer () {
 
72
            public boolean isEnabled (Object node) {
 
73
                if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
 
74
                return debugger.getCurrentThread () != node;
 
75
            }
 
76
            
 
77
            public void perform (Object[] nodes) {
 
78
                if (nodes[0] instanceof MonitorModel.ThreadWithBordel) nodes[0] = ((MonitorModel.ThreadWithBordel) nodes[0]).originalThread;
 
79
                ((JPDAThread) nodes [0]).makeCurrent ();
 
80
            }
 
81
        },
 
82
        Models.MULTISELECTION_TYPE_EXACTLY_ONE
 
83
    );
 
84
 
 
85
    private static Action GO_TO_SOURCE_ACTION = Models.createAction (
 
86
        NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_GoToSource_Label"),
 
87
        new Models.ActionPerformer () {
 
88
            public boolean isEnabled (Object node) {
 
89
                if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
 
90
                return isGoToSourceSupported ((JPDAThread) node);
 
91
            }
 
92
            
 
93
            public void perform (Object[] nodes) {
 
94
                if (nodes[0] instanceof MonitorModel.ThreadWithBordel) nodes[0] = ((MonitorModel.ThreadWithBordel) nodes[0]).originalThread;
 
95
                String language = DebuggerManager.getDebuggerManager ().
 
96
                    getCurrentSession ().getCurrentLanguage ();
 
97
                SourcePath sp = (SourcePath) DebuggerManager.
 
98
                    getDebuggerManager ().getCurrentEngine ().lookupFirst 
 
99
                    (null, SourcePath.class);
 
100
                sp.showSource ((JPDAThread) nodes [0], language);
 
101
            }
 
102
        },
 
103
        Models.MULTISELECTION_TYPE_EXACTLY_ONE
 
104
    );
 
105
 
 
106
    private Action SUSPEND_ACTION = Models.createAction (
 
107
        NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Suspend_Label"),
 
108
        new Models.ActionPerformer () {
 
109
            public boolean isEnabled (Object node) {
 
110
                if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
 
111
                if (node instanceof JPDAThread)
 
112
                    return !((JPDAThread) node).isSuspended ();
 
113
                else
 
114
                    return true;
 
115
            }
 
116
            
 
117
            public void perform (Object[] nodes) {
 
118
                int i, k = nodes.length;
 
119
                for (i = 0; i < k; i++) {
 
120
                    Object node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ? 
 
121
                            ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
 
122
                    if (node instanceof JPDAThread)
 
123
                        ((JPDAThread) node).suspend ();
 
124
                    else
 
125
                        ((JPDAThreadGroup) node).suspend ();
 
126
                }
 
127
            }
 
128
        },
 
129
        Models.MULTISELECTION_TYPE_ALL
 
130
    );
 
131
 
 
132
    private Action RESUME_ACTION = Models.createAction (
 
133
        NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Resume_Label"),
 
134
        new Models.ActionPerformer () {
 
135
            public boolean isEnabled (Object node) {
 
136
                if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
 
137
                if (node instanceof JPDAThread)
 
138
                    return ((JPDAThread) node).isSuspended ();
 
139
                else
 
140
                    return true;
 
141
            }
 
142
            
 
143
            public void perform (Object[] nodes) {
 
144
                int i, k = nodes.length;
 
145
                for (i = 0; i < k; i++) {
 
146
                    Object node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ? 
 
147
                            ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
 
148
                    if (node instanceof JPDAThread)
 
149
                        ((JPDAThread) node).resume ();
 
150
                    else
 
151
                        ((JPDAThreadGroup) node).resume ();
 
152
                }
 
153
            }
 
154
        },
 
155
        Models.MULTISELECTION_TYPE_ALL
 
156
    );
 
157
        
 
158
    private Action INTERRUPT_ACTION = Models.createAction (
 
159
        NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Interrupt_Label"),
 
160
        new Models.ActionPerformer () {
 
161
            public boolean isEnabled (Object node) {
 
162
                if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
 
163
                if (node instanceof JPDAThread)
 
164
                    return !((JPDAThread) node).isSuspended ();
 
165
                else
 
166
                    return false;
 
167
            }
 
168
            
 
169
            public void perform (Object[] nodes) {
 
170
                int i, k = nodes.length;
 
171
                for (i = 0; i < k; i++) {
 
172
                    Object node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ? 
 
173
                            ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
 
174
                    if (node instanceof JPDAThread) {
 
175
                        ((JPDAThread) node).interrupt();
 
176
                    }
 
177
                }
 
178
            }
 
179
        },
 
180
        Models.MULTISELECTION_TYPE_ALL
 
181
    );
 
182
        
 
183
    private JPDADebugger debugger;
 
184
    
 
185
    
 
186
    public ThreadsActionsProvider (ContextProvider lookupProvider) {
 
187
        debugger = (JPDADebugger) lookupProvider.
 
188
            lookupFirst (null, JPDADebugger.class);
 
189
    }
 
190
    
 
191
    public Action[] getActions (Object node) throws UnknownTypeException {
 
192
        if (node == TreeModel.ROOT) 
 
193
            return new Action [0];
 
194
        if (node instanceof JPDAThreadGroup) {
 
195
            return new Action [] {
 
196
                RESUME_ACTION,
 
197
                SUSPEND_ACTION,
 
198
            };
 
199
        } else
 
200
        if (node instanceof JPDAThread) {
 
201
            JPDAThread t = (JPDAThread) node;
 
202
            boolean suspended = t.isSuspended ();
 
203
            Action a = null;
 
204
            if (suspended)
 
205
                a = RESUME_ACTION;
 
206
            else
 
207
                a = SUSPEND_ACTION;
 
208
            return new Action [] {
 
209
                MAKE_CURRENT_ACTION,
 
210
                a,
 
211
                INTERRUPT_ACTION,
 
212
                GO_TO_SOURCE_ACTION,
 
213
            };
 
214
        } else
 
215
        throw new UnknownTypeException (node);
 
216
    }
 
217
    
 
218
    public void performDefaultAction (Object node) throws UnknownTypeException {
 
219
        if (node == TreeModel.ROOT) 
 
220
            return;
 
221
        if (node instanceof JPDAThread)
 
222
            ((JPDAThread) node).makeCurrent ();
 
223
        else
 
224
        if (node instanceof JPDAThreadGroup) 
 
225
            return;
 
226
        else
 
227
        throw new UnknownTypeException (node);
 
228
    }
 
229
 
 
230
    /** 
 
231
     *
 
232
     * @param l the listener to add
 
233
     */
 
234
    public void addModelListener (ModelListener l) {
 
235
    }
 
236
 
 
237
    /** 
 
238
     *
 
239
     * @param l the listener to remove
 
240
     */
 
241
    public void removeModelListener (ModelListener l) {
 
242
    }
 
243
 
 
244
    private static boolean isGoToSourceSupported (JPDAThread t) {
 
245
        String language = DebuggerManager.getDebuggerManager ().
 
246
            getCurrentSession ().getCurrentLanguage ();
 
247
        if (!t.isSuspended ())
 
248
            return false;
 
249
        String className = t.getClassName ();
 
250
        SourcePath sp = (SourcePath) DebuggerManager.
 
251
            getDebuggerManager ().getCurrentEngine ().lookupFirst 
 
252
            (null, SourcePath.class);
 
253
        return sp.sourceAvailable (t, language, true);
 
254
    }
 
255
}