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.lang.ref.WeakReference;
045: import java.util.Vector;
046:
047: import org.netbeans.api.debugger.Session;
048: import org.netbeans.api.debugger.DebuggerManager;
049: import org.netbeans.api.debugger.DebuggerManagerAdapter;
050: import org.netbeans.spi.viewmodel.TreeModel;
051: import org.netbeans.spi.viewmodel.ModelListener;
052: import org.netbeans.spi.viewmodel.UnknownTypeException;
053:
054: /**
055: * @author Jan Jancura
056: */
057: public class SessionsTreeModel implements TreeModel {
058:
059: private Listener listener;
060: private Vector listeners = new Vector();
061:
062: /**
063: *
064: * @return threads contained in this group of threads
065: */
066: public Object getRoot() {
067: return ROOT;
068: }
069:
070: /**
071: *
072: * @return threads contained in this group of threads
073: */
074: public Object[] getChildren(Object parent, int from, int to)
075: throws UnknownTypeException {
076: if (parent == ROOT) {
077: Session[] ss = DebuggerManager.getDebuggerManager()
078: .getSessions();
079: if (listener == null)
080: listener = new Listener(this );
081: to = Math.min(ss.length, to);
082: from = Math.min(ss.length, from);
083: Session[] fss = new Session[to - from];
084: System.arraycopy(ss, from, fss, 0, to - from);
085: return fss;
086: } else
087: throw new UnknownTypeException(parent);
088: }
089:
090: /**
091: * Returns number of children for given node.
092: *
093: * @param node the parent node
094: * @throws UnknownTypeException if this TreeModel implementation is not
095: * able to resolve children for given node type
096: *
097: * @return true if node is leaf
098: */
099: public int getChildrenCount(Object node)
100: throws UnknownTypeException {
101: if (node == ROOT) {
102: if (listener == null)
103: listener = new Listener(this );
104: // Performance, see issue #59058.
105: return Integer.MAX_VALUE;
106: //return DebuggerManager.getDebuggerManager ().
107: // getSessions ().length;
108: } else
109: throw new UnknownTypeException(node);
110: }
111:
112: public boolean isLeaf(Object node) throws UnknownTypeException {
113: if (node == ROOT)
114: return false;
115: if (node instanceof Session)
116: return true;
117: throw new UnknownTypeException(node);
118: }
119:
120: public void addModelListener(ModelListener l) {
121: listeners.add(l);
122: }
123:
124: public void removeModelListener(ModelListener l) {
125: listeners.remove(l);
126: }
127:
128: public void fireTreeChanged() {
129: Vector v = (Vector) listeners.clone();
130: int i, k = v.size();
131: for (i = 0; i < k; i++)
132: ((ModelListener) v.get(i)).modelChanged(null);
133: }
134:
135: // innerclasses ............................................................
136:
137: private static class Listener extends DebuggerManagerAdapter {
138:
139: private WeakReference model;
140:
141: public Listener(SessionsTreeModel tm) {
142: model = new WeakReference(tm);
143: DebuggerManager.getDebuggerManager().addDebuggerListener(
144: DebuggerManager.PROP_SESSIONS, this );
145: }
146:
147: private SessionsTreeModel getModel() {
148: SessionsTreeModel m = (SessionsTreeModel) model.get();
149: if (m == null) {
150: DebuggerManager.getDebuggerManager()
151: .removeDebuggerListener(
152: DebuggerManager.PROP_SESSIONS, this );
153: }
154: return m;
155: }
156:
157: public void sessionAdded(Session s) {
158: SessionsTreeModel m = getModel();
159: if (m == null)
160: return;
161: m.fireTreeChanged();
162: }
163:
164: public void sessionRemoved(Session s) {
165: SessionsTreeModel m = getModel();
166: if (m == null)
167: return;
168: m.fireTreeChanged();
169: }
170: }
171: }
|