All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.gui.MeasureTableModel Maven / Gradle / Ivy

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software Copyright (c) 2006 by MCS
 * -------------------------------------- Created on 16.08.2006 by w.klaas
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
/**
 * 
 */
package de.mcs.jmeasurement.gui;

import javax.swing.table.AbstractTableModel;

import de.mcs.jmeasurement.MeasureData;
import de.mcs.jmeasurement.MeasureFactory;
import de.mcs.jmeasurement.MeasurePoint;

/**
 * @author w.klaas
 * 
 */
public class MeasureTableModel extends AbstractTableModel {

  /**
   * 
   */
  private static final long serialVersionUID = 3030242676793959481L;

  /** the measure points. */
  private MeasurePoint[] points;

  /** the filter to use. */
  private String filter;

  /** name of the snapshot to use as data. */
  private String snapshot;

  /**
   * 
   */
  public MeasureTableModel() {
    super();
    points = MeasureFactory.getMeasurePoints(null);
    snapshot = null;
  }

  /**
   * @param snapshotname
   *            using the point in the snapshot..
   */
  public MeasureTableModel(final String snapshotname) {
    super();
    this.snapshot = snapshotname;
    points = MeasureFactory.getSnapShot(snapshotname).getMeasurePoints(null);
  }

  /**
   * Returns the number of rows in the model. A JTable uses
   * this method to determine how many rows it should display. This method
   * should be quick, as it is called frequently during rendering.
   * 
   * @return the number of rows in the model
   * @see #getColumnCount
   * @see javax.swing.table.TableModel#getRowCount()
   */
  public final int getRowCount() {
    return points.length;
  }

  /**
   * Returns the number of columns in the model. A JTable uses
   * this method to determine how many columns it should create and display by
   * default.
   * 
   * @return the number of columns in the model
   * @see #getRowCount
   * @see javax.swing.table.TableModel#getColumnCount()
   */
  public final int getColumnCount() {
    return points[0].getData().length;
  }

  /**
   * Returns Object.class regardless of
   * columnIndex.
   * 
   * @param columnIndex
   *            the column being queried
   * @return the Object.class
   * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
   */
  public final Class getColumnClass(final int columnIndex) {
    if ((points != null) && (points.length > 0)) {
      MeasurePoint point = points[0];
      MeasureData[] values = point.getData();
      MeasureData value = values[columnIndex];
      if (value == null) {
        return String.class;
      }
      return value.getValueClass();
    } else {
      return super.getColumnClass(columnIndex);
    }
  }

  /**
   * Returns the value for the cell at columnIndex and
   * rowIndex.
   * 
   * @param rowIndex
   *            the row whose value is to be queried
   * @param columnIndex
   *            the column whose value is to be queried
   * @return the value Object at the specified cell
   * @see javax.swing.table.TableModel#getValueAt(int, int)
   */
  public final Object getValueAt(final int rowIndex, final int columnIndex) {
    MeasurePoint point = points[rowIndex];
    MeasureData[] values = point.getData();
    MeasureData value = values[columnIndex];
    if (value == null) {
      return "";
    }
    Object data = value.getValue();
    if (value.getValueClass().equals(String[].class)) {
      if (data != null) {
        String[] datavalues = (String[]) data;
        if (datavalues.length > 1) {
          return datavalues[0] + "...";
        } else if (datavalues.length > 0) {
          return datavalues[0];
        } else {
          return "no data";
        }
      }
    }
    if (data == null) {
      if (value.getValueClass().equals(Float.class)) {
        return new Float(0.0);
      }
      try {
        return value.getValueClass().newInstance();
      } catch (InstantiationException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
      return "";
    }
    return data;
  }

  /**
   * Returns a default name for the column using spreadsheet conventions: A,
   * B, C, ... Z, AA, AB, etc. If column cannot be found,
   * returns an empty string.
   * 
   * @param column
   *            the column being queried
   * @return a string containing the default name of column
   * 
   * @see javax.swing.table.AbstractTableModel#getColumnName(int)
   */
  @Override
  public final String getColumnName(final int column) {
    return points[0].getData()[column].getName();
  }

  /**
   * @return Returns the filter.
   */
  public final String getFilter() {
    return filter;
  }

  /**
   * @param aFilter
   *            The filter to set.
   */
  public final void setFilter(final String aFilter) {
    this.filter = aFilter;
    reload();
  }

  /**
   * rreload the measure data.
   */
  private void reload() {
    try {
      if (snapshot == null) {
        points = MeasureFactory.getMeasurePoints(filter);
      } else {
        points = MeasureFactory.getSnapShot(snapshot).getMeasurePoints(filter);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    fireTableDataChanged();
  }

  /**
   * getting the longest value for every column.
   * 
   * @return Object []
   */
  public final Object[] getLongValues() {
    MeasurePoint point = points[0];
    String[] values = new String[point.getData().length];
    for (int i = 0; i < values.length; i++) {
      values[i] = point.getData()[i].getAsString();
    }
    return values;
  }

  /**
   * getting the description for every column.
   * 
   * @param column
   *            column index
   * @return String the description
   */
  public final String getColumnDescription(final int column) {
    return points[0].getData()[column].getDescription();
  }

  /**
   * @return the snapshot
   */
  public final String getSnapshot() {
    return snapshot;
  }

  /**
   * @param snapshotname
   *            the snapshot to set
   */
  public final void setSnapshot(final String snapshotname) {
    this.snapshot = snapshotname;
    reload();
  }

  /**
   * getting a single point with name.
   * 
   * @param pointName
   *            name of the point to get.
   * @return MeasurePoint if the point exists, otherwise null.
   */
  public final MeasurePoint getMeasurePoint(final String pointName) {
    for (int i = 0; i < points.length; i++) {
      MeasurePoint point = points[i];
      if (point.getName().equals(pointName)) {
        return point;
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy