2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
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.
42
package org.netbeans.modules.php.dbgp.models;
44
import java.util.ArrayList;
45
import java.util.Collection;
46
import java.util.List;
48
import javax.swing.Action;
50
import org.netbeans.api.debugger.Session;
51
import org.netbeans.api.project.FileOwnerQuery;
52
import org.netbeans.api.project.Project;
53
import org.netbeans.modules.php.dbgp.DebugSession;
54
import org.netbeans.modules.php.dbgp.StartActionProviderImpl;
55
import org.netbeans.modules.php.dbgp.api.SessionId;
56
import org.netbeans.modules.php.dbgp.packets.StatusCommand;
57
import org.netbeans.spi.debugger.ContextProvider;
58
import org.netbeans.spi.debugger.ui.Constants;
59
import org.netbeans.spi.viewmodel.ModelEvent;
60
import org.netbeans.spi.viewmodel.NodeActionsProvider;
61
import org.netbeans.spi.viewmodel.NodeModel;
62
import org.netbeans.spi.viewmodel.TableModel;
63
import org.netbeans.spi.viewmodel.TreeModel;
64
import org.netbeans.spi.viewmodel.UnknownTypeException;
65
import org.openide.filesystems.FileObject;
66
import org.openide.filesystems.FileUtil;
67
import org.openide.util.NbBundle;
73
public class ThreadsModel extends ViewModelSupport
74
implements TreeModel, NodeModel, NodeActionsProvider, TableModel
77
private static final String RUNNING_STATE
78
= "LBL_Running"; // NOI18N
80
private static final String SUSPENDED_STATE
81
= "LBL_Suspended"; // NOI18N
83
private static final String INACTIVE_THREAD_STATE
84
= "LBL_InactiveThreadState"; // NOI18N
86
private static final String ACTIVE_THREAD_STATE
87
= "LBL_ActiveThreadState"; // NOI18N
89
private static final String THREAD_NAME
90
= "LBL_ThreadName"; // NOI18N
92
public static final String CURRENT =
93
"org/netbeans/modules/debugger/resources/threadsView/CurrentThread"; // NOI18N
94
public static final String RUNNING =
95
"org/netbeans/modules/debugger/resources/threadsView/RunningThread"; // NOI18N
96
public static final String SUSPENDED =
97
"org/netbeans/modules/debugger/resources/threadsView/SuspendedThread";// NOI18N
99
public ThreadsModel(ContextProvider contextProvider) {
100
myProvider = contextProvider;
104
* @see org.netbeans.modules.php.dbgp.models.ViewModelSupport#clearModel()
107
public void clearModel() {
112
* @see org.netbeans.spi.viewmodel.TreeModel#getRoot()
114
public Object getRoot() {
118
public void update( ){
122
public void updateSession( DebugSession session ){
123
updateThreadState(session);
127
* @see org.netbeans.spi.viewmodel.TreeModel#getChildren(java.lang.Object, int, int)
129
public Object[] getChildren(Object parent, int from, int to)
130
throws UnknownTypeException
132
if (parent == ROOT) {
133
SessionId id = getSessionId();
135
return new Object[0];
137
Collection<DebugSession> collection =
138
StartActionProviderImpl.getInstance().getSessions(id);
139
if ( from >= collection.size() ){
140
return new Object[0];
142
int end = Math.min( to, collection.size());
143
if ( from == 0 && to == collection.size() ){
144
return collection.toArray( new Object[ collection.size() ]);
146
ArrayList<DebugSession> list = new ArrayList<DebugSession>(
148
List<DebugSession> result = list.subList( from , end );
149
return result.toArray( new Object[ result.size() ] );
152
throw new UnknownTypeException(parent);
156
* @see org.netbeans.spi.viewmodel.TreeModel#isLeaf(java.lang.Object)
158
public boolean isLeaf(Object node) throws UnknownTypeException {
162
else if (node instanceof DebugSession) {
166
throw new UnknownTypeException(node);
170
* @see org.netbeans.spi.viewmodel.TreeModel#getChildrenCount(java.lang.Object)
172
public int getChildrenCount(Object node) throws UnknownTypeException {
174
SessionId id = getSessionId();
178
return StartActionProviderImpl.getInstance().getSessions(id).size();
180
throw new UnknownTypeException(node);
184
* @see org.netbeans.spi.viewmodel.NodeModel#getDisplayName(java.lang.Object)
186
public String getDisplayName(Object node) throws UnknownTypeException {
187
if (node instanceof DebugSession ) {
188
DebugSession session = (DebugSession) node;
189
String scriptName = getScriptName( session );
191
return NbBundle.getMessage(ThreadsModel.class, THREAD_NAME,
194
else if (node == ROOT) {
195
return ROOT.toString();
198
throw new UnknownTypeException(node);
202
* @see org.netbeans.spi.viewmodel.NodeModel#getIconBase(java.lang.Object)
204
public String getIconBase(Object node) throws UnknownTypeException {
205
if (node instanceof DebugSession) {
206
DebugSession session = (DebugSession)node;
207
if ( session.getBridge().isSuspended()){
211
if ( isCurrent( session ) ) {
219
else if (node == ROOT) {
223
throw new UnknownTypeException(node);
227
* @see org.netbeans.spi.viewmodel.NodeModel#getShortDescription(java.lang.Object)
229
public String getShortDescription(Object node) throws UnknownTypeException {
233
else if (node instanceof DebugSession) {
234
return ((DebugSession)node).getFileName();
236
throw new UnknownTypeException(node);
240
* @see org.netbeans.spi.viewmodel.NodeActionsProvider#performDefaultAction(java.lang.Object)
242
public void performDefaultAction(Object node) throws UnknownTypeException {
243
if (node instanceof DebugSession) {
244
DebugSession session = (DebugSession) node;
245
SessionId id = getSessionId();
249
DebugSession current =
250
StartActionProviderImpl.getInstance().getCurrentSession(id);
252
if (! session.equals( current)) {
253
Session sess = getSession();
254
StartActionProviderImpl.getInstance().setCurrentSession(
256
StatusCommand command = new StatusCommand(
257
session.getTransactionId() );
258
session.sendCommandLater(command);
259
updateThreadState(current);
260
updateThreadState(session);
264
throw new UnknownTypeException(node);
268
* @see org.netbeans.spi.viewmodel.NodeActionsProvider#getActions(java.lang.Object)
270
public Action[] getActions(Object node) throws UnknownTypeException {
271
return new Action [] {};
275
* @see org.netbeans.spi.viewmodel.TableModel#getValueAt(java.lang.Object, java.lang.String)
277
public Object getValueAt(Object node, String columnID)
278
throws UnknownTypeException
284
if (node instanceof DebugSession) {
285
DebugSession session = (DebugSession)node;
287
if (columnID == Constants.THREAD_SUSPENDED_COLUMN_ID) {
288
return session.getBridge().isSuspended();
290
else if (columnID == Constants.THREAD_STATE_COLUMN_ID) {
291
String key = isCurrent(session) ? ACTIVE_THREAD_STATE :
292
INACTIVE_THREAD_STATE;
293
String value = session.getBridge().isSuspended() ?
294
SUSPENDED_STATE : RUNNING_STATE;
295
String result = NbBundle.getMessage(ThreadsModel.class,
296
key,NbBundle.getMessage(ThreadsModel.class, value ));
301
throw new UnknownTypeException(node);
305
* @see org.netbeans.spi.viewmodel.TableModel#isReadOnly(java.lang.Object, java.lang.String)
307
public boolean isReadOnly(Object node, String columnID)
308
throws UnknownTypeException
310
if (node == ROOT || node instanceof DebugSession ) {
314
throw new UnknownTypeException(node);
318
* @see org.netbeans.spi.viewmodel.TableModel#setValueAt(java.lang.Object, java.lang.String, java.lang.Object)
320
public void setValueAt(Object node, String columnID, Object value)
321
throws UnknownTypeException
323
throw new UnknownTypeException(node);
327
private void updateThreadState(DebugSession session) {
328
fireChangeEvent(new ModelEvent.NodeChanged(this, session));
331
private String getScriptName( DebugSession session ) {
332
SessionId id = session.getSessionId();
336
String fileName = session.getFileName();
337
FileObject script = id.getFileObjectByRemote( fileName );
338
Project project = FileOwnerQuery.getOwner( script );
339
return FileUtil.getRelativePath( project.getProjectDirectory(), script );
342
private Session getSession(){
343
return (Session)getContextProvider().lookupFirst( null , Session.class );
346
private SessionId getSessionId(){
347
ContextProvider provider = getContextProvider();
348
if ( provider == null ){
351
return (SessionId)provider.lookupFirst( null , SessionId.class );
354
private ContextProvider getContextProvider() {
358
private boolean isCurrent( DebugSession session ){
359
SessionId id = getSessionId();
360
DebugSession current =
361
StartActionProviderImpl.getInstance().getCurrentSession(id);
362
return session.equals( current );
365
private ContextProvider myProvider;