001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.gui;
034:
035: import java.util.*;
036: import javax.swing.table.*;
037:
038: import com.jeantessier.metrics.*;
039:
040: public class OOMetricsTableModel extends AbstractTableModel {
041: private static final Integer LOCAL_DISPOSE_IGNORE = StatisticalMeasurement.DISPOSE_IGNORE;
042: private static final Integer LOCAL_DISPOSE_MINIMUM = StatisticalMeasurement.DISPOSE_MINIMUM;
043: private static final Integer LOCAL_DISPOSE_MEDIAN = StatisticalMeasurement.DISPOSE_MEDIAN;
044: private static final Integer LOCAL_DISPOSE_AVERAGE = StatisticalMeasurement.DISPOSE_AVERAGE;
045: private static final Integer LOCAL_DISPOSE_STANDARD_DEVIATION = StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION;
046: private static final Integer LOCAL_DISPOSE_MAXIMUM = StatisticalMeasurement.DISPOSE_MAXIMUM;
047: private static final Integer LOCAL_DISPOSE_SUM = StatisticalMeasurement.DISPOSE_SUM;
048: // private static final Integer LOCAL_DISPOSE_NB_DATA_POINTS = StatisticalMeasurement.DISPOSE_NB_DATA_POINTS;
049:
050: private List<MeasurementDescriptor> descriptors;
051: private List<Metrics> metricsList;
052:
053: private String[] measurementNames;
054: private MeasurementDescriptor[] measurementDescriptors;
055: private int[] measurementDispose;
056: private Object[][] measurementValues;
057:
058: private MetricsComparator comparator = new MetricsComparator("name");
059:
060: public OOMetricsTableModel(List<MeasurementDescriptor> descriptors) {
061: this .descriptors = descriptors;
062:
063: buildMetricNames();
064: buildMetricValues();
065: }
066:
067: public void setMetrics(Collection<Metrics> metricsList) {
068: this .metricsList = new ArrayList<Metrics>(metricsList);
069:
070: if (metricsList.isEmpty()) {
071: buildMetricValues();
072: } else {
073: Collections.sort(this .metricsList, comparator);
074: buildMetricValues(this .metricsList);
075: }
076:
077: fireTableStructureChanged();
078: }
079:
080: public MeasurementDescriptor getColumnDescriptor(int column) {
081: return measurementDescriptors[column];
082: }
083:
084: public void updateMetrics(Collection<Metrics> metricsList) {
085: this .metricsList = new ArrayList<Metrics>(metricsList);
086:
087: if (metricsList.isEmpty()) {
088: buildMetricValues();
089: } else {
090: Collections.sort(this .metricsList, comparator);
091: buildMetricValues(this .metricsList);
092: }
093:
094: fireTableDataChanged();
095: }
096:
097: public void sortOn(String name, int dispose) {
098: comparator.sortOn(name, dispose);
099:
100: Collections.sort(metricsList, comparator);
101: buildMetricValues(metricsList);
102:
103: fireTableDataChanged();
104: }
105:
106: private void buildMetricNames() {
107: List<String> names = new LinkedList<String>();
108: names.add("name");
109:
110: List<MeasurementDescriptor> columnDescriptors = new LinkedList<MeasurementDescriptor>();
111: columnDescriptors.add(null);
112:
113: List<Integer> dispose = new LinkedList<Integer>();
114: dispose.add(LOCAL_DISPOSE_IGNORE);
115:
116: for (MeasurementDescriptor descriptor : descriptors) {
117: if (descriptor.isVisible()) {
118: if (descriptor.getClassFor().equals(
119: StatisticalMeasurement.class)) {
120: names.add(descriptor.getShortName());
121: columnDescriptors.add(descriptor);
122: dispose.add(LOCAL_DISPOSE_MINIMUM);
123: names.add(descriptor.getShortName());
124: columnDescriptors.add(descriptor);
125: dispose.add(LOCAL_DISPOSE_MEDIAN);
126: names.add(descriptor.getShortName());
127: columnDescriptors.add(descriptor);
128: dispose.add(LOCAL_DISPOSE_AVERAGE);
129: names.add(descriptor.getShortName());
130: columnDescriptors.add(descriptor);
131: dispose.add(LOCAL_DISPOSE_STANDARD_DEVIATION);
132: names.add(descriptor.getShortName());
133: columnDescriptors.add(descriptor);
134: dispose.add(LOCAL_DISPOSE_MAXIMUM);
135: names.add(descriptor.getShortName());
136: columnDescriptors.add(descriptor);
137: dispose.add(LOCAL_DISPOSE_SUM);
138: } else {
139: names.add(descriptor.getShortName());
140: columnDescriptors.add(descriptor);
141: dispose.add(LOCAL_DISPOSE_IGNORE);
142: }
143: }
144: }
145:
146: measurementNames = names.toArray(new String[0]);
147: measurementDescriptors = columnDescriptors
148: .toArray(new MeasurementDescriptor[0]);
149: measurementDispose = new int[dispose.size()];
150: for (int j = 0; j < dispose.size(); j++) {
151: measurementDispose[j] = dispose.get(j);
152: }
153: }
154:
155: private void buildMetricValues() {
156: measurementValues = new Object[0][];
157: }
158:
159: private void buildMetricValues(Collection<Metrics> metricsList) {
160: measurementValues = new Object[metricsList.size()][];
161:
162: int i = 0;
163: for (Metrics currentMetrics : metricsList) {
164: List<Measurement> measurements = new ArrayList<Measurement>(
165: measurementNames.length);
166: for (MeasurementDescriptor descriptor : descriptors) {
167: if (descriptor.isVisible()) {
168: Measurement measurement = currentMetrics
169: .getMeasurement(descriptor.getShortName());
170:
171: if (measurement instanceof StatisticalMeasurement) {
172: measurements.add(measurement);
173: measurements.add(measurement);
174: measurements.add(measurement);
175: measurements.add(measurement);
176: measurements.add(measurement);
177: measurements.add(measurement);
178: } else {
179: measurements.add(measurement);
180: }
181: }
182: }
183:
184: measurementValues[i] = new Object[measurements.size() + 1];
185:
186: int j = 0;
187: measurementValues[i][j++] = currentMetrics.getName();
188: for (Measurement measurement : measurements) {
189: measurementValues[i][j++] = measurement;
190: }
191:
192: i++;
193: }
194: }
195:
196: public int getColumnCount() {
197: return measurementNames.length;
198: }
199:
200: public int getRowCount() {
201: return measurementValues.length;
202: }
203:
204: public Object getValueAt(int rowIndex, int columnIndex) {
205: return measurementValues[rowIndex][columnIndex];
206: }
207:
208: public String getRawColumnName(int column) {
209: return measurementNames[column];
210: }
211:
212: public int getRawColumnDispose(int column) {
213: return measurementDispose[column];
214: }
215:
216: public String getColumnName(int column) {
217: String result = getRawColumnName(column);
218:
219: switch (getRawColumnDispose(column)) {
220: case StatisticalMeasurement.DISPOSE_MINIMUM:
221: case StatisticalMeasurement.DISPOSE_MEDIAN:
222: case StatisticalMeasurement.DISPOSE_AVERAGE:
223: case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
224: case StatisticalMeasurement.DISPOSE_MAXIMUM:
225: case StatisticalMeasurement.DISPOSE_SUM:
226: result += " ("
227: + StatisticalMeasurement
228: .getDisposeAbbreviation(getRawColumnDispose(column))
229: + ")";
230: break;
231: case StatisticalMeasurement.DISPOSE_IGNORE:
232: case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
233: default:
234: // Ignore
235: break;
236: }
237:
238: return result;
239: }
240: }
|