1/* 2 * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25package sun.jvm.hotspot.ui.tree; 26 27import java.util.*; 28import javax.swing.*; 29import javax.swing.event.*; 30import javax.swing.tree.*; 31 32/** A very simple tree model which allows the root to be set, so we 33 can reuse the same model for various types of data; the 34 specialization is contained within the nodes. This tree model 35 operates on SimpleTreeNodes. */ 36 37public class SimpleTreeModel implements TreeModel { 38 private static final SimpleTreeNode singletonNullRoot = new SimpleTreeNode() { 39 public int getChildCount() { return 0; } 40 public SimpleTreeNode getChild(int index) { return null; } 41 public boolean isLeaf() { return true; } 42 public int getIndexOfChild(SimpleTreeNode child) { return 0; } 43 public String toString() { return ""; } 44 public String getName() { return toString(); } 45 public String getValue() { return toString(); } 46 }; 47 private SimpleTreeNode root = singletonNullRoot; 48 /** List<TreeModelListener> */ 49 private List listeners = new ArrayList(); 50 51 public void setRoot(SimpleTreeNode node) { 52 if (node != null) { 53 root = node; 54 } else { 55 root = singletonNullRoot; 56 } 57 fireTreeStructureChanged(); 58 } 59 60 public Object getRoot() { 61 return root; 62 } 63 64 public Object getChild(Object parent, int index) { 65 return ((SimpleTreeNode) parent).getChild(index); 66 } 67 68 public int getChildCount(Object parent) { 69 return ((SimpleTreeNode) parent).getChildCount(); 70 } 71 72 public boolean isLeaf(Object node) { 73 if (node == null) { 74 return true; 75 } 76 return ((SimpleTreeNode) node).isLeaf(); 77 } 78 79 /** Unsupported operation */ 80 public void valueForPathChanged(TreePath path, Object newValue) { 81 throw new UnsupportedOperationException(); 82 } 83 84 public int getIndexOfChild(Object parent, Object child) { 85 return ((SimpleTreeNode) parent).getIndexOfChild((SimpleTreeNode) child); 86 } 87 88 public void addTreeModelListener(TreeModelListener l) { 89 listeners.add(l); 90 } 91 92 public void removeTreeModelListener(TreeModelListener l) { 93 listeners.remove(l); 94 } 95 96 public void fireTreeStructureChanged() { 97 TreeModelEvent e = new TreeModelEvent(getRoot(), new Object[] { getRoot() }, null, null); 98 for (Iterator iter = listeners.iterator(); iter.hasNext(); ) { 99 TreeModelListener l = (TreeModelListener) iter.next(); 100 l.treeStructureChanged(e); 101 } 102 } 103} 104