1 32 33 package com.jeantessier.dependencyfinder.gui; 34 35 import java.util.*; 36 37 import javax.swing.table.*; 38 39 import com.jeantessier.metrics.*; 40 41 public class OOMetricsTableModel extends AbstractTableModel { 42 private static final Integer LOCAL_DISPOSE_IGNORE = new Integer (StatisticalMeasurement.DISPOSE_IGNORE); 43 private static final Integer LOCAL_DISPOSE_MINIMUM = new Integer (StatisticalMeasurement.DISPOSE_MINIMUM); 44 private static final Integer LOCAL_DISPOSE_MEDIAN = new Integer (StatisticalMeasurement.DISPOSE_MEDIAN); 45 private static final Integer LOCAL_DISPOSE_AVERAGE = new Integer (StatisticalMeasurement.DISPOSE_AVERAGE); 46 private static final Integer LOCAL_DISPOSE_STANDARD_DEVIATION = new Integer (StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION); 47 private static final Integer LOCAL_DISPOSE_MAXIMUM = new Integer (StatisticalMeasurement.DISPOSE_MAXIMUM); 48 private static final Integer LOCAL_DISPOSE_SUM = new Integer (StatisticalMeasurement.DISPOSE_SUM); 49 private static final Integer LOCAL_DISPOSE_NB_DATA_POINTS = new Integer (StatisticalMeasurement.DISPOSE_NB_DATA_POINTS); 50 51 private List descriptors; 52 private List metricsList; 53 54 private String [] measurementNames; 55 private MeasurementDescriptor[] measurementDescriptors; 56 private int[] measurementDispose; 57 private Object [][] measurementValues; 58 59 private MetricsComparator comparator = new MetricsComparator("name"); 60 61 public OOMetricsTableModel(List descriptors) { 62 this.descriptors = descriptors; 63 64 buildMetricNames(); 65 buildMetricValues(); 66 } 67 68 public void setMetrics(Collection metricsList) { 69 this.metricsList = new ArrayList(metricsList); 70 71 if (metricsList.isEmpty()) { 72 buildMetricValues(); 73 } else { 74 Collections.sort(this.metricsList, comparator); 75 buildMetricValues(this.metricsList); 76 } 77 78 fireTableStructureChanged(); 79 } 80 81 public MeasurementDescriptor getColumnDescriptor(int column) { 82 return measurementDescriptors[column]; 83 } 84 85 public void updateMetrics(Collection metricsList) { 86 this.metricsList = new ArrayList(metricsList); 87 88 if (metricsList.isEmpty()) { 89 buildMetricValues(); 90 } else { 91 Collections.sort(this.metricsList, comparator); 92 buildMetricValues(this.metricsList); 93 } 94 95 fireTableDataChanged(); 96 } 97 98 public void sortOn(String name, int dispose) { 99 comparator.sortOn(name, dispose); 100 101 Collections.sort(metricsList, comparator); 102 buildMetricValues(metricsList); 103 104 fireTableDataChanged(); 105 } 106 107 private void buildMetricNames() { 108 List names = new LinkedList(); 109 names.add("name"); 110 111 List columnDescriptors = new LinkedList(); 112 columnDescriptors.add(null); 113 114 List dispose = new LinkedList(); 115 dispose.add(LOCAL_DISPOSE_IGNORE); 116 117 Iterator i = descriptors.iterator(); 118 while (i.hasNext()) { 119 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 120 121 if (descriptor.isVisible()) { 122 if (descriptor.getClassFor().equals(StatisticalMeasurement.class)) { 123 names.add(descriptor.getShortName()); 124 columnDescriptors.add(descriptor); 125 dispose.add(LOCAL_DISPOSE_MINIMUM); 126 names.add(descriptor.getShortName()); 127 columnDescriptors.add(descriptor); 128 dispose.add(LOCAL_DISPOSE_MEDIAN); 129 names.add(descriptor.getShortName()); 130 columnDescriptors.add(descriptor); 131 dispose.add(LOCAL_DISPOSE_AVERAGE); 132 names.add(descriptor.getShortName()); 133 columnDescriptors.add(descriptor); 134 dispose.add(LOCAL_DISPOSE_STANDARD_DEVIATION); 135 names.add(descriptor.getShortName()); 136 columnDescriptors.add(descriptor); 137 dispose.add(LOCAL_DISPOSE_MAXIMUM); 138 names.add(descriptor.getShortName()); 139 columnDescriptors.add(descriptor); 140 dispose.add(LOCAL_DISPOSE_SUM); 141 } else { 142 names.add(descriptor.getShortName()); 143 columnDescriptors.add(descriptor); 144 dispose.add(LOCAL_DISPOSE_IGNORE); 145 } 146 } 147 } 148 149 measurementNames = (String []) names.toArray(new String [0]); 150 measurementDescriptors = (MeasurementDescriptor[]) columnDescriptors.toArray(new MeasurementDescriptor[0]); 151 measurementDispose = new int[dispose.size()]; 152 for (int j=0; j<dispose.size(); j++) { 153 measurementDispose[j] = ((Integer ) dispose.get(j)).intValue(); 154 } 155 } 156 157 private void buildMetricValues() { 158 measurementValues = new Object [0][]; 159 } 160 161 private void buildMetricValues(Collection metricsList) { 162 List values = new ArrayList(metricsList.size()); 163 164 Iterator i = metricsList.iterator(); 165 while (i.hasNext()) { 166 Metrics currentMetrics = (Metrics) i.next(); 167 168 Collection currentValues = new ArrayList(measurementNames.length); 169 values.add(currentValues); 170 171 currentValues.add(currentMetrics); 172 173 Iterator j = descriptors.iterator(); 174 while (j.hasNext()) { 175 MeasurementDescriptor descriptor = (MeasurementDescriptor) j.next(); 176 177 if (descriptor.isVisible()) { 178 Measurement measurement = currentMetrics.getMeasurement(descriptor.getShortName()); 179 180 if (measurement instanceof StatisticalMeasurement) { 181 currentValues.add(measurement); 182 currentValues.add(measurement); 183 currentValues.add(measurement); 184 currentValues.add(measurement); 185 currentValues.add(measurement); 186 currentValues.add(measurement); 187 } else { 188 currentValues.add(measurement); 189 } 190 } 191 } 192 } 193 194 measurementValues = new Object [values.size()][]; 195 for (int j=0; j<values.size(); j++) { 196 measurementValues[j] = ((Collection) values.get(j)).toArray(); 197 } 198 } 199 200 public int getColumnCount() { 201 return measurementNames.length; 202 } 203 204 public int getRowCount() { 205 return measurementValues.length; 206 } 207 208 public Object getValueAt(int rowIndex, int columnIndex) { 209 return measurementValues[rowIndex][columnIndex]; 210 } 211 212 public String getRawColumnName(int column) { 213 return measurementNames[column]; 214 } 215 216 public int getRawColumnDispose(int column) { 217 return measurementDispose[column]; 218 } 219 220 public String getColumnName(int column) { 221 String result = getRawColumnName(column); 222 223 switch (getRawColumnDispose(column)) { 224 case StatisticalMeasurement.DISPOSE_MINIMUM: 225 case StatisticalMeasurement.DISPOSE_MEDIAN: 226 case StatisticalMeasurement.DISPOSE_AVERAGE: 227 case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION: 228 case StatisticalMeasurement.DISPOSE_MAXIMUM: 229 case StatisticalMeasurement.DISPOSE_SUM: 230 result += " (" + StatisticalMeasurement.getDisposeAbbreviation(getRawColumnDispose(column)) + ")"; 231 break; 232 case StatisticalMeasurement.DISPOSE_IGNORE: 233 case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS: 234 default: 235 break; 237 } 238 239 return result; 240 } 241 } 242 | Popular Tags |