KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > gui > OOMetricsTableModel


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc LOCAL_DISPOSE_IGNORE = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_IGNORE);
43     private static final Integer JavaDoc LOCAL_DISPOSE_MINIMUM = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_MINIMUM);
44     private static final Integer JavaDoc LOCAL_DISPOSE_MEDIAN = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_MEDIAN);
45     private static final Integer JavaDoc LOCAL_DISPOSE_AVERAGE = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_AVERAGE);
46     private static final Integer JavaDoc LOCAL_DISPOSE_STANDARD_DEVIATION = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION);
47     private static final Integer JavaDoc LOCAL_DISPOSE_MAXIMUM = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_MAXIMUM);
48     private static final Integer JavaDoc LOCAL_DISPOSE_SUM = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_SUM);
49     private static final Integer JavaDoc LOCAL_DISPOSE_NB_DATA_POINTS = new Integer JavaDoc(StatisticalMeasurement.DISPOSE_NB_DATA_POINTS);
50
51     private List descriptors;
52     private List metricsList;
53     
54     private String JavaDoc[] measurementNames;
55     private MeasurementDescriptor[] measurementDescriptors;
56     private int[] measurementDispose;
57     private Object JavaDoc[][] 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 JavaDoc 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 JavaDoc[]) names.toArray(new String JavaDoc[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 JavaDoc) dispose.get(j)).intValue();
154         }
155     }
156
157     private void buildMetricValues() {
158         measurementValues = new Object JavaDoc[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 JavaDoc[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 JavaDoc getValueAt(int rowIndex, int columnIndex) {
209         return measurementValues[rowIndex][columnIndex];
210     }
211
212     public String JavaDoc getRawColumnName(int column) {
213         return measurementNames[column];
214     }
215
216     public int getRawColumnDispose(int column) {
217         return measurementDispose[column];
218     }
219
220     public String JavaDoc getColumnName(int column) {
221         String JavaDoc 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                 // Ignore
236
break;
237         }
238
239         return result;
240     }
241 }
242
Popular Tags