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

« back to all changes in this revision

Viewing changes to scripting/php/debugger/src/org/netbeans/modules/php/dbgp/models/ThreadsModel.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-2007 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.php.dbgp.models;
 
43
 
 
44
import java.util.ArrayList;
 
45
import java.util.Collection;
 
46
import java.util.List;
 
47
 
 
48
import javax.swing.Action;
 
49
 
 
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;
 
68
 
 
69
/**
 
70
 *
 
71
 * @author ads
 
72
 */
 
73
public class ThreadsModel extends ViewModelSupport
 
74
        implements TreeModel, NodeModel, NodeActionsProvider, TableModel 
 
75
{
 
76
 
 
77
    private static final String RUNNING_STATE 
 
78
                                      = "LBL_Running";                        // NOI18N
 
79
 
 
80
    private static final String SUSPENDED_STATE 
 
81
                                      = "LBL_Suspended";                      // NOI18N
 
82
 
 
83
    private static final String INACTIVE_THREAD_STATE 
 
84
                                      = "LBL_InactiveThreadState";            // NOI18N
 
85
 
 
86
    private static final String ACTIVE_THREAD_STATE 
 
87
                                       = "LBL_ActiveThreadState";             // NOI18N
 
88
 
 
89
    private static final String THREAD_NAME         
 
90
                                       = "LBL_ThreadName";                    // NOI18N
 
91
    
 
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
 
98
 
 
99
    public ThreadsModel(ContextProvider contextProvider) {
 
100
        myProvider = contextProvider;
 
101
    }
 
102
    
 
103
    /* (non-Javadoc)
 
104
     * @see org.netbeans.modules.php.dbgp.models.ViewModelSupport#clearModel()
 
105
     */
 
106
    @Override
 
107
    public void clearModel() {
 
108
        update();
 
109
    }
 
110
    
 
111
    /* (non-Javadoc)
 
112
     * @see org.netbeans.spi.viewmodel.TreeModel#getRoot()
 
113
     */
 
114
    public Object getRoot() {
 
115
        return ROOT;
 
116
    }
 
117
    
 
118
    public void update( ){
 
119
        refresh();
 
120
    }
 
121
    
 
122
    public void updateSession( DebugSession session ){
 
123
        updateThreadState(session);
 
124
    }
 
125
 
 
126
    /* (non-Javadoc)
 
127
     * @see org.netbeans.spi.viewmodel.TreeModel#getChildren(java.lang.Object, int, int)
 
128
     */
 
129
    public Object[] getChildren(Object parent, int from, int to) 
 
130
        throws UnknownTypeException 
 
131
    {
 
132
        if (parent == ROOT) {
 
133
            SessionId id = getSessionId();
 
134
            if ( id == null ){
 
135
                return new Object[0];
 
136
            }
 
137
            Collection<DebugSession> collection = 
 
138
                StartActionProviderImpl.getInstance().getSessions(id);
 
139
            if ( from >= collection.size() ){
 
140
                return new Object[0];
 
141
            }
 
142
            int end = Math.min( to, collection.size());
 
143
            if ( from == 0 && to == collection.size() ){
 
144
                return collection.toArray( new Object[ collection.size() ]);
 
145
            }
 
146
            ArrayList<DebugSession> list = new ArrayList<DebugSession>( 
 
147
                    collection );
 
148
            List<DebugSession> result = list.subList( from , end );
 
149
            return result.toArray( new Object[ result.size() ] );
 
150
        }
 
151
        
 
152
        throw new UnknownTypeException(parent);
 
153
    }
 
154
 
 
155
    /* (non-Javadoc)
 
156
     * @see org.netbeans.spi.viewmodel.TreeModel#isLeaf(java.lang.Object)
 
157
     */
 
158
    public boolean isLeaf(Object node) throws UnknownTypeException {
 
159
        if (node == ROOT) {
 
160
            return false;
 
161
        }
 
162
        else if (node instanceof DebugSession) {
 
163
            return true;
 
164
        }
 
165
 
 
166
        throw new UnknownTypeException(node);
 
167
    }
 
168
 
 
169
    /* (non-Javadoc)
 
170
     * @see org.netbeans.spi.viewmodel.TreeModel#getChildrenCount(java.lang.Object)
 
171
     */
 
172
    public int getChildrenCount(Object node) throws UnknownTypeException {
 
173
        if (node == ROOT) {
 
174
            SessionId id = getSessionId();
 
175
            if ( id == null ){
 
176
                return 0;
 
177
            }
 
178
            return StartActionProviderImpl.getInstance().getSessions(id).size();
 
179
        }
 
180
        throw new UnknownTypeException(node);
 
181
    }
 
182
 
 
183
    /* (non-Javadoc)
 
184
     * @see org.netbeans.spi.viewmodel.NodeModel#getDisplayName(java.lang.Object)
 
185
     */
 
186
    public String getDisplayName(Object node) throws UnknownTypeException {
 
187
        if (node instanceof DebugSession ) {
 
188
            DebugSession session = (DebugSession) node;
 
189
            String scriptName = getScriptName( session );
 
190
            
 
191
            return NbBundle.getMessage(ThreadsModel.class, THREAD_NAME, 
 
192
                    scriptName );
 
193
        }
 
194
        else if (node == ROOT) {
 
195
            return ROOT.toString();
 
196
        }
 
197
        
 
198
        throw new UnknownTypeException(node);
 
199
    }
 
200
 
 
201
    /* (non-Javadoc)
 
202
     * @see org.netbeans.spi.viewmodel.NodeModel#getIconBase(java.lang.Object)
 
203
     */
 
204
    public String getIconBase(Object node) throws UnknownTypeException {
 
205
        if (node instanceof DebugSession) {
 
206
            DebugSession session = (DebugSession)node;
 
207
            if ( session.getBridge().isSuspended()){
 
208
                return SUSPENDED;
 
209
            }
 
210
            else {
 
211
                if ( isCurrent( session ) ) {
 
212
                    return CURRENT;
 
213
                }                
 
214
                else {
 
215
                    return RUNNING;
 
216
                }
 
217
            }
 
218
        }
 
219
        else if (node == ROOT) {
 
220
            return null;
 
221
        }
 
222
        
 
223
        throw new UnknownTypeException(node);
 
224
    }
 
225
 
 
226
    /* (non-Javadoc)
 
227
     * @see org.netbeans.spi.viewmodel.NodeModel#getShortDescription(java.lang.Object)
 
228
     */
 
229
    public String getShortDescription(Object node) throws UnknownTypeException {
 
230
        if (node == ROOT) {
 
231
            return null;
 
232
        }
 
233
        else if (node instanceof DebugSession) {
 
234
            return ((DebugSession)node).getFileName();
 
235
        }
 
236
        throw new UnknownTypeException(node);
 
237
    }
 
238
 
 
239
    /* (non-Javadoc)
 
240
     * @see org.netbeans.spi.viewmodel.NodeActionsProvider#performDefaultAction(java.lang.Object)
 
241
     */
 
242
    public void performDefaultAction(Object node) throws UnknownTypeException {
 
243
        if (node instanceof DebugSession) {
 
244
            DebugSession session = (DebugSession) node;
 
245
            SessionId id = getSessionId();
 
246
            if ( id == null ){
 
247
                return;
 
248
            }
 
249
            DebugSession current = 
 
250
                StartActionProviderImpl.getInstance().getCurrentSession(id);
 
251
            
 
252
            if (! session.equals( current)) {
 
253
                Session sess = getSession();
 
254
                StartActionProviderImpl.getInstance().setCurrentSession(
 
255
                        sess, session );
 
256
                StatusCommand command = new StatusCommand( 
 
257
                        session.getTransactionId() );
 
258
                session.sendCommandLater(command);
 
259
                updateThreadState(current);
 
260
                updateThreadState(session);
 
261
            }
 
262
        }
 
263
        
 
264
        throw new UnknownTypeException(node);
 
265
    }
 
266
 
 
267
    /* (non-Javadoc)
 
268
     * @see org.netbeans.spi.viewmodel.NodeActionsProvider#getActions(java.lang.Object)
 
269
     */
 
270
    public Action[] getActions(Object node) throws UnknownTypeException {
 
271
        return new Action [] {};
 
272
    }
 
273
 
 
274
    /* (non-Javadoc)
 
275
     * @see org.netbeans.spi.viewmodel.TableModel#getValueAt(java.lang.Object, java.lang.String)
 
276
     */
 
277
    public Object getValueAt(Object node, String columnID) 
 
278
        throws UnknownTypeException 
 
279
    {
 
280
        if (node == ROOT) {
 
281
            return null;
 
282
        }
 
283
 
 
284
        if (node instanceof DebugSession) {
 
285
            DebugSession session = (DebugSession)node;
 
286
            
 
287
            if (columnID == Constants.THREAD_SUSPENDED_COLUMN_ID) {
 
288
                return session.getBridge().isSuspended();
 
289
            }
 
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 ));
 
297
                return result;
 
298
            }
 
299
        }
 
300
 
 
301
        throw new UnknownTypeException(node);
 
302
    }
 
303
 
 
304
    /* (non-Javadoc)
 
305
     * @see org.netbeans.spi.viewmodel.TableModel#isReadOnly(java.lang.Object, java.lang.String)
 
306
     */
 
307
    public boolean isReadOnly(Object node, String columnID) 
 
308
        throws UnknownTypeException 
 
309
    {
 
310
        if (node == ROOT || node instanceof DebugSession ) {
 
311
            return true;
 
312
        }
 
313
 
 
314
        throw new UnknownTypeException(node);
 
315
    }
 
316
 
 
317
    /* (non-Javadoc)
 
318
     * @see org.netbeans.spi.viewmodel.TableModel#setValueAt(java.lang.Object, java.lang.String, java.lang.Object)
 
319
     */
 
320
    public void setValueAt(Object node, String columnID, Object value) 
 
321
        throws UnknownTypeException 
 
322
    {
 
323
        throw new UnknownTypeException(node);
 
324
    }
 
325
 
 
326
 
 
327
    private void updateThreadState(DebugSession session) {
 
328
        fireChangeEvent(new ModelEvent.NodeChanged(this, session));
 
329
    }
 
330
    
 
331
    private String getScriptName( DebugSession session ) {
 
332
        SessionId id = session.getSessionId();
 
333
        if ( id == null ){
 
334
            return "";
 
335
        }
 
336
        String fileName = session.getFileName();
 
337
        FileObject script = id.getFileObjectByRemote( fileName );
 
338
        Project project = FileOwnerQuery.getOwner( script );
 
339
        return FileUtil.getRelativePath( project.getProjectDirectory(), script );
 
340
    }
 
341
    
 
342
    private Session getSession(){
 
343
        return (Session)getContextProvider().lookupFirst( null , Session.class );
 
344
    }
 
345
    
 
346
    private SessionId getSessionId(){
 
347
        ContextProvider provider = getContextProvider();
 
348
        if ( provider == null ){
 
349
            return null;
 
350
        }
 
351
        return (SessionId)provider.lookupFirst( null , SessionId.class );
 
352
    }
 
353
    
 
354
    private ContextProvider getContextProvider() {
 
355
        return myProvider;
 
356
    }
 
357
    
 
358
    private boolean isCurrent( DebugSession session ){
 
359
        SessionId id = getSessionId();
 
360
        DebugSession current = 
 
361
            StartActionProviderImpl.getInstance().getCurrentSession(id);
 
362
        return session.equals( current );
 
363
    }
 
364
    
 
365
    private ContextProvider myProvider;
 
366
 
 
367
}