View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.interaction;
3   
4   import java.util.*;
5   import javax.swing.*;
6   import javax.swing.table.*;
7   
8   import hep.graphics.heprep.HepRep;
9   import hep.graphics.heprep.HepRepAttDef;
10  import hep.graphics.heprep.HepRepAttValue;
11  import hep.graphics.heprep.HepRepInstance;
12  import hep.graphics.heprep.HepRepPoint;
13  import hep.graphics.heprep.HepRepType;
14  import hep.graphics.heprep.HepRepTypeTree;
15  import hep.graphics.heprep.util.HepRepUtil;
16  
17  import hep.wired.util.SortableListTableModel;
18  
19  import hep.wired.util.ScientificTable;
20  
21  /***
22   * TableModel for HepRep Attributes.
23   *
24   * @author Mark Donszelmann
25   * @version $Id: AttributeTableModel.java 1935 2005-06-21 18:05:29Z duns $
26   */
27  public class AttributeTableModel extends SortableListTableModel {
28  
29      private List/*<String>*/ categoryList;
30      private Set /*<String>*/ shownCategorySet;
31  
32      private static final int ATTNAME = 0;
33      private static final int ATTVALUE = 1;
34      private static final int ATTUNIT = 2;
35      private static final int ATTNODE = 3;
36      
37      private HepRepInstance instance;
38  
39      private Set/*<String>*/ attributeNames = new HashSet();
40      private List/*<HepRepAttValue>*/ attributeValues;
41      private Map/*<HepRepAttValue, String>*/ attributeNodes = new HashMap();
42          
43      // May be null if no categories exist
44      public AttributeTableModel(List /*<String>*/ categories, Set /*<String>*/ shownCategories) {
45          super(new String[] { "Name", "Value", "Unit", "Node" },
46                new Class[] { String.class, Class.class, String.class, String.class },
47                new ArrayList());
48          attributeValues = getKeys();
49          categoryList = categories;
50          shownCategorySet = shownCategories;
51      }
52                           
53      public int getRowCount() {
54          return (instance != null) ? attributeValues.size() : 0;
55      }
56                          
57      public Object getValueAt(int row, int col) {
58          HepRepAttValue value = (HepRepAttValue)attributeValues.get(row);
59          switch(col) {
60              case ATTNAME: 
61                  return value.getName();
62              case ATTVALUE:
63                  switch(value.getType()) {
64                      case HepRepAttValue.TYPE_BOOLEAN:
65                          return new Boolean(value.getBoolean());
66                      
67                      case HepRepAttValue.TYPE_COLOR:
68                          return value.getColor();
69                      
70                      case HepRepAttValue.TYPE_DOUBLE:
71                          return new Double(value.getDouble());
72                      
73                      case HepRepAttValue.TYPE_INT:
74                          return new Integer(value.getInteger());
75                      
76                      case HepRepAttValue.TYPE_LONG:
77                          return new Long(value.getLong());
78                      
79                      case HepRepAttValue.TYPE_STRING:
80                          return value.getString();
81                      
82                      case HepRepAttValue.TYPE_UNKNOWN:                            
83                      default: 
84                          return value.getAsString();                  
85                  }
86              case ATTUNIT: 
87                  HepRepAttDef def = instance.getType().getAttDef(value.getName());
88                  return def != null ? HepRepUtil.getUnit(def) : "";
89              case ATTNODE: 
90                  return (String)attributeNodes.get(value);
91              default: 
92                  return "";
93          }
94      }
95      
96      protected int compareKeys(Object o1, Object o2, int col, boolean ascending) {
97          HepRepAttValue a1 = (HepRepAttValue)o1;
98          HepRepAttValue a2 = (HepRepAttValue)o2;
99          switch(col) {
100             default:   
101             case ATTNAME: 
102                 return (ascending ? 1 : -1) * 
103                        (a1.getName().compareTo(a2.getName()));
104             case ATTVALUE:
105                 if (a1.getType() == a2.getType()) {
106                     switch(a1.getType()) {
107                         case HepRepAttValue.TYPE_BOOLEAN:
108                             return (ascending ? 1 : -1) * (a1.getBoolean() ? (a2.getBoolean() ? 0 : 1) : a2.getBoolean() ? -1 : 0);
109                         
110                         case HepRepAttValue.TYPE_COLOR: // No order...
111                             return 0;
112                         
113                         case HepRepAttValue.TYPE_DOUBLE:
114                             return (ascending ? 1 : -1) * (a1.getDouble() < a2.getDouble() ? -1 : a1.getDouble() > a2.getDouble() ? 1 : 0);
115                         
116                         case HepRepAttValue.TYPE_INT:
117                             return (ascending ? 1 : -1) * (a1.getInteger() < a2.getInteger() ? -1 : a1.getInteger() > a2.getInteger() ? 1 : 0);
118                         
119                         case HepRepAttValue.TYPE_LONG:
120                             return (ascending ? 1 : -1) * (a1.getLong() < a2.getLong() ? -1 : a1.getLong() > a2.getLong() ? 1 : 0);
121                         
122                         case HepRepAttValue.TYPE_STRING:
123                             return (ascending ? 1 : -1) * (a1.getString().compareTo(a2.getString()));
124                         
125                         case HepRepAttValue.TYPE_UNKNOWN:                            
126                         default: 
127                             return (ascending ? 1 : -1) * (a1.getAsString().compareTo(a2.getAsString()));                  
128                     }
129                 } else {
130                     return (ascending ? 1 : -1) * (a1.getType() < a2.getType() ? -1 : 1);
131                 }
132             case ATTUNIT: 
133                 HepRepAttDef d1 = instance.getType().getAttDef(a1.getName());
134                 HepRepAttDef d2 = instance.getType().getAttDef(a2.getName());
135                 String u1 = d1 != null ? HepRepUtil.getUnit(d1) : "";
136                 String u2 = d2 != null ? HepRepUtil.getUnit(d2) : "";
137                 return (ascending ? 1 : -1) * 
138                        (u1.compareTo(u2));
139             case ATTNODE: 
140                 String b1 = (String)attributeNodes.get(a1);
141                 String b2 = (String)attributeNodes.get(a2);
142                 return (ascending ? 1 : -1) * 
143                        (b1.compareTo(b2));
144         }
145     }
146     
147     public void setInstance(HepRepInstance instance) {
148         this.instance = instance;
149     }
150 
151     public void update() {
152         attributeNames.clear();
153         attributeValues.clear();
154         attributeNodes.clear();
155 
156         HepRepInstance i = instance;
157         boolean onNode = true;
158         while (i != null)  {
159             // add the attributes of the instance
160             HepRepType type = i.getType();
161             addAttributeValues(type, i.getAttValuesFromNode(), onNode ? "Instance" : "Parent");
162             
163             // add the attributes of the points
164             if (onNode) {
165                 Iterator j = i.getPoints().iterator(); 
166                 int n = 1;
167                 while (j.hasNext()) {
168                     HepRepPoint p = (HepRepPoint)j.next();
169                     addAttributeValues(type, p.getAttValuesFromNode(), "Point"+n);
170                     n++;
171                 }
172             }
173             
174             // add the attributes of the instance's type (and the type's parents)
175             while (type != null) {
176                 addAttributeValues(type, type.getAttValuesFromNode(), "Type");
177                 type = type.getSuperType();
178             }
179 
180             // go to the instance's parent
181             if (i.getAttValue("ShowParentAttributes").getBoolean()) {
182                 i = i.getSuperInstance();
183                 onNode = false;
184             } else {
185                 i = null;
186             }  
187         }
188         fireTableDataChanged();
189     }
190     
191     private void addAttributeValues(HepRepType type, Set values, String node) {
192         for (Iterator i = values.iterator(); i.hasNext(); ) {
193             HepRepAttValue value = (HepRepAttValue)i.next();
194             String name = value.getLowerCaseName();
195             
196             // duplcates are allowed for atts on points.
197             if (node.startsWith("Point") || !attributeNames.contains(name)) {
198                 HepRepAttDef attDef = type.getAttDef(value.getName());
199                 if ((attDef == null) || categoryList.isEmpty() || shownCategorySet.contains(attDef.getCategory())) {
200                     attributeNames.add(name);
201                     attributeValues.add(value);
202                     attributeNodes.put(value, node);
203                 }
204             }
205         }        
206     }
207 }