KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.netbeans.api.debugger.DebuggerEngine;
27 import org.netbeans.api.debugger.Session;
28 import org.netbeans.api.debugger.jpda.JPDADebugger;
29 import org.netbeans.spi.debugger.ui.Constants;
30 import org.netbeans.spi.viewmodel.TableModel;
31 import org.netbeans.spi.viewmodel.TableModelFilter;
32 import org.netbeans.spi.viewmodel.ModelListener;
33 import org.netbeans.spi.viewmodel.UnknownTypeException;
34 import org.openide.util.NbBundle;
35
36
37 /**
38  *
39  * @author Jan Jancura
40  */

41 public class SessionsTableModelFilter implements TableModelFilter, Constants,
42 PropertyChangeListener JavaDoc {
43     
44     private static String JavaDoc loc(String JavaDoc key) {
45         return NbBundle.getBundle (SessionsTableModelFilter.class).getString (key);
46     }
47
48     private Vector JavaDoc listeners = new Vector JavaDoc ();
49     private boolean addedAsListener;
50
51     
52     public SessionsTableModelFilter () {
53     }
54
55     public Object JavaDoc getValueAt (TableModel original, Object JavaDoc row, String JavaDoc columnID) throws
56     UnknownTypeException {
57         if (row instanceof Session && isJPDASession((Session) row)) {
58             if (SESSION_STATE_COLUMN_ID.equals (columnID))
59                 return getSessionState ((Session) row);
60             else
61             if (SESSION_LANGUAGE_COLUMN_ID.equals (columnID))
62                 return row;
63             else
64             if (SESSION_HOST_NAME_COLUMN_ID.equals (columnID))
65                 return ((Session) row).getLocationName ();
66             else
67                 throw new UnknownTypeException (row);
68         }
69         return original.getValueAt(row, columnID);
70     }
71     
72     public boolean isReadOnly (TableModel original, Object JavaDoc row, String JavaDoc columnID) throws
73     UnknownTypeException {
74         if (row instanceof Session && isJPDASession((Session) row)) {
75             if (SESSION_STATE_COLUMN_ID.equals (columnID))
76                 return true;
77             else
78             if (SESSION_LANGUAGE_COLUMN_ID.equals (columnID))
79                 return false;
80             else
81             if (SESSION_HOST_NAME_COLUMN_ID.equals (columnID))
82                 return true;
83             else
84                 throw new UnknownTypeException (row);
85         }
86         return original.isReadOnly(row, columnID);
87     }
88     
89     public void setValueAt (TableModel original, Object JavaDoc row, String JavaDoc columnID, Object JavaDoc value)
90     throws UnknownTypeException {
91         original.setValueAt(row, columnID, value);
92     }
93
94     
95     // other methods ...........................................................
96

97     static boolean isJPDASession(Session s) {
98         DebuggerEngine e = s.getCurrentEngine ();
99         if (e == null) {
100             return false;
101         }
102         JPDADebugger d = (JPDADebugger) e.lookupFirst(null, JPDADebugger.class);
103         return d != null;
104     }
105     
106     private String JavaDoc getSessionState (Session s) {
107         DebuggerEngine e = s.getCurrentEngine ();
108         if (e == null)
109             return loc ("MSG_Session_State_Starting");
110         JPDADebugger d = (JPDADebugger) e.lookupFirst (null, JPDADebugger.class);
111         synchronized (this) {
112             if (!addedAsListener) {
113                 d.addPropertyChangeListener (JPDADebugger.PROP_STATE, this);
114             }
115         }
116         switch (d.getState ()) {
117             case JPDADebugger.STATE_DISCONNECTED:
118                 return loc ("MSG_Session_State_Disconnected");
119             case JPDADebugger.STATE_RUNNING:
120                 return loc ("MSG_Session_State_Running");
121             case JPDADebugger.STATE_STARTING:
122                 return loc ("MSG_Session_State_Starting");
123             case JPDADebugger.STATE_STOPPED:
124                 return loc ("MSG_Session_State_Stopped");
125         }
126         return null;
127     }
128     
129     /**
130      * Registers given listener.
131      *
132      * @param l the listener to add
133      */

134     public void addModelListener (ModelListener l) {
135         listeners.add (l);
136     }
137
138     /**
139      * Unregisters given listener.
140      *
141      * @param l the listener to remove
142      */

143     public void removeModelListener (ModelListener l) {
144         listeners.remove (l);
145     }
146     
147     private void fireTreeChanged () {
148         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
149         int i, k = v.size ();
150         for (i = 0; i < k; i++)
151             ((ModelListener) v.get (i)).modelChanged (null);
152     }
153     
154     private static final Integer JavaDoc SD = new Integer JavaDoc
155         (JPDADebugger.STATE_DISCONNECTED);
156     
157     public void propertyChange (PropertyChangeEvent JavaDoc e) {
158         fireTreeChanged ();
159         if (e.getNewValue ().equals (SD))
160             ((JPDADebugger) e.getSource ()).removePropertyChangeListener (
161                 JPDADebugger.PROP_STATE, this
162             );
163     }
164 }
165
Popular Tags