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.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.ref.WeakReference;
047: import java.util.ArrayList;
048: import java.util.Comparator;
049: import java.util.Set;
050: import java.util.TreeSet;
051: import java.util.Vector;
052:
053: import org.netbeans.api.debugger.Breakpoint;
054: import org.netbeans.api.debugger.DebuggerManager;
055: import org.netbeans.api.debugger.DebuggerManagerAdapter;
056: import org.netbeans.spi.debugger.ui.Constants;
057: import org.netbeans.spi.viewmodel.ModelEvent;
058: import org.netbeans.spi.viewmodel.TreeModel;
059: import org.netbeans.spi.viewmodel.ModelListener;
060: import org.netbeans.spi.viewmodel.UnknownTypeException;
061:
062: /**
063: * @author Jan Jancura
064: */
065: public class BreakpointsTreeModel implements TreeModel {
066:
067: private Listener listener;
068: private Vector listeners = new Vector();
069:
070: /**
071: *
072: * @return threads contained in this group of threads
073: */
074: public Object getRoot() {
075: return ROOT;
076: }
077:
078: /**
079: *
080: * @return threads contained in this group of threads
081: */
082: public Object[] getChildren(Object parent, int from, int to)
083: throws UnknownTypeException {
084: if (parent == ROOT) {
085: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
086: .getBreakpoints();
087: ArrayList l = new ArrayList();
088: int i, k = bs.length;
089: for (i = 0; i < k; i++) {
090: String gn = bs[i].getGroupName();
091: if (gn.equals("")) {
092: l.add(bs[i]);
093: } else {
094: if (!l.contains(gn)) {
095: l.add(gn);
096: }
097: }
098: }
099: if (listener == null)
100: listener = new Listener(this );
101: if (to == 0)
102: return l.toArray();
103: to = Math.min(l.size(), to);
104: from = Math.min(l.size(), from);
105: return l.subList(from, to).toArray();
106: } else if (parent instanceof String) {
107: String groupName = (String) parent;
108: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
109: .getBreakpoints();
110: ArrayList l = new ArrayList();
111: int i, k = bs.length;
112: for (i = 0; i < k; i++)
113: if (bs[i].getGroupName().equals(groupName))
114: l.add(bs[i]);
115: if (listener == null)
116: listener = new Listener(this );
117: if (to == 0)
118: return l.toArray();
119: to = Math.min(l.size(), to);
120: from = Math.min(l.size(), from);
121: return l.subList(from, to).toArray();
122: } else
123: throw new UnknownTypeException(parent);
124: }
125:
126: /**
127: * Returns number of children for given node.
128: *
129: * @param node the parent node
130: * @throws UnknownTypeException if this TreeModel implementation is not
131: * able to resolve children for given node type
132: *
133: * @return true if node is leaf
134: */
135: public int getChildrenCount(Object node)
136: throws UnknownTypeException {
137: if (node == ROOT) {
138: // Performance, see issue #59058.
139: return Integer.MAX_VALUE;
140: //return getChildren (node, 0, 0).length;
141: } else if (node instanceof String) {
142: // Performance, see issue #59058.
143: return Integer.MAX_VALUE;
144: //return getChildren (node, 0, 0).length;
145: } else
146: throw new UnknownTypeException(node);
147: }
148:
149: public boolean isLeaf(Object node) throws UnknownTypeException {
150: if (node == ROOT)
151: return false;
152: if (node instanceof Breakpoint)
153: return true;
154: if (node instanceof String)
155: return false;
156: throw new UnknownTypeException(node);
157: }
158:
159: public void addModelListener(ModelListener l) {
160: listeners.add(l);
161: }
162:
163: public void removeModelListener(ModelListener l) {
164: listeners.remove(l);
165: }
166:
167: private void fireTreeChanged() {
168: Vector v = (Vector) listeners.clone();
169: int i, k = v.size();
170: for (i = 0; i < k; i++)
171: ((ModelListener) v.get(i))
172: .modelChanged(new ModelEvent.TreeChanged(this ));
173: }
174:
175: private void fireTreeChanged(ModelEvent me) {
176: Vector v = (Vector) listeners.clone();
177: int i, k = v.size();
178: for (i = 0; i < k; i++)
179: ((ModelListener) v.get(i)).modelChanged(me);
180: }
181:
182: // innerclasses ............................................................
183:
184: private static class Listener extends DebuggerManagerAdapter
185: implements PropertyChangeListener {
186:
187: private WeakReference model;
188:
189: public Listener(BreakpointsTreeModel tm) {
190: model = new WeakReference(tm);
191: DebuggerManager.getDebuggerManager().addDebuggerListener(
192: DebuggerManager.PROP_BREAKPOINTS, this );
193: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
194: .getBreakpoints();
195: int i, k = bs.length;
196: for (i = 0; i < k; i++)
197: bs[i].addPropertyChangeListener(this );
198: }
199:
200: private BreakpointsTreeModel getModel() {
201: BreakpointsTreeModel m = (BreakpointsTreeModel) model.get();
202: if (m == null) {
203: DebuggerManager.getDebuggerManager()
204: .removeDebuggerListener(
205: DebuggerManager.PROP_BREAKPOINTS, this );
206: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
207: .getBreakpoints();
208: int i, k = bs.length;
209: for (i = 0; i < k; i++)
210: bs[i].removePropertyChangeListener(this );
211: }
212: return m;
213: }
214:
215: public void breakpointAdded(Breakpoint breakpoint) {
216: BreakpointsTreeModel m = getModel();
217: if (m == null)
218: return;
219: breakpoint.addPropertyChangeListener(this );
220: m.fireTreeChanged();
221: }
222:
223: public void breakpointRemoved(Breakpoint breakpoint) {
224: BreakpointsTreeModel m = getModel();
225: if (m == null)
226: return;
227: breakpoint.removePropertyChangeListener(this );
228: m.fireTreeChanged();
229: }
230:
231: public void propertyChange(PropertyChangeEvent evt) {
232: BreakpointsTreeModel m = getModel();
233: if (m == null)
234: return;
235: if (!(evt.getSource() instanceof Breakpoint))
236: return;
237: if (evt.getPropertyName() == Breakpoint.PROP_GROUP_NAME) {
238: m.fireTreeChanged();
239: } else {
240: m.fireTreeChanged(new ModelEvent.NodeChanged(m, evt
241: .getSource()));
242: if (evt.getPropertyName() == Breakpoint.PROP_ENABLED) {
243: Breakpoint bp = (Breakpoint) evt.getSource();
244: String groupName = bp.getGroupName();
245: if (groupName != null) {
246: m
247: .fireTreeChanged(new ModelEvent.TableValueChanged(
248: m,
249: groupName,
250: Constants.BREAKPOINT_ENABLED_COLUMN_ID));
251: }
252: }
253: }
254: }
255: }
256:
257: }
|