/*
* SwingML Copyright (C) 2002 SwingML Team
*
* 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 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.
*
* Authors: Robert J. Morris <robertj@morris.net>
*
*/
package org.swingml.view.renderer;
import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
import org.swingml.*;
import org.swingml.component.*;
import org.swingml.model.*;
import org.swingml.view.*;
import org.swingml.view.Renderer;
import org.swingml.model.TableColumnModel;
public class JTableRenderer extends RendererUtil implements Renderer {
private void applyColumnHeaderRenderers (JTableComponent table, JTableModel model) {
List columns = model.getColumns();
for (int i = 0; i < columns.size(); i++) {
TableColumn col = table.getColumnModel().getColumn(i);
col.setHeaderRenderer(new TableHeaderCellRenderer());
}
}
/**
* Re-order the columns according to the specified order in the model.
*/
private void applyColumnOrder (JTableComponent component, JTableModel model) {
List columns = model.getColumns();
javax.swing.table.TableColumnModel componentColumnModel = component.getColumnModel();
for (int index = 0; index < columns.size(); index++) {
TableColumnModel columnModel = (TableColumnModel) columns.get(index);
String columnName = columnModel.getText();
int currentIndex = componentColumnModel.getColumnIndex(columnName);
int destinationIndex = columnModel.getColumnOrder();
// is it a valid destination index?
if (destinationIndex >= 0 && destinationIndex <= componentColumnModel.getColumnCount()) {
componentColumnModel.moveColumn(currentIndex, columnModel.getColumnOrder());
} else if (currentIndex != index) {
// was trying to be moved... so just put it at the end
componentColumnModel.moveColumn(currentIndex, columns.size() - 1);
}
}
}
private void applyColumnWidths (JTableComponent component, JTableModel model) {
List columns = model.getColumns();
for (int i = 0; i < columns.size(); i++) {
TableColumnModel columnModel = (TableColumnModel) columns.get(i);
if (columnModel.isHidden()) {
component.setColumnWidth(i, 0);
} else {
component.setColumnWidth(i, columnModel.getWidth());
}
}
}
public Container render (final AbstractSwingMLModel model, final Container parent) {
JTableModel tableModel = (JTableModel) model;
JTableComponent table = new JTableComponent(tableModel);
applyColumnOrder(table, tableModel);
applyColumnWidths(table, tableModel);
applyColumnHeaderRenderers(table, tableModel);
new TableCellColorDecorator(table);
new TableCellComboDecorator(table);
new TableCellLabelDecorator(table);
new TableCellIconDecorator(table);
Dimension d = new Dimension(tableModel.getWidth(), tableModel.getHeight());
if (parent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) parent;
scrollPane.setViewportView(table);
scrollPane.setPreferredSize(d);
} else {
table.setPreferredSize(d);
String orientation = tableModel.getOrientation();
if (orientation != null) {
parent.add(table, orientation);
} else {
parent.add(table);
}
}
return table;
}
}