View Javadoc

1   package org.lcsim.plugin.browser;
2   
3   import javax.swing.table.AbstractTableModel;
4   import java.util.List;
5   import java.util.Vector;
6   import org.lcsim.event.EventHeader.LCMetaData;
7   import org.lcsim.event.GenericObject;
8   import org.lcsim.lcio.LCIOConstants;
9   import org.lcsim.lcio.LCIOUtil;
10  
11  /** 
12   * Table model for LCGenericObjects. If the objects are fixed size and have a
13   * valid data description in the collection parameters this is used for
14   * displaying the data. Otherwise the data is displayed in a generic format, 
15   * i.e. using nInt, nFloat and nDouble.
16   *
17   * @author gaede
18   * @version $Id: LCGenericObjectTableModel.java,v 1.2 2011/04/01 22:05:04 tonyj Exp $
19   */
20  class LCGenericObjectTableModel extends AbstractTableModel implements EventBrowserTableModel
21  {
22     private List data;
23     private String[] columns = {"index","nInt","intValues","nFloat","floatValues","nDouble","doubleValues"};
24     private Class[] klasses = { Integer.class, Integer.class , new int[0].getClass(), Integer.class , new float[0].getClass(), Integer.class , new double[0].getClass() };
25     private boolean isFixedSize = false;
26     private int[] indices = null;
27     
28     public boolean canDisplay(Class c)
29     {
30        return GenericObject.class.isAssignableFrom(c);
31     }
32     public void setData(LCMetaData meta, List data)
33     {
34        this.data = data;
35        int flags = meta.getFlags();
36        
37        String[] desc = meta.getStringParameters().get("DataDescription");
38        isFixedSize = LCIOUtil.bitTest(flags, LCIOConstants.GOBIT_FIXED);
39        
40        if( isFixedSize && desc != null && desc.length != 0 && desc[0].length() != 0 )
41        {
42           // some helper vectors
43           Vector colVec = new Vector() ;
44           Vector klassVec = new Vector() ;
45           Vector indexVec = new Vector() ;
46           
47           colVec.add( "index" ) ;
48           klassVec.add( Integer.class ) ;
49           
50           try
51           {
52              
53              evaluateDataDescription( data, desc[0], colVec, klassVec , indexVec ) ;
54              
55              // copy vectors to arrays ...
56              String[] newColumns = new String[ colVec.size() ] ;
57              for(int i=0;i<colVec.size() ;i++)
58                 newColumns[i] = (String) colVec.get(i) ;
59              columns = newColumns ;
60              
61              Class[] newKlasses = new Class[ klassVec.size() ] ;
62              for(int i=0;i<klassVec.size() ;i++)
63                 newKlasses[i] = (Class) klassVec.get(i) ;
64              klasses =  newKlasses ;
65              
66              indices = new int[ indexVec.size() ] ;
67              for(int i=0;i<indexVec.size() ;i++)
68                 indices[i] = ( (Integer) indexVec.get(i) ).intValue() ;
69           }
70           catch( Exception e)
71           {
72              isFixedSize = false ; // sth. went wrong with the data description ...
73           }
74        }
75        else
76        {
77           isFixedSize = false ; // in case ther is no data description ...
78        }
79        fireTableDataChanged();
80     }
81     
82     /** Helper method that determines the colums and classes for the table from the data description string */
83     void evaluateDataDescription(List data, String desc, Vector colVec, Vector klassVec, Vector indexVec ) throws Exception
84     {
85        
86        int nInt=0, nFloat=0, nDouble=0 ;
87        
88        String values[] = desc.split(",") ;
89        for( int i=0 ; i< values.length ; i++)
90        {
91           
92           String value[] = values[i].split(":") ;
93           
94           if( value[0].equals("i"))
95           {
96              colVec.add( value[1] ) ;
97              klassVec.add( Integer.class ) ;
98              indexVec.add( new Integer( nInt ++ ) )  ;
99           }
100          if( value[0].equals("x"))
101          {
102             colVec.add( value[1] ) ;
103             klassVec.add( String.class ) ;  // print hex numbers in table
104             indexVec.add( new Integer( nInt ++ ) )  ;
105          }
106          if( value[0].equals("f"))
107          {
108             colVec.add( value[1] ) ;
109             klassVec.add( Float.class ) ;
110             indexVec.add( new Integer( nFloat ++ ) )  ;
111          }
112          if( value[0].equals("d"))
113          {
114             colVec.add( value[1] ) ;
115             klassVec.add( Double.class ) ;
116             indexVec.add( new Integer( nDouble ++ ) )  ;
117          }
118       }
119       
120       // now do some sanity checks:
121       
122       GenericObject obj = (GenericObject) data.get(0) ;
123       if(  obj.getNInt() != nInt || obj.getNFloat() != nFloat || obj.getNDouble() != nDouble )
124       {
125          throw new Exception("Wrong data description string !"
126                  +  obj.getNInt()+" : " + nInt
127                  +  obj.getNFloat()+" : " + nFloat
128                  +  obj.getNDouble()+" : " + nDouble ) ;
129       }
130    }
131    
132    public int getRowCount()
133    {
134       return data == null ? 0 : data.size();
135    }
136    public int getColumnCount()
137    {
138       return columns.length;
139    }
140    public String getColumnName(int index)
141    {
142       return columns[index];
143    }
144    public Class getColumnClass(int index)
145    {
146       return klasses[index];
147    }
148    
149    public Object getValueAt(int row, int column)
150    {
151       GenericObject obj = (GenericObject) data.get(row);
152       
153       if( isFixedSize )
154       {
155          
156          if( column == 0 )
157             return new Integer(row);
158          
159          if( klasses[column] == Integer.class )
160             return new Integer( obj.getIntVal( indices[ column-1 ] ) ) ;
161          
162          if( klasses[column] == String.class )
163          {
164             String zeros = "0x00000000" ;
165             String hex = Integer.toHexString( obj.getIntVal( indices[ column-1 ]  ) ) ;
166             return zeros.substring(0, ( 10 - hex.length() ) ) + hex  ;
167          }
168          
169          if( klasses[column] == Float.class )
170             return new Float( obj.getFloatVal( indices[ column-1 ] ) );
171          
172          if( klasses[column] == Double.class )
173             return new Double( obj.getDoubleVal( indices[ column-1 ] ) ) ;
174          
175       }
176       else
177       {  // generic format
178          
179          switch (column)
180          {
181             
182             case 0:
183                return new Integer(row);
184                
185             case 1:
186                return new Integer( obj.getNInt() );
187                
188             case 2:
189                int[] ints = new int[ obj.getNInt() ] ;
190                for(int i=0;i<obj.getNInt();i++)
191                   ints[i] = obj.getIntVal(i) ;
192                return ints ;
193                
194             case 3:
195                return new Integer( obj.getNFloat() );
196                
197             case 4:
198                float[] floats = new float[ obj.getNFloat() ] ;
199                for(int i=0;i<obj.getNFloat();i++)
200                   floats[i] = obj.getFloatVal(i) ;
201                return floats ;
202                
203             case 5:
204                return new Integer( obj.getNDouble() );
205                
206             case 6:
207                double[] doubles = new double[ obj.getNDouble() ] ;
208                for(int i=0;i<obj.getNDouble();i++)
209                   doubles[i] = obj.getDoubleVal(i) ;
210                return doubles ;
211                
212          }
213          
214       }
215       return " " ;
216    }
217 }