001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 16.08.2006 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: /**
018: *
019: */package de.mcs.jmeasurement.gui;
020:
021: import javax.swing.table.AbstractTableModel;
022:
023: import de.mcs.jmeasurement.MeasureData;
024: import de.mcs.jmeasurement.MeasureFactory;
025: import de.mcs.jmeasurement.MeasurePoint;
026:
027: /**
028: * @author w.klaas
029: *
030: */
031: public class MeasureTableModel extends AbstractTableModel {
032:
033: /**
034: *
035: */
036: private static final long serialVersionUID = 3030242676793959481L;
037:
038: /** the measure points. */
039: private MeasurePoint[] points;
040:
041: /** the filter to use. */
042: private String filter;
043:
044: /** name of the snapshot to use as data. */
045: private String snapshot;
046:
047: /**
048: *
049: */
050: public MeasureTableModel() {
051: super ();
052: points = MeasureFactory.getMeasurePoints(null);
053: snapshot = null;
054: }
055:
056: /**
057: * @param snapshotname
058: * using the point in the snapshot..
059: */
060: public MeasureTableModel(final String snapshotname) {
061: super ();
062: this .snapshot = snapshotname;
063: points = MeasureFactory.getSnapShot(snapshotname)
064: .getMeasurePoints(null);
065: }
066:
067: /**
068: * Returns the number of rows in the model. A <code>JTable</code> uses
069: * this method to determine how many rows it should display. This method
070: * should be quick, as it is called frequently during rendering.
071: *
072: * @return the number of rows in the model
073: * @see #getColumnCount
074: * @see javax.swing.table.TableModel#getRowCount()
075: */
076: public final int getRowCount() {
077: return points.length;
078: }
079:
080: /**
081: * Returns the number of columns in the model. A <code>JTable</code> uses
082: * this method to determine how many columns it should create and display by
083: * default.
084: *
085: * @return the number of columns in the model
086: * @see #getRowCount
087: * @see javax.swing.table.TableModel#getColumnCount()
088: */
089: public final int getColumnCount() {
090: return points[0].getData().length;
091: }
092:
093: /**
094: * Returns <code>Object.class</code> regardless of
095: * <code>columnIndex</code>.
096: *
097: * @param columnIndex
098: * the column being queried
099: * @return the Object.class
100: * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
101: */
102: @SuppressWarnings("unchecked")
103: public final Class getColumnClass(final int columnIndex) {
104: if ((points != null) && (points.length > 0)) {
105: MeasurePoint point = points[0];
106: MeasureData[] values = point.getData();
107: MeasureData value = values[columnIndex];
108: if (value == null) {
109: return String.class;
110: }
111: return value.getValueClass();
112: } else {
113: return super .getColumnClass(columnIndex);
114: }
115: }
116:
117: /**
118: * Returns the value for the cell at <code>columnIndex</code> and
119: * <code>rowIndex</code>.
120: *
121: * @param rowIndex
122: * the row whose value is to be queried
123: * @param columnIndex
124: * the column whose value is to be queried
125: * @return the value Object at the specified cell
126: * @see javax.swing.table.TableModel#getValueAt(int, int)
127: */
128: public final Object getValueAt(final int rowIndex,
129: final int columnIndex) {
130: MeasurePoint point = points[rowIndex];
131: MeasureData[] values = point.getData();
132: MeasureData value = values[columnIndex];
133: if (value == null) {
134: return "";
135: }
136: Object data = value.getValue();
137: if (value.getValueClass().equals(String[].class)) {
138: if (data != null) {
139: String[] datavalues = (String[]) data;
140: if (datavalues.length > 1) {
141: return datavalues[0] + "...";
142: } else if (datavalues.length > 0) {
143: return datavalues[0];
144: } else {
145: return "no data";
146: }
147: }
148: }
149: if (data == null) {
150: if (value.getValueClass().equals(Float.class)) {
151: return new Float(0.0);
152: }
153: try {
154: return value.getValueClass().newInstance();
155: } catch (InstantiationException e) {
156: e.printStackTrace();
157: } catch (IllegalAccessException e) {
158: e.printStackTrace();
159: }
160: return "";
161: }
162: return data;
163: }
164:
165: /**
166: * Returns a default name for the column using spreadsheet conventions: A,
167: * B, C, ... Z, AA, AB, etc. If <code>column</code> cannot be found,
168: * returns an empty string.
169: *
170: * @param column
171: * the column being queried
172: * @return a string containing the default name of <code>column</code>
173: *
174: * @see javax.swing.table.AbstractTableModel#getColumnName(int)
175: */
176: @Override
177: public final String getColumnName(final int column) {
178: return points[0].getData()[column].getName();
179: }
180:
181: /**
182: * @return Returns the filter.
183: */
184: public final String getFilter() {
185: return filter;
186: }
187:
188: /**
189: * @param aFilter
190: * The filter to set.
191: */
192: public final void setFilter(final String aFilter) {
193: this .filter = aFilter;
194: reload();
195: }
196:
197: /**
198: * rreload the measure data.
199: */
200: private void reload() {
201: try {
202: if (snapshot == null) {
203: points = MeasureFactory.getMeasurePoints(filter);
204: } else {
205: points = MeasureFactory.getSnapShot(snapshot)
206: .getMeasurePoints(filter);
207: }
208: } catch (Exception e) {
209: e.printStackTrace();
210: }
211: fireTableDataChanged();
212: }
213:
214: /**
215: * getting the longest value for every column.
216: *
217: * @return Object []
218: */
219: public final Object[] getLongValues() {
220: MeasurePoint point = points[0];
221: String[] values = new String[point.getData().length];
222: for (int i = 0; i < values.length; i++) {
223: values[i] = point.getData()[i].getAsString();
224: }
225: return values;
226: }
227:
228: /**
229: * getting the description for every column.
230: *
231: * @param column
232: * column index
233: * @return String the description
234: */
235: public final String getColumnDescription(final int column) {
236: return points[0].getData()[column].getDescription();
237: }
238:
239: /**
240: * @return the snapshot
241: */
242: public final String getSnapshot() {
243: return snapshot;
244: }
245:
246: /**
247: * @param snapshotname
248: * the snapshot to set
249: */
250: public final void setSnapshot(final String snapshotname) {
251: this .snapshot = snapshotname;
252: reload();
253: }
254:
255: /**
256: * getting a single point with name.
257: *
258: * @param pointName
259: * name of the point to get.
260: * @return MeasurePoint if the point exists, otherwise null.
261: */
262: public final MeasurePoint getMeasurePoint(final String pointName) {
263: for (int i = 0; i < points.length; i++) {
264: MeasurePoint point = points[i];
265: if (point.getName().equals(pointName)) {
266: return point;
267: }
268: }
269: return null;
270: }
271: }
|