View Javadoc

1   // Copyright 2005, FreeHEP.
2   package hep.wired.heprep.util;
3   
4   import java.util.HashSet;
5   import java.util.Enumeration;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.ArrayList;
9   import java.util.Set;
10  import java.util.Collection;
11  import java.util.Map;
12  import java.util.HashMap;
13  import javax.swing.JTree;
14  import javax.swing.tree.TreePath;
15  
16  import hep.graphics.heprep.HepRep;
17  import hep.graphics.heprep.HepRepAttDef;
18  import hep.graphics.heprep.HepRepDefaults;
19  import hep.graphics.heprep.HepRepInstance;
20  import hep.graphics.heprep.HepRepPoint;
21  import hep.graphics.heprep.HepRepType;
22  import hep.graphics.heprep.HepRepTypeTree;
23  
24  import hep.wired.util.TristateTreeNode;
25  import hep.wired.util.XYZindices;
26  
27  import hep.wired.heprep.tree.TypeTristateTreeModel;
28  import hep.wired.heprep.tree.TypeTreeModel;
29  
30  /***
31   * FIXME WIRED-395 most or all of these methods should maybe move to jheprep.
32   *
33   * @author Mark Donszelmann
34   * @version $Id: WiredHepRepUtil.java 2087 2005-07-20 23:49:09Z duns $
35   */ 
36  public class WiredHepRepUtil implements XYZindices {
37  
38      private WiredHepRepUtil() {
39      }
40      
41      public static Set/*<String>*/ getCategories(HepRep heprep) {
42          Set categories = new HashSet();
43          // defaults categories
44          categories.addAll(getCategories(HepRepDefaults.getAttDefs()));
45              
46          // other categories
47          for (Iterator i = heprep.getTypeTrees().iterator(); i.hasNext(); ) {
48              HepRepTypeTree typeTree = (HepRepTypeTree)i.next();
49              categories.addAll(getCategories(typeTree.getTypeList()));
50          }
51          return categories;
52      }
53      
54      public static Set/*<String>*/ getCategories(List/*<HepRepType>*/ types) {
55          Set categories = new HashSet();
56          for (Iterator i = types.iterator(); i.hasNext(); ) {
57              HepRepType type = (HepRepType)i.next();
58              categories.addAll(getCategories(type.getAttDefsFromNode()));
59              categories.addAll(getCategories(type.getTypeList()));
60          }
61          return categories;
62      }
63      
64      public static Set/*<String>*/ getCategories(Set/*<HepRepAttDef>*/ attDefs) {
65          Set categories = new HashSet();
66          for (Iterator j = attDefs.iterator(); j.hasNext(); ) {
67              HepRepAttDef attDef = (HepRepAttDef)j.next();
68              categories.add(attDef.getCategory());
69          }
70          return categories;
71      }
72  
73      public static String getHepRepName(Object object) {
74          if (object instanceof HepRepType) {
75              return ((HepRepType)object).getName();
76          } else if (object instanceof HepRepTypeTree) {
77              return ((HepRepTypeTree)object).getName();
78          } else if (object instanceof HepRep) {
79              return "HepRep";
80          } else {
81              return object.getClass().getName();
82          }
83      }    
84      
85      public static TypeTristateTreeModel copyTypeTree(TypeTristateTreeModel oldModel, HepRep heprep) {
86          // Save whatever is set in the old model
87          Map oldTypes = new HashMap();
88          Set oldSet = new HashSet();
89          if (oldModel != null) {
90              oldTypes = oldModel.getTypes();
91              oldSet = oldTypes.keySet();            
92          }
93          int oldSetSize = oldSet.size();
94          
95  //        System.err.println("OldSet = "+oldSetSize);
96  /*
97          for (Iterator i=oldSet.iterator(); i.hasNext(); ) {
98              System.err.println(i.next());
99          }
100 */      
101           
102         // Create the new model
103         TypeTristateTreeModel newModel = new TypeTristateTreeModel(new TypeTreeModel(heprep));
104         
105         // Lookup the types in the new model
106         Map newTypes = newModel.getTypes();
107         Set newSet = newTypes.keySet();
108 /* debug
109         System.err.println("NewSet = "+newSet.size());     
110         for (Iterator i=newSet.iterator(); i.hasNext(); ) {
111             System.err.println(i.next());
112         }
113 */        
114         // Check if the types of the old model are relevant for the new model
115         oldSet.retainAll(newSet);
116         double resemblance = (oldSetSize == 0) ? 0.0 : (double)oldSet.size() / oldSetSize;
117 // debug
118 //        System.err.println("Resemblance = "+resemblance*100+"%");
119 
120         if (resemblance >= 0.6) {
121             for(Iterator i=oldSet.iterator(); i.hasNext(); ) {
122                 TreePath path = (TreePath)i.next();
123                 TristateTreeNode oldType = (TristateTreeNode)oldTypes.get(path);
124                 TristateTreeNode newType = (TristateTreeNode)newTypes.get(path);
125                 newType.setSelected(oldType.isSelected());
126                 newType.setIncluded(oldType.isIncluded());
127 //debug
128 //                System.err.println();
129 //                System.err.println(oldType);
130 //                System.err.println(newType);
131 //                System.err.println("    Setting "+path);
132             }
133         }
134         
135         return newModel;
136     }    
137 
138 
139     public static List getExpandedState(TypeTristateTreeModel typeModel, JTree typeTree) {
140         List typeNames = new ArrayList();
141         if ((typeModel != null) && (typeModel.getRoot() != null)) {
142             for (Enumeration e = typeTree.getExpandedDescendants(new TreePath(typeModel.getRoot())); e.hasMoreElements(); ) {
143                 TreePath path = (TreePath)e.nextElement();
144                 // NOTE: do not store the rootnode (always HepRep)
145                 if (path.getPathCount() > 1) {
146                     List typeName = new ArrayList(path.getPathCount()-1);
147                     for (int i=1; i<path.getPathCount(); i++) {
148                         TristateTreeNode node = (TristateTreeNode)path.getPathComponent(i);
149                         typeName.add(WiredHepRepUtil.getHepRepName(node.getUserObject()));
150                     }
151                     typeNames.add(typeName);
152                 }
153             }
154         }
155         return typeNames;
156     }
157     
158     public static void setExpandedState(List/*<List<...>>*/ typeNames, TypeTristateTreeModel typeModel, JTree typeTree) {
159         for (Iterator i=typeNames.iterator(); i.hasNext(); ) {
160             List type = (List)i.next();
161             TristateTreeNode node = (TristateTreeNode)typeModel.getRoot();
162             TreePath path = new TreePath(node);
163             
164             for (Iterator j=type.iterator(); j.hasNext() && (path != null); ) {
165                 String name = (String)j.next();
166                 
167                 boolean found = false;
168                 for (int k=0; !found && (k<node.getChildCount()); k++) {
169                     TristateTreeNode child = (TristateTreeNode)node.getChildAt(k);
170                     if (WiredHepRepUtil.getHepRepName(child.getUserObject()).equals(name)) {
171                         found = true;
172                         node = child;
173                         break;
174                     }
175                 }
176                 
177                 if (found) {
178                     path = path.pathByAddingChild(node);   
179                 } else {
180                     System.err.println("Cannot find '"+name+"' in '"+WiredHepRepUtil.getHepRepName(node.getUserObject())+"'.");
181                     path = null;
182                     break;
183                 }
184             }
185             
186             if (path != null) {
187                 typeTree.expandPath(path);
188             }
189         }
190     }
191     
192     /***
193      * Calculates a center point of all points within this instance (and points of subinstances).
194      */
195     public static double[] getCenterPoint(HepRepInstance instance) {
196         int n=0;
197         double[] t = null;
198 
199         List instances = instance.getInstances();
200         for (Iterator j=instances.iterator(); j.hasNext(); ) {
201             HepRepInstance i = (HepRepInstance)j.next();
202             double[] c = getCenterPoint(i);
203             if (c != null) {
204                 if (t == null) {
205                     t = c;
206                 } else {
207                     t[X] += c[X]; t[Y] += c[Y]; t[Z] += c[Z];
208                 }
209                 n++;
210             }
211         }
212         
213         List points = instance.getPoints();
214         for (Iterator i=points.iterator(); i.hasNext(); ) {
215             HepRepPoint p = (HepRepPoint)i.next();
216             if (t == null) {
217                 t = new double[3];
218                 t[X] = p.getX(); t[Y] = p.getY(); t[Z] = p.getZ();
219             } else {
220                 t[X] += p.getX(); t[Y] += p.getY(); t[Z] += p.getZ();
221             }
222             n++;
223         }
224         
225         if ((n > 0) && (t != null)) {
226             t[X] /= n; t[Y] /= n; t[Z] /= n;
227         }
228         return t;
229     }
230     
231     
232     public static Set/*<HepRepInstance>*/ getInstancesAndChildren(Collection/*<HepRepInstance>*/ instances) {
233         Set s = new HashSet();
234         s.addAll(instances);
235         for (Iterator j=instances.iterator(); j.hasNext(); ) {
236             HepRepInstance i = (HepRepInstance)j.next();
237             s.addAll(getInstancesAndChildren(i.getInstances()));
238         }
239         return s;        
240     }
241 }