KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > models > BreakpointsTableModel


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.ui.models;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.netbeans.api.debugger.Breakpoint;
28 import org.netbeans.api.debugger.DebuggerManager;
29 import org.netbeans.spi.debugger.ui.Constants;
30 import org.netbeans.spi.viewmodel.ModelEvent;
31 import org.netbeans.spi.viewmodel.TableModel;
32 import org.netbeans.spi.viewmodel.ModelListener;
33 import org.netbeans.spi.viewmodel.UnknownTypeException;
34 import org.openide.util.RequestProcessor;
35
36
37 /**
38  *
39  * @author Jan Jancura
40  */

41 public class BreakpointsTableModel implements TableModel, Constants {
42     
43     private Map JavaDoc breakpointsBeingEnabled = new HashMap JavaDoc();
44     private RequestProcessor rp;
45     private Collection JavaDoc modelListeners = new ArrayList JavaDoc();
46
47     public Object JavaDoc getValueAt (Object JavaDoc row, String JavaDoc columnID) throws
48     UnknownTypeException {
49         if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) {
50             if (row instanceof Breakpoint) {
51                 return Boolean.valueOf (((Breakpoint) row).isEnabled ());
52             } else if (row instanceof String JavaDoc) {
53                 // group name
54
String JavaDoc groupName = (String JavaDoc) row;
55                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
56                     getBreakpoints ();
57                 Boolean JavaDoc enabled = null;
58                 for (int i = 0; i < bs.length; i++) {
59                     if (bs [i].getGroupName ().equals (groupName)) {
60                         if (enabled == null) {
61                             enabled = Boolean.valueOf (bs[i].isEnabled ());
62                         } else {
63                             if (enabled.booleanValue() != bs[i].isEnabled ()) {
64                                 return null; // Some are enabled, some disabled
65
}
66                         }
67                     }
68                 }
69                 return enabled;
70             }
71         }
72         throw new UnknownTypeException (row);
73     }
74     
75     public boolean isReadOnly (Object JavaDoc row, String JavaDoc columnID) throws
76     UnknownTypeException {
77         if (row instanceof Breakpoint) {
78             if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) {
79                 synchronized (breakpointsBeingEnabled) {
80                     if (breakpointsBeingEnabled.containsKey(row)) {
81                         return true;
82                     } else {
83                         return false;
84                     }
85                 }
86             }
87         } else if (row instanceof String JavaDoc) {
88             // group name
89
return false;
90         }
91         throw new UnknownTypeException (row);
92     }
93     
94     public void setValueAt (final Object JavaDoc row, final String JavaDoc columnID, final Object JavaDoc value)
95     throws UnknownTypeException {
96         if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) {
97             if (row instanceof Breakpoint) {
98                 synchronized (breakpointsBeingEnabled) {
99                     // Keep the original value until we change the BP state...
100
breakpointsBeingEnabled.put(row, Boolean.valueOf(((Breakpoint) row).isEnabled()));
101                     if (rp == null) {
102                         rp = new RequestProcessor("Enable Breakpoints RP", 1); // NOI18N
103
}
104                 }
105                 rp.post(new BreakpointEnabler((Breakpoint) row, ((Boolean JavaDoc) value).booleanValue ()));
106                 return;
107             } else if (row instanceof String JavaDoc) {
108                 String JavaDoc groupName = (String JavaDoc) row;
109                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
110                     getBreakpoints ();
111                 ArrayList JavaDoc breakpoints = new ArrayList JavaDoc();
112                 for (int i = 0; i < bs.length; i++) {
113                     if (bs [i].getGroupName ().equals (groupName)) {
114                         breakpoints.add(bs[i]);
115                     }
116                 }
117                 if (breakpoints.size() > 0) {
118                     synchronized (breakpointsBeingEnabled) {
119                         // Keep the original value until we change the BP state...
120
for (Iterator JavaDoc it = breakpoints.iterator(); it.hasNext(); ) {
121                             Breakpoint bp = (Breakpoint) it.next();
122                             breakpointsBeingEnabled.put(bp, Boolean.valueOf(bp.isEnabled()));
123                         }
124                         if (rp == null) {
125                             rp = new RequestProcessor("Enable Breakpoints RP", 1); // NOI18N
126
}
127                         for (Iterator JavaDoc it = breakpoints.iterator(); it.hasNext(); ) {
128                             Breakpoint bp = (Breakpoint) it.next();
129                             rp.post(new BreakpointEnabler(bp, ((Boolean JavaDoc) value).booleanValue ()));
130                         }
131                     }
132                 }
133                 return ;
134             }
135         }
136         throw new UnknownTypeException (row);
137     }
138         
139     private class BreakpointEnabler extends Object JavaDoc implements Runnable JavaDoc {
140         
141         private Breakpoint bp;
142         private boolean enable;
143         
144         public BreakpointEnabler(Breakpoint bp, boolean enable) {
145             this.bp = bp;
146             this.enable = enable;
147         }
148         
149         public void run() {
150             if (enable)
151                 bp.enable ();
152             else
153                 bp.disable ();
154             synchronized (breakpointsBeingEnabled) {
155                 breakpointsBeingEnabled.remove(bp);
156             }
157             fireModelEvent(new ModelEvent.TableValueChanged(
158                     BreakpointsTableModel.this,
159                     bp, BREAKPOINT_ENABLED_COLUMN_ID));
160             // re-calculate the enabled state of the BP group
161
String JavaDoc groupName = bp.getGroupName();
162             if (groupName != null) {
163                 fireModelEvent(new ModelEvent.TableValueChanged(
164                     BreakpointsTableModel.this,
165                     groupName, BREAKPOINT_ENABLED_COLUMN_ID));
166             }
167         }
168     }
169     
170     /**
171      * Registers given listener.
172      *
173      * @param l the listener to add
174      */

175     public void addModelListener (ModelListener l) {
176         synchronized (modelListeners) {
177             modelListeners.add(l);
178         }
179     }
180
181     /**
182      * Unregisters given listener.
183      *
184      * @param l the listener to remove
185      */

186     public void removeModelListener (ModelListener l) {
187         synchronized (modelListeners) {
188             modelListeners.remove(l);
189         }
190     }
191     
192     private void fireModelEvent(ModelEvent ev) {
193         Collection JavaDoc listeners;
194         synchronized (modelListeners) {
195             listeners = new ArrayList JavaDoc(modelListeners);
196         }
197         for (Iterator JavaDoc it = listeners.iterator(); it.hasNext(); ) {
198             ModelListener l = (ModelListener) it.next();
199             l.modelChanged(ev);
200         }
201     }
202 }
203
Popular Tags