/*
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.table;
import java.util.Map;
import javax.swing.table.AbstractTableModel;
import com.jstatcom.model.JSCData;
import com.jstatcom.model.Symbol;
import com.jstatcom.model.SymbolEvent;
import com.jstatcom.model.SymbolListener;
/**
* A table model to be used with JSCAbstractDataTable instances.
* It implements a listener to the underlying symbol that translates symbol
* events to table model events. Subclasses can reference the current symbol via
* getCurrentSymbol. Implementations of the methods
* getRowCount, getColumnCount and
* getValueAt should take the transposed property
* into account to provide a transposed view to the underlying data without
* changin it.
*
* @author Markus Kraetzig
*/
public abstract class JSCAbstractTableModel extends AbstractTableModel {
private Map colIndexEditableMap;
private Map rowIndexEditableMap;
private boolean editable = false;
private Symbol currentSymbol = null;
/**
* Creates a table model for a symbol. It installs a symbol listener
* if symbol != null that translates symbol events into table
* model events.
*
* Subclasses should provide a type check for the symbol.
*
* @param symbol
* the symbol to represent data for the model
*/
public JSCAbstractTableModel(Symbol symbol) {
super();
currentSymbol = symbol;
if (symbol != null)
symbol.addSymbolListener(new SymbolListener() {
public void valueChanged(SymbolEvent evt) {
// because the source might change
currentSymbol = evt.getSource();
fireTableStructureChanged();
}
});
}
/**
* Gets the currently used symbol. Subclasses should use this method to
* reference the symbol. It might be different from the symbol originally
* set in the constructor, when symbols are copied from one table to the
* other together with their attached listeners.
*
* @return the current symbol for the model
*/
protected final Symbol getCurrentSymbol() {
return currentSymbol;
}
/**
* Gets the JSCData object represented by this model. The
* type of the data depends on the implementation.
*
* Usage note:
* The returned data is a reference to the underlying JSCData
* object only until a new data object has been set. It should be used read
* only. Changes that should not affect this model must be made on a copy of
* the returned object.
*
* @return JSCData represented by this model
*/
public final JSCData getJSCData() {
if (currentSymbol == null)
return null;
return currentSymbol.getJSCData();
}
/**
* Gets whether all cells are editable. The default is true.
*
* @return true if editable
*/
public final boolean isEditable() {
return editable;
}
/**
* Sets whether all cells are editable.
*
* @param isEditable
* true if editable
*/
public final void setEditable(boolean isEditable) {
editable = isEditable;
}
/**
* Sets the editable properties for each column. This overwrites the overall
* editable settings.
*
* @param colIndexEditableMap
*/
public void setColEditableMap(Map colIndexEditableMap) {
this.colIndexEditableMap = colIndexEditableMap;
}
/**
* Sets the editable properties for each row. This overwrites the overall
* editable settings.
*
* @param rowIndexEditableMap
*/
public void setRowEditableMap(Map rowIndexEditableMap) {
this.rowIndexEditableMap = rowIndexEditableMap;
}
/**
* Gets the current mapping from column indices to editable properties.
*
* @return editable map
*/
public Map getColIndexEditableMap() {
return colIndexEditableMap;
}
/**
* Gets the current mapping from row indices to editable properties.
*
* @return editable map
*/
public Map getRowIndexEditableMap() {
return rowIndexEditableMap;
}
/**
* Overwrites default implementation to return isEditable for
* all cells. The row index map overwrites all other settings for all
* row indices with non-null elements.
*
* @param rowIndex
* used if (getRowIndexEditableMap() != null),
* row settings overwrite overall editable property, null
* elements fall back to global editable property
* @param columnIndex
* used if (getColIndexEditableMap() != null),
* column settings overwrite overall editable property, null
* elements fall back to global editable property
*
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (colIndexEditableMap == null && rowIndexEditableMap == null)
return editable;
boolean rtn = editable;
if (colIndexEditableMap != null) {
Boolean val = colIndexEditableMap.get(columnIndex);
rtn = (val == null) ? rtn : val; // global coming through
}
if (rowIndexEditableMap != null) {
Boolean val = rowIndexEditableMap.get(rowIndex);
rtn = (val == null) ? rtn : val;
}
return rtn;
}
}
Ads help maintain this website.