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.viewmodel;
043:
044: import java.lang.ref.WeakReference;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.WeakHashMap;
048: import javax.swing.SwingUtilities;
049: import org.netbeans.spi.viewmodel.Models;
050:
051: import org.netbeans.spi.viewmodel.TreeModel;
052: import org.netbeans.spi.viewmodel.ModelEvent;
053: import org.netbeans.spi.viewmodel.ModelListener;
054:
055: import org.openide.nodes.Node;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.RequestProcessor;
058:
059: /**
060: * Implements root node of hierarchy created for given TreeModel.
061: *
062: * @author Jan Jancura
063: */
064: public class TreeModelRoot implements ModelListener {
065: /** generated Serialized Version UID */
066: static final long serialVersionUID = -1259352660663524178L;
067:
068: // variables ...............................................................
069:
070: private Models.CompoundModel model;
071: private TreeModelNode rootNode;
072: private WeakHashMap<Object, WeakReference<TreeModelNode>> objectToNode = new WeakHashMap<Object, WeakReference<TreeModelNode>>();
073: private TreeTable treeTable;
074:
075: /** The children evaluator for view if this root. */
076: private TreeModelNode.LazyEvaluator childrenEvaluator;
077: /** The values evaluator for view if this root. */
078: private TreeModelNode.LazyEvaluator valuesEvaluator;
079:
080: public TreeModelRoot(Models.CompoundModel model, TreeTable treeTable) {
081: this .model = model;
082: this .treeTable = treeTable;
083: model.addModelListener(this );
084: }
085:
086: public TreeTable getTreeTable() {
087: return treeTable;
088: }
089:
090: public TreeModelNode getRootNode() {
091: if (rootNode == null)
092: rootNode = new TreeModelNode(model, this , model.getRoot());
093: return rootNode;
094: }
095:
096: void registerNode(Object o, TreeModelNode n) {
097: objectToNode.put(o, new WeakReference<TreeModelNode>(n));
098: }
099:
100: TreeModelNode findNode(Object o) {
101: WeakReference<TreeModelNode> wr = objectToNode.get(o);
102: if (wr == null)
103: return null;
104: return wr.get();
105: }
106:
107: public void modelChanged(final ModelEvent event) {
108: SwingUtilities.invokeLater(new Runnable() {
109: public void run() {
110: if (model == null)
111: return; // already disposed
112: if (event instanceof ModelEvent.TableValueChanged) {
113: ModelEvent.TableValueChanged tvEvent = (ModelEvent.TableValueChanged) event;
114: Object node = tvEvent.getNode();
115: if (node != null) {
116: TreeModelNode tmNode = findNode(node);
117: if (tmNode != null) {
118: String column = tvEvent.getColumnID();
119: if (column != null) {
120: tmNode.refreshColumn(column);
121: } else {
122: tmNode.refresh();
123: }
124: return; // We're done
125: }
126: }
127: }
128: if (event instanceof ModelEvent.NodeChanged) {
129: ModelEvent.NodeChanged nchEvent = (ModelEvent.NodeChanged) event;
130: Object node = nchEvent.getNode();
131: if (node != null) {
132: TreeModelNode tmNode = findNode(node);
133: if (tmNode != null) {
134: tmNode.refresh(nchEvent.getChange());
135: return; // We're done
136: }
137: }
138: }
139: rootNode.setObject(model.getRoot());
140: }
141: });
142: // Iterator i = new HashSet (objectToNode.values ()).iterator ();
143: // while (i.hasNext ()) {
144: // WeakReference wr = (WeakReference) i.next ();
145: // if (wr == null) continue;
146: // TreeModelNode it = (TreeModelNode) wr.get ();
147: // if (it != null)
148: // it.refresh ();
149: // }
150: }
151:
152: // public void treeNodeChanged (Object parent) {
153: // final TreeModelNode tmn = findNode (parent);
154: // if (tmn == null) return;
155: // SwingUtilities.invokeLater (new Runnable () {
156: // public void run () {
157: // tmn.refresh ();
158: // }
159: // });
160: // }
161:
162: synchronized TreeModelNode.LazyEvaluator getChildrenEvaluator() {
163: if (childrenEvaluator == null) {
164: childrenEvaluator = new TreeModelNode.LazyEvaluator();
165: }
166: return childrenEvaluator;
167: }
168:
169: synchronized TreeModelNode.LazyEvaluator getValuesEvaluator() {
170: if (valuesEvaluator == null) {
171: valuesEvaluator = new TreeModelNode.LazyEvaluator();
172: }
173: return valuesEvaluator;
174: }
175:
176: public void destroy() {
177: if (model != null)
178: model.removeModelListener(this );
179: model = null;
180: objectToNode = new WeakHashMap<Object, WeakReference<TreeModelNode>>();
181: }
182: }
|