View Javadoc

1   /*
2    * ObjectTableModelData.java
3    * Created on 25 Август 2006 г., 15:35
4    */
5   
6   package org.weda.model.impl;
7   
8   import java.util.List;
9   import java.util.Map;
10  import org.weda.model.TableModel;
11  import org.weda.model.TableModelData;
12  import org.weda.model.TableModelDataException;
13  import org.weda.model.TableModelException;
14  import org.weda.model.impl.ObjectTableModel.Position;
15  import org.weda.property.PropertyDescriptor;
16  import org.weda.property.PropertyValue;
17  import org.weda.store.ObjectSource;
18  
19  /**
20   *
21   * @author Mikhail Titov
22   */
23  public class ObjectTableModelData implements TableModelData {
24      
25      private PropertyValue propertyValueGetter;    
26      private ObjectSource objectSource;
27      private Position[] positions;
28      private List<PropertyDescriptor> columns;
29      private Map<Object, String>[] valuesAliases;
30      
31      private ObjectTableModel model;    
32      
33      public ObjectTableModelData(ObjectTableModel model) 
34          throws TableModelException
35      {
36          this.model = model;
37          this.objectSource = model.getObjectSource();
38          this.positions = model.getPositions();
39          this.columns = model.getColumnDescriptors();
40          this.propertyValueGetter = model.getPropertyValueGetter();
41          this.valuesAliases = model.getValuesAliases();
42      }
43  
44      public void selectRow(int row) {
45          objectSource.selectRow(row);        
46      }
47  
48      public boolean isRowSelected(int row) {
49          return objectSource.isRowSelected(row);        
50      }
51  
52      public boolean hasSummaryRow() throws TableModelException {
53          try{            
54              return     objectSource.getSummaryRow() != null
55                      && objectSource.getRowCount()>0 ;                    
56          }catch(Exception e){
57              throw new TableModelException(
58                      String.format(
59                          "Error while checking for summary row " +
60                          "in object table model (%s)"
61                          , model.getName())
62                      , e);            
63          }
64      }
65      
66      public Object getSummaryValueAt(int col) throws TableModelException {
67          try{
68              Position pos = positions[col];
69              if (pos.hasSummaryValue){
70                  return objectSource.getSummaryRow().getResultValue(
71                          pos.inPos, columns.get(col).getName());
72              }else
73                  return null;
74          }catch(Exception e){
75              throw new TableModelException(
76                      String.format(
77                          "Can't get summary value for column (%d) " +
78                          "for object table model (%s)"
79                          , col, model.getName())
80                      , e);
81              
82          }
83      }
84  
85      public void deselectRow(int row) {
86          objectSource.deselectRow(row);
87      }
88  
89      public Object getValueAt(int row, int col) throws TableModelException {
90          try{
91              Position pos = positions[col];
92              Object obj = objectSource.getRowAt(row)[pos.inPos];
93              if (obj==null)
94                  return null;
95              else {
96                  Object value;
97                  if (pos.propertyIndex==null)
98                      value = obj;
99                  else 
100                     value = 
101                         propertyValueGetter.getValue(obj, pos.propertyIndex);
102                 if (valuesAliases[col]==null)
103                     return value;
104                 else
105                     return valuesAliases[col].get(value);
106             }
107         }catch(Exception e){
108             throw new TableModelException(
109                     String.format(
110                         "Can't get value at position (row=%d, col=%d) " +
111                         "for object table model (%s)"
112                         , row, col, model.getName())
113                     , e);
114         }
115     }
116 
117     public int getSelectedRow() {
118         return objectSource.getSelectedRow();
119     }
120 
121     public int getRowCount() throws TableModelException {
122         return objectSource.getRowCount();
123     }
124 
125     public int getSelectedRowCount() {
126         return objectSource.getSelectedRowCount();
127     }
128     
129     
130 }