/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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.
*
* Copyright (c) 2009 Pentaho Corporation. All rights reserved.
*/
package org.pentaho.reporting.ui.datasources.table;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.util.TypedTableModel;
import org.pentaho.reporting.libraries.designtime.swing.FormattingTableCellRenderer;
import org.pentaho.reporting.libraries.designtime.swing.GenericCellEditor;
import org.pentaho.reporting.libraries.designtime.swing.GenericCellRenderer;
import org.pentaho.reporting.libraries.designtime.swing.date.DateCellEditor;
import org.pentaho.reporting.libraries.designtime.swing.date.TimeCellEditor;
import org.pentaho.reporting.libraries.designtime.swing.settings.LocaleSettings;
public class TableEditor extends JTable
{
private static final Log logger = LogFactory.getLog(TableEditor.class);
private TableEditorModel tableModel;
private TableColumn selectedColumn;
private EditableHeader tableHeader;
public TableEditor()
{
tableModel = new TableEditorModel();
setModel(tableModel);
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
final TableColumnModel columnModel = getColumnModel();
tableHeader = new EditableHeader(columnModel, tableModel);
setTableHeader(tableHeader);
final SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
setDefaultRenderer(Date.class, new FormattingTableCellRenderer(isoDateFormat));
setDefaultRenderer(java.sql.Date.class, new FormattingTableCellRenderer(new SimpleDateFormat("yyyy-MM-dd")));
setDefaultRenderer(Time.class, new FormattingTableCellRenderer(new SimpleDateFormat("HH:mm:ss.SSS")));
setDefaultRenderer(Timestamp.class, new FormattingTableCellRenderer(isoDateFormat));
setDefaultRenderer(String.class, new GenericCellRenderer());
setDefaultRenderer(Object.class, new GenericCellRenderer());
setDefaultEditor(Number.class, new GenericCellEditor(BigDecimal.class));
setDefaultEditor(Integer.class, new GenericCellEditor(Integer.class));
setDefaultEditor(Float.class, new GenericCellEditor(Float.class));
setDefaultEditor(Double.class, new GenericCellEditor(Double.class));
setDefaultEditor(Short.class, new GenericCellEditor(Short.class));
setDefaultEditor(Byte.class, new GenericCellEditor(Byte.class));
setDefaultEditor(Long.class, new GenericCellEditor(Long.class));
setDefaultEditor(BigInteger.class, new GenericCellEditor(BigInteger.class));
setDefaultEditor(BigDecimal.class, new GenericCellEditor(BigDecimal.class));
setDefaultEditor(String.class, new GenericCellEditor(String.class, true));
setDefaultEditor(Object.class, new GenericCellEditor(String.class, false));
setDefaultEditor(Date.class, new DateCellEditor(Date.class));
setDefaultEditor(java.sql.Date.class, new DateCellEditor(java.sql.Date.class));
setDefaultEditor(Time.class, new TimeCellEditor(Time.class));
setDefaultEditor(Timestamp.class, new DateCellEditor(Timestamp.class));
updateUI();
}
public void applyLocaleSettings(final LocaleSettings localeSettings)
{
try
{
final SimpleDateFormat isoDateFormat =
new SimpleDateFormat(localeSettings.getDatetimeFormatPattern(),localeSettings.getLocale());
isoDateFormat.setTimeZone(localeSettings.getTimeZone());
setDefaultRenderer(Date.class, new FormattingTableCellRenderer(isoDateFormat));
setDefaultRenderer(Timestamp.class, new FormattingTableCellRenderer(isoDateFormat));
final DateCellEditor dateCellEditor = new DateCellEditor(Date.class);
dateCellEditor.setDateFormat(isoDateFormat);
setDefaultEditor(Date.class, dateCellEditor);
final DateCellEditor timestampEditor = new DateCellEditor(Timestamp.class);
timestampEditor.setDateFormat(isoDateFormat);
setDefaultEditor(Timestamp.class, timestampEditor);
}
catch (Exception e)
{
logger.warn("Invalid format string found in locale settings", e);
}
try
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat(localeSettings.getDateFormatPattern(),localeSettings.getLocale());
dateFormat.setTimeZone(localeSettings.getTimeZone());
setDefaultRenderer(java.sql.Date.class, new FormattingTableCellRenderer(dateFormat));
final DateCellEditor dateCellEditor = new DateCellEditor(java.sql.Date.class);
dateCellEditor.setDateFormat(dateFormat);
setDefaultEditor(java.sql.Date.class, dateCellEditor);
}
catch (Exception e)
{
logger.warn("Invalid format string found in locale settings", e);
}
try
{
final SimpleDateFormat timeFormat =
new SimpleDateFormat(localeSettings.getTimeFormatPattern(),localeSettings.getLocale());
timeFormat.setTimeZone(localeSettings.getTimeZone());
setDefaultRenderer(Time.class, new FormattingTableCellRenderer(timeFormat));
final TimeCellEditor timeCellEditor = new TimeCellEditor(Time.class);
timeCellEditor.setDateFormat(timeFormat);
setDefaultEditor(Time.class, timeCellEditor);
}
catch (Exception e)
{
logger.warn("Invalid format string found in locale settings", e);
}
}
/**
* Creates default columns for the table from
* the data model using the <code>getColumnCount</code> method
* defined in the <code>TableModel</code> interface.
* <p/>
* Clears any existing columns before creating the
* new columns based on information from the model.
*
* @see #getAutoCreateColumnsFromModel
*/
public void createDefaultColumnsFromModel()
{
final TableModel m = getModel();
if (m != null)
{
// Remove any current columns
final TableColumnModel cm = getColumnModel();
while (cm.getColumnCount() > 0)
{
cm.removeColumn(cm.getColumn(0));
}
// Create new columns from the data model info
for (int i = 0; i < m.getColumnCount(); i++)
{
final EditableHeaderTableColumn newColumn = new EditableHeaderTableColumn(i);
newColumn.setHeaderEditor(new TypedHeaderCellEditor());
addColumn(newColumn);
}
}
}
public void addColumn(final TableColumn aColumn)
{
if (aColumn.getHeaderValue() == null)
{
final int modelColumn = aColumn.getModelIndex();
final String columnName = getModel().getColumnName(modelColumn);
final Class columnType = getModel().getColumnClass(modelColumn);
aColumn.setHeaderValue(new TypedHeaderInformation(columnType, columnName));
}
getColumnModel().addColumn(aColumn);
}
public void addColumn(final String aHeaderName)
{
tableHeader.removeEditor();
tableModel.addColumn(aHeaderName, String.class);
}
public void addRow()
{
tableModel.addRow();
setRowSelectionInterval(getRowCount() - 1, getRowCount() - 1);
}
public void removeRow()
{
tableModel.removeRow(getSelectedRow());
}
public void removeColumn()
{
tableHeader.removeEditor();
tableModel.removeColumn(selectedColumn.getModelIndex());
}
public void setSelectedColumn(final TableColumn aSelectedColumn)
{
selectedColumn = aSelectedColumn;
}
public void setTableEditorModel(final TableModel model)
{
try
{
tableModel.setSuspendEvents(true);
tableModel.clear();
if (model == null)
{
return;
}
final int columnCount = model.getColumnCount();
for (int col = 0; col < columnCount; col++)
{
tableModel.addColumn(model.getColumnName(col), model.getColumnClass(col));
}
final int rowCount = model.getRowCount();
for (int r = 0; r < rowCount; r++)
{
tableModel.addRow();
for (int col = 0; col < columnCount; col++)
{
tableModel.setValueAt(model.getValueAt(r, col), r, col);
}
}
}
finally
{
tableModel.setSuspendEvents(false);
}
}
public TableModel getTableEditorModel()
{
final TypedTableModel tableModel = new TypedTableModel();
final TableModel model = this.tableModel;
final int columnCount = model.getColumnCount();
for (int col = 0; col < columnCount; col++)
{
tableModel.addColumn(model.getColumnName(col), model.getColumnClass(col));
}
final int rowCount = model.getRowCount();
for (int r = 0; r < rowCount; r++)
{
for (int col = 0; col < columnCount; col++)
{
tableModel.setValueAt(model.getValueAt(r, col), r, col);
}
}
return tableModel;
}
}