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 threaddemo.views.looktree;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.swing.event.TreeModelEvent;
050: import javax.swing.event.TreeModelListener;
051: import javax.swing.tree.TreeModel;
052: import javax.swing.tree.TreePath;
053: import org.netbeans.spi.looks.LookSelector;
054:
055: /**
056: * Tree model displaying a tree of represented objects using looks.
057: * @author Jesse Glick
058: */
059: final class LookTreeModel implements TreeModel {
060:
061: private static final Logger logger = Logger
062: .getLogger(LookTreeModel.class.getName());
063:
064: private final Object rootObject;
065: private final LookSelector sel;
066: private LookTreeNode root;
067: private final List<TreeModelListener> listeners;
068:
069: public LookTreeModel(Object root, LookSelector sel) {
070: listeners = new ArrayList<TreeModelListener>();
071: this .rootObject = root;
072: this .sel = sel;
073: }
074:
075: public void addNotify() {
076: root = LookTreeNode.createRoot(rootObject, sel, this );
077: fireChildrenChange(root);
078: }
079:
080: public void removeNotify() {
081: root.forgetEverything();
082: root = null;
083: }
084:
085: public Object getRoot() {
086: return root;
087: }
088:
089: public Object getChild(Object parent, int index) {
090: LookTreeNode n = (LookTreeNode) parent;
091: return n.getChild(index);
092: }
093:
094: public int getChildCount(Object parent) {
095: LookTreeNode n = (LookTreeNode) parent;
096: //logger.log(Level.FINER, "childCount of {0} is {1}", new Object[] {parent, n.getChildren().size()});
097: return n.getChildCount();
098: }
099:
100: public int getIndexOfChild(Object parent, Object child) {
101: LookTreeNode n = (LookTreeNode) parent;
102: return n.getIndexOfChild((LookTreeNode) child);
103: }
104:
105: public boolean isLeaf(Object node) {
106: LookTreeNode n = (LookTreeNode) node;
107: return n.isLeaf();
108: }
109:
110: public void addTreeModelListener(TreeModelListener l) {
111: listeners.add(l);
112: }
113:
114: public void removeTreeModelListener(TreeModelListener l) {
115: listeners.remove(l);
116: }
117:
118: @SuppressWarnings("unchecked")
119: public void valueForPathChanged(TreePath path, Object newValue) {
120: LookTreeNode n = (LookTreeNode) path.getLastPathComponent();
121: try {
122: n.getLook().rename(n.getData(), (String) newValue,
123: n.getLookup());
124: // XXX cell renderer does not adjust size to match new value...
125: } catch (IOException e) {
126: // More or less normal.
127: logger.info(e.toString());
128: }
129: }
130:
131: void fireDisplayChange(LookTreeNode source) {
132: if (listeners.isEmpty()) {
133: return;
134: }
135: LookTreeNode parent = source.getParent();
136: TreePath path = findPath(parent != null ? parent : source);
137: int[] childIndices = parent != null ? new int[] { getIndexOfChild(
138: parent, source) }
139: : null;
140: Object[] children = parent != null ? new Object[] { source }
141: : null;
142: TreeModelEvent ev = new TreeModelEvent(this , path,
143: childIndices, children);
144: for (TreeModelListener l : listeners) {
145: l.treeNodesChanged(ev);
146: }
147: }
148:
149: void fireChildrenChange(LookTreeNode source) {
150: logger.log(Level.FINER, "fireChildrenChange: {0}", source);
151: if (listeners.isEmpty()) {
152: return;
153: }
154: // XXX this is crude, could try to actually compute added/removed children...
155: TreePath path = (source == root) ? null : findPath(source
156: .getParent());
157: TreeModelEvent ev = new TreeModelEvent(this , path, null, null);
158: for (TreeModelListener l : listeners) {
159: logger.log(Level.FINER, "firing: {0} to {1}", new Object[] {
160: ev, l });
161: l.treeStructureChanged(ev);
162: }
163: }
164:
165: private TreePath findPath(LookTreeNode node) {
166: /*
167: ArrayList l = new ArrayList(20);
168: for (LookTreeNode n = node; n != null; n = n.getParent()) {
169: l.add(n);
170: }
171: Collections.reverse(l);
172: return new TreePath(l.toArray());
173: */
174: return new LookTreePath(node);
175: }
176:
177: }
|