View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.tree;
3   
4   import java.awt.*;
5   import java.util.*;
6   import java.util.List;
7   import javax.swing.*;
8   import javax.swing.event.*;
9   import javax.swing.tree.*;
10  
11  import hep.graphics.heprep.HepRep;
12  import hep.graphics.heprep.HepRepAttDef;
13  import hep.graphics.heprep.HepRepAttValue;
14  import hep.graphics.heprep.HepRepInstance;
15  import hep.graphics.heprep.HepRepType;
16  import hep.graphics.heprep.HepRepTypeTree;
17  
18  import hep.wired.util.SortableListTableModel;
19  import hep.wired.util.TristateTreeCellRenderer;
20  
21  import hep.wired.util.ScientificTable;
22  
23  /***
24   * TreeModel for HepRep TypeTree.
25   *
26   * @author Mark Donszelmann
27   * @version $Id: TypeTreeModel.java 1935 2005-06-21 18:05:29Z duns $
28   */
29  public class TypeTreeModel implements TreeModel {
30  
31      private List treeModelListeners = new ArrayList();
32      private Object root;    // either HepRep, HepRepTypeTree or HepRepType.
33      private int maxDepth;
34      
35      public TypeTreeModel(HepRep heprep) {
36          root = heprep;
37          init();
38      }
39  
40      public TypeTreeModel(HepRepTypeTree typeTree) {
41          root = typeTree;
42          init();
43      }
44  
45      public TypeTreeModel(HepRepType type) {
46          root = type;
47          init();
48      }
49  
50      private void init() {
51          maxDepth = Integer.MAX_VALUE;
52      }        
53  
54      public int getMaximumDepth() {
55          Object node = getRoot();
56          if (node == null) return 0;
57          // adjust for Root and TypeTree
58          return Math.max(0, getMaximumDepth(node)-2);
59      }
60      
61      private int getMaximumDepth(Object node) {
62          int n = getChildCount(node, Integer.MAX_VALUE);
63          int level = 0;
64          for (int i=0; i<n; i++) {
65              Object child = getChild(node, i);
66              level = Math.max(level, getMaximumDepth(child));
67          }
68          return level+1;        
69      }
70      
71      public void setRoot(HepRep heprep) {
72          root = heprep;
73          fireTreeStructureChanged();
74      }
75  
76      public Object getRoot() {
77          return root;
78      }
79  
80      public Object getChild(Object parent, int index) {
81          if (parent instanceof HepRepType) {        
82              HepRepType type = (HepRepType)parent;
83              return type.getTypeList().get(index);
84          } else if (parent instanceof HepRepTypeTree) {
85              HepRepTypeTree tree = (HepRepTypeTree)parent;
86              return tree.getTypeList().get(index);
87          } else if (parent instanceof HepRep) {
88              HepRep heprep = (HepRep)parent;
89              return heprep.getTypeTreeList().get(index);
90          }       
91          return null;
92      }
93                             
94      public int getChildCount(Object parent) {
95          return getChildCount(parent, maxDepth);
96      }
97      
98      private int getChildCount(Object parent, int depth) {
99          if (parent instanceof HepRepType) {        
100             HepRepType type = (HepRepType)parent;
101             return type.getTypes().size();
102         } else if (parent instanceof HepRepTypeTree) {
103             HepRepTypeTree tree = (HepRepTypeTree)parent;
104             return tree.getTypes().size();
105         } else if (parent instanceof HepRep) {
106             HepRep heprep = (HepRep)parent;
107             return heprep.getTypeTreeList().size();
108         }
109         return 0; 
110     }
111 
112     public boolean isLeaf(Object node) {
113         return getChildCount(node) == 0;
114     }    
115 
116     public void valueForPathChanged(TreePath path, Object newValue) {
117         // FIXME, WIRED-393 maybe we could make this better optimized
118         fireTreeNodesChanged();
119     }
120 
121     public int getIndexOfChild(Object parent, Object child) {
122         if ((parent == null) || (child == null)) return -1;
123         if (parent instanceof HepRepType) {        
124             HepRepType type = (HepRepType)parent;
125             return type.getTypeList().indexOf(child);
126         } else if (parent instanceof HepRepTypeTree) {
127             HepRepTypeTree tree = (HepRepTypeTree)parent;
128             return tree.getTypeList().indexOf(child);
129         } else if (parent instanceof HepRep) {
130             HepRep heprep = (HepRep)parent;
131             return heprep.getTypeTreeList().indexOf(child);
132         }
133         return -1;
134     }
135     
136     public void addTreeModelListener(TreeModelListener l) {
137         treeModelListeners.add(l);
138     }
139 
140     public void removeTreeModelListener(TreeModelListener l) {
141         treeModelListeners.remove(l);
142     }   
143     
144     // EXTRA
145     public void setMaximumDepth(int depth) {
146         if (depth < 0) depth = Integer.MAX_VALUE;
147         if (maxDepth != depth) {
148             maxDepth = depth;
149             fireTreeNodesChanged();
150         }
151     }
152     
153     public void fireTreeStructureChanged() {
154         if (root == null) return;
155         
156         TreeModelEvent e = new TreeModelEvent(this, new Object[] { root } );
157         for (Iterator i=treeModelListeners.iterator(); i.hasNext(); ) {
158             ((TreeModelListener)i.next()).treeStructureChanged(e);
159         }
160     }
161 
162     public void fireTreeNodesChanged() {
163         if (root == null) return;
164         
165         TreeModelEvent e = new TreeModelEvent(this, new Object[] { root } );
166         for (Iterator i=treeModelListeners.iterator(); i.hasNext(); ) {
167             ((TreeModelListener)i.next()).treeNodesChanged(e);
168         }
169     }
170 
171     // Delegated from tree
172     public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
173         if (value instanceof HepRepType) {        
174             HepRepType type = (HepRepType)value;
175             return type.getName();
176         } else if (value instanceof HepRepTypeTree) {
177             HepRepTypeTree tree = (HepRepTypeTree)value;
178             return tree.getName();
179         } else if (value instanceof HepRep) {
180             HepRep heprep = (HepRep)value;
181             return "HepRep";
182         }
183         return value != null ? value.toString() : "null";
184     } 
185 }