001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.ui.models;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.Map;
049: import org.netbeans.api.debugger.Breakpoint;
050: import org.netbeans.api.debugger.DebuggerManager;
051: import org.netbeans.spi.debugger.ui.Constants;
052: import org.netbeans.spi.viewmodel.ModelEvent;
053: import org.netbeans.spi.viewmodel.TableModel;
054: import org.netbeans.spi.viewmodel.ModelListener;
055: import org.netbeans.spi.viewmodel.UnknownTypeException;
056: import org.openide.util.RequestProcessor;
057:
058: /**
059: *
060: * @author Jan Jancura
061: */
062: public class BreakpointsTableModel implements TableModel, Constants {
063:
064: private Map breakpointsBeingEnabled = new HashMap();
065: private RequestProcessor rp;
066: private Collection modelListeners = new ArrayList();
067:
068: public Object getValueAt(Object row, String columnID)
069: throws UnknownTypeException {
070: if (columnID.equals(BREAKPOINT_ENABLED_COLUMN_ID)) {
071: if (row instanceof Breakpoint) {
072: return Boolean.valueOf(((Breakpoint) row).isEnabled());
073: } else if (row instanceof String) {
074: // group name
075: String groupName = (String) row;
076: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
077: .getBreakpoints();
078: Boolean enabled = null;
079: for (int i = 0; i < bs.length; i++) {
080: if (bs[i].getGroupName().equals(groupName)) {
081: if (enabled == null) {
082: enabled = Boolean
083: .valueOf(bs[i].isEnabled());
084: } else {
085: if (enabled.booleanValue() != bs[i]
086: .isEnabled()) {
087: return null; // Some are enabled, some disabled
088: }
089: }
090: }
091: }
092: return enabled;
093: }
094: }
095: throw new UnknownTypeException(row);
096: }
097:
098: public boolean isReadOnly(Object row, String columnID)
099: throws UnknownTypeException {
100: if (row instanceof Breakpoint) {
101: if (columnID.equals(BREAKPOINT_ENABLED_COLUMN_ID)) {
102: synchronized (breakpointsBeingEnabled) {
103: if (breakpointsBeingEnabled.containsKey(row)) {
104: return true;
105: } else {
106: return false;
107: }
108: }
109: }
110: } else if (row instanceof String) {
111: // group name
112: return false;
113: }
114: throw new UnknownTypeException(row);
115: }
116:
117: public void setValueAt(final Object row, final String columnID,
118: final Object value) throws UnknownTypeException {
119: if (columnID.equals(BREAKPOINT_ENABLED_COLUMN_ID)) {
120: if (row instanceof Breakpoint) {
121: synchronized (breakpointsBeingEnabled) {
122: // Keep the original value until we change the BP state...
123: breakpointsBeingEnabled.put(row, Boolean
124: .valueOf(((Breakpoint) row).isEnabled()));
125: if (rp == null) {
126: rp = new RequestProcessor(
127: "Enable Breakpoints RP", 1); // NOI18N
128: }
129: }
130: rp.post(new BreakpointEnabler((Breakpoint) row,
131: ((Boolean) value).booleanValue()));
132: return;
133: } else if (row instanceof String) {
134: String groupName = (String) row;
135: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
136: .getBreakpoints();
137: ArrayList breakpoints = new ArrayList();
138: for (int i = 0; i < bs.length; i++) {
139: if (bs[i].getGroupName().equals(groupName)) {
140: breakpoints.add(bs[i]);
141: }
142: }
143: if (breakpoints.size() > 0) {
144: synchronized (breakpointsBeingEnabled) {
145: // Keep the original value until we change the BP state...
146: for (Iterator it = breakpoints.iterator(); it
147: .hasNext();) {
148: Breakpoint bp = (Breakpoint) it.next();
149: breakpointsBeingEnabled.put(bp, Boolean
150: .valueOf(bp.isEnabled()));
151: }
152: if (rp == null) {
153: rp = new RequestProcessor(
154: "Enable Breakpoints RP", 1); // NOI18N
155: }
156: for (Iterator it = breakpoints.iterator(); it
157: .hasNext();) {
158: Breakpoint bp = (Breakpoint) it.next();
159: rp.post(new BreakpointEnabler(bp,
160: ((Boolean) value).booleanValue()));
161: }
162: }
163: }
164: return;
165: }
166: }
167: throw new UnknownTypeException(row);
168: }
169:
170: private class BreakpointEnabler extends Object implements Runnable {
171:
172: private Breakpoint bp;
173: private boolean enable;
174:
175: public BreakpointEnabler(Breakpoint bp, boolean enable) {
176: this .bp = bp;
177: this .enable = enable;
178: }
179:
180: public void run() {
181: if (enable)
182: bp.enable();
183: else
184: bp.disable();
185: synchronized (breakpointsBeingEnabled) {
186: breakpointsBeingEnabled.remove(bp);
187: }
188: fireModelEvent(new ModelEvent.TableValueChanged(
189: BreakpointsTableModel.this , bp,
190: BREAKPOINT_ENABLED_COLUMN_ID));
191: // re-calculate the enabled state of the BP group
192: String groupName = bp.getGroupName();
193: if (groupName != null) {
194: fireModelEvent(new ModelEvent.TableValueChanged(
195: BreakpointsTableModel.this , groupName,
196: BREAKPOINT_ENABLED_COLUMN_ID));
197: }
198: }
199: }
200:
201: /**
202: * Registers given listener.
203: *
204: * @param l the listener to add
205: */
206: public void addModelListener(ModelListener l) {
207: synchronized (modelListeners) {
208: modelListeners.add(l);
209: }
210: }
211:
212: /**
213: * Unregisters given listener.
214: *
215: * @param l the listener to remove
216: */
217: public void removeModelListener(ModelListener l) {
218: synchronized (modelListeners) {
219: modelListeners.remove(l);
220: }
221: }
222:
223: private void fireModelEvent(ModelEvent ev) {
224: Collection listeners;
225: synchronized (modelListeners) {
226: listeners = new ArrayList(modelListeners);
227: }
228: for (Iterator it = listeners.iterator(); it.hasNext();) {
229: ModelListener l = (ModelListener) it.next();
230: l.modelChanged(ev);
231: }
232: }
233: }
|