/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.ts;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.apache.log4j.Logger;
import com.jstatcom.table.JSCCellRendererTypes;
import com.jstatcom.table.JSCPopupTypes;
import com.jstatcom.table.NumberDisplayTable;
/**
* This class provides a configurable table view of one or more time series with
* editing capabilities. It uses the TSTableModel to represent
* the underlying data.
*
* @see com.jstatcom.ts.TSTableModel
* @author Markus Kraetzig
*/
public final class TSTable extends JTable implements NumberDisplayTable {
private static final Logger log = Logger.getLogger(TSTable.class);
private TSTableModel tSModel = new TSTableModel();
private TableCellRenderer cellRenderer = JSCCellRendererTypes.DEFAULT;
private JPopupMenu tablePopup = JSCPopupTypes.PRECISION;
private MouseListener triggerPopUp = null;
private int precision = 4;
private boolean modelChanged = false;
/**
* TSTable default constructor sets the default renderer,
* model and popup.
*/
public TSTable() {
super();
initialize();
}
/**
* Gets the currently used cell renderer for all cells.
*
* @return the renderer for all cells
*/
public javax.swing.table.TableCellRenderer getCellRenderer() {
return cellRenderer;
}
/**
* Gets number of digits right to decimal point.
*
* @return number precision
*/
public int getPrecision() {
return precision;
}
/**
* @see #setTablePopup
* @return the menu that pops up when the RIGHT mouse is clicked
*/
public JPopupMenu getTablePopup() {
return tablePopup;
}
/**
* Gets the TS that are in sync with the current table model.
*
* @return the TS in the columns of the table
* @throws ClassCastException
* if the underlying model is not of type
* TSTableModel
*/
public TS[] getTS() {
TSTableModel model = (TSTableModel) getModel();
TS[] ret = new TS[model.getColumnCount()];
for (int i = 0; i < model.getColumnCount(); i++) {
String name = model.getColumnName(i);
double[] data = new double[model.getRowCount()];
for (int k = 0; k < data.length; k++)
data[k] = model.getDoubleValueAt(k, i);
TS orig = TSHolder.getInstance().getTS(name);
if (orig != null)
ret[i] = new TS(data, name, model.getStartTSDate(),
orig.type(), orig.project());
else
ret[i] = new TS(data, name, model.getStartTSDate());
}
return ret;
}
/**
* Called whenever the part throws an exception.
*
* @param exception
* Exception
*/
private void handleException(Throwable exception) {
log.error("Unhandled Exception", exception);
}
/**
* Initialize the class.
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
try {
// user code begin {1}
// user code end
setName("TSTableScrollPane");
setSize(197, 190);
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
// user code begin {2}
setModel(tSModel);
setTablePopup(tablePopup);
setCellRenderer(cellRenderer);
setRowSelectionAllowed(true);
// user code end
}
/**
* Gets whether underlying data changed.
*
* @return true if something changed
*/
public boolean isModelChanged() {
return modelChanged;
}
/**
* Override method to set alignment of default editor to
* JLabel.RIGHT. The method also takes care that the same
* cell renderer is used for editing, that is used for the display.
*
* @param editor
* the default editor
* @param row
* index for row
* @param col
* index for column
* @return the adjusted cell editor
*/
public Component prepareEditor(TableCellEditor editor, int row, int col) {
Component c = super.prepareEditor(editor, row, col);
if (c instanceof JTextField) {
JTextField ed = (JTextField) c;
ed.setHorizontalAlignment(SwingConstants.RIGHT);
// Get rendered text for editing default.
Component cc = cellRenderer.getTableCellRendererComponent(this,
getValueAt(row, col), true, true, row, col);
if (cc instanceof JLabel)
ed.setText(((JLabel) cc).getText());
return ed;
}
return c;
}
/**
* Sets a new cell renderer for all columns of the TSTable.
* It also sets the minimum column width for each column.
*
* @param newCellRenderer
* the new renderer, typically an instance of
* DataTableCellRenderers
* @see com.jstatcom.table.JSCCellRendererTypes
*/
public void setCellRenderer(
javax.swing.table.TableCellRenderer newCellRenderer) {
cellRenderer = newCellRenderer;
TableModel m = getModel();
if (m != null) {
TableColumnModel cm = getColumnModel();
int n = cm.getColumnCount();
for (int i = 0; i < n; i++) {
TableColumn tc = cm.getColumn(i);
tc.setCellRenderer(cellRenderer);
tc.setMinWidth(50);
}
repaint();
}
}
/**
* Sets number of digits right to decimal point.
*
* @param newPrecision
* new number precision
* @throws IllegalArgumentException
* if newPrecision < 0
*/
public void setPrecision(int newPrecision) {
if (newPrecision < 0)
throw new IllegalArgumentException("Negative precision argument.");
int oldPrecision = precision;
this.precision = newPrecision;
if (oldPrecision != precision)
repaint();
}
/**
* Sets the new JPopupMenu that appears, if the RIGHT mouse
* is clicked over this table.
*
* @param newTablePopup
* if set to null, then no popup is shown
*/
public void setTablePopup(JPopupMenu newTablePopup) {
tablePopup = newTablePopup;
if (tablePopup == null)
removeMouseListener(triggerPopUp);
else {
triggerPopUp = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
tablePopup.processMouseEvent(e, null, null);
}
};
addMouseListener(triggerPopUp);
}
}
/**
* Sets the TS to be held by the table. The
* TSTableModel takes care of merging the series. All series
* need to have the same sub periodicities.
*
* @param tsArray
* an array with the TS objects to display
* @throws IllegalArgumentException
* if sub periodicities are not equal for all TS
*/
public void setTS(TS[] tsArray) {
tSModel.setTS(tsArray);
setCellRenderer(cellRenderer);
if (tsArray.length > 10)
setAutoResizeMode(AUTO_RESIZE_OFF);
else
setAutoResizeMode(AUTO_RESIZE_SUBSEQUENT_COLUMNS);
modelChanged = false;
}
/* update and set table names
*/
public void updateTable(String[] array) {
tSModel.updateTable(array);
}
public TSTableModel getModel(){
return tSModel;
}
// set data
public void setArrays(Double[][] data, String[] names) {
tSModel.setArrays(data,names);
}
/**
* Sets data at a given position and displays an error message if input
* cannot be parsed to a double.
*
* @param obj
* Object that the method tries to parse to a Double
* @param row
* index for row
* @param col
* index for column
*/
public void setValueAt(Object obj, int row, int col) {
tSModel.setValueAt(obj,row,col);
}
/**
* Invoked when the underlying table model changed.
*
* @param evt
* the model event
*/
public void tableChanged(javax.swing.event.TableModelEvent evt) {
super.tableChanged(evt);
modelChanged = true;
}
}
Ads help maintain this website.