KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Vector JavaDoc;
23 import org.netbeans.api.debugger.jpda.JPDAThread;
24 import org.netbeans.api.debugger.jpda.JPDAThreadGroup;
25 import org.netbeans.spi.debugger.ui.Constants;
26 import org.netbeans.spi.viewmodel.ModelEvent;
27 import org.netbeans.spi.viewmodel.TableModel;
28 import org.netbeans.spi.viewmodel.ModelListener;
29 import org.netbeans.spi.viewmodel.UnknownTypeException;
30 import org.openide.ErrorManager;
31 import org.openide.util.NbBundle;
32
33
34
35 /**
36  *
37  * @author Jan Jancura
38  */

39 public class ThreadsTableModel implements TableModel, Constants {
40     
41     private Vector JavaDoc listeners = new Vector JavaDoc ();
42     
43
44     public Object JavaDoc getValueAt (Object JavaDoc row, String JavaDoc columnID) throws
45     UnknownTypeException {
46         //if (row instanceof javax.swing.JToolTip) {
47
// will throw UnknownTypeException - the value is used for tooltips
48
//}
49
if (row instanceof MonitorModel.ThreadWithBordel)
50             row = ((MonitorModel.ThreadWithBordel) row).originalThread;
51         if (row instanceof JPDAThreadGroup) {
52             if (THREAD_STATE_COLUMN_ID.equals (columnID))
53                 return "";
54             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID)) {
55                 JPDAThreadGroup group = (JPDAThreadGroup) row;
56                 JPDAThread[] threads = group.getThreads ();
57                 if (threads.length < 1) return Boolean.FALSE;
58                 return Boolean.valueOf (threads [0].isSuspended ());
59             }
60         }
61         if (row instanceof JPDAThread) {
62             if (THREAD_STATE_COLUMN_ID.equals (columnID)) {
63                 String JavaDoc description = getThreadStateDescription(((JPDAThread) row).getState ());
64                 if (description != null) {
65                     return description;
66                 }
67             } else
68             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID))
69                 return Boolean.valueOf (((JPDAThread) row).isSuspended ());
70         }
71         throw new UnknownTypeException (row);
72     }
73     
74     private static String JavaDoc getThreadStateDescription(int state) {
75         switch (state) {
76             case JPDAThread.STATE_MONITOR:
77                 return NbBundle.getMessage (
78                     ThreadsTableModel.class,
79                     "CTL_Thread_State_OnMonitor"
80                 );
81             case JPDAThread.STATE_NOT_STARTED:
82                 return NbBundle.getMessage (
83                     ThreadsTableModel.class,
84                     "CTL_Thread_State_NotStarted"
85                 );
86             case JPDAThread.STATE_RUNNING:
87                 return NbBundle.getMessage (
88                     ThreadsTableModel.class,
89                     "CTL_Thread_State_Running"
90                 );
91             case JPDAThread.STATE_SLEEPING:
92                 return NbBundle.getMessage (
93                     ThreadsTableModel.class,
94                     "CTL_Thread_State_Sleeping"
95                 );
96             case JPDAThread.STATE_UNKNOWN:
97                 return NbBundle.getMessage (
98                     ThreadsTableModel.class,
99                     "CTL_Thread_State_Unknown"
100                 );
101             case JPDAThread.STATE_WAIT:
102                 return NbBundle.getMessage (
103                     ThreadsTableModel.class,
104                     "CTL_Thread_State_Waiting"
105                 );
106             case JPDAThread.STATE_ZOMBIE:
107                 return NbBundle.getMessage (
108                     ThreadsTableModel.class,
109                     "CTL_Thread_State_Zombie"
110                 );
111             default: ErrorManager.getDefault().log(ErrorManager.WARNING, "Unknown thread state: "+state);
112                     return null;
113         }
114     }
115     
116     public boolean isReadOnly (Object JavaDoc row, String JavaDoc columnID) throws
117     UnknownTypeException {
118         if (row instanceof MonitorModel.ThreadWithBordel)
119             row = ((MonitorModel.ThreadWithBordel) row).originalThread;
120         if (row instanceof JPDAThreadGroup) {
121             if (THREAD_STATE_COLUMN_ID.equals (columnID))
122                 return true;
123             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID))
124                 return false;
125         }
126         if (row instanceof JPDAThread) {
127             if (THREAD_STATE_COLUMN_ID.equals (columnID))
128                 return true;
129             else
130             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID))
131                 return false;
132         }
133         throw new UnknownTypeException (row);
134     }
135     
136     public void setValueAt (Object JavaDoc row, String JavaDoc columnID, Object JavaDoc value)
137     throws UnknownTypeException {
138         if (row instanceof MonitorModel.ThreadWithBordel)
139             row = ((MonitorModel.ThreadWithBordel) row).originalThread;
140         if (row instanceof JPDAThreadGroup) {
141             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID)) {
142                 if (((Boolean JavaDoc) value).booleanValue ())
143                     ((JPDAThreadGroup) row).suspend ();
144                 else
145                     ((JPDAThreadGroup) row).resume ();
146                 fireTableValueChanged (row, Constants.THREAD_SUSPENDED_COLUMN_ID);
147                 return;
148             }
149         }
150         if (row instanceof JPDAThread) {
151             if (THREAD_SUSPENDED_COLUMN_ID.equals (columnID)) {
152                 if (value.equals (Boolean.TRUE))
153                     ((JPDAThread) row).suspend ();
154                 else
155                     ((JPDAThread) row).resume ();
156                 fireTableValueChanged (row, Constants.THREAD_SUSPENDED_COLUMN_ID);
157                 return;
158             }
159         }
160         throw new UnknownTypeException (row);
161     }
162     
163     /**
164      * Registers given listener.
165      *
166      * @param l the listener to add
167      */

168     public void addModelListener (ModelListener l) {
169         listeners.add (l);
170     }
171
172     /**
173      * Unregisters given listener.
174      *
175      * @param l the listener to remove
176      */

177     public void removeModelListener (ModelListener l) {
178         listeners.remove (l);
179     }
180     
181     private void fireTableValueChanged (Object JavaDoc o, String JavaDoc propertyName) {
182         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
183         int i, k = v.size ();
184         for (i = 0; i < k; i++)
185             ((ModelListener) v.get (i)).modelChanged (
186                 new ModelEvent.TableValueChanged (this, o, propertyName)
187             );
188     }
189 }
190
Popular Tags