001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Robert J. Morris <robertj@morris.net>
019: *
020: */
021:
022: package org.swingml.view.renderer;
023:
024: import java.awt.*;
025:
026: import java.util.List;
027:
028: import javax.swing.*;
029: import javax.swing.table.*;
030:
031: import org.swingml.*;
032: import org.swingml.component.*;
033: import org.swingml.model.*;
034: import org.swingml.view.*;
035: import org.swingml.view.Renderer;
036: import org.swingml.model.TableColumnModel;
037:
038: public class JTableRenderer extends RendererUtil implements Renderer {
039:
040: private void applyColumnHeaderRenderers(JTableComponent table,
041: JTableModel model) {
042: List columns = model.getColumns();
043: for (int i = 0; i < columns.size(); i++) {
044: TableColumn col = table.getColumnModel().getColumn(i);
045: col.setHeaderRenderer(new TableHeaderCellRenderer());
046: }
047: }
048:
049: /**
050: * Re-order the columns according to the specified order in the model.
051: */
052: private void applyColumnOrder(JTableComponent component,
053: JTableModel model) {
054: List columns = model.getColumns();
055: javax.swing.table.TableColumnModel componentColumnModel = component
056: .getColumnModel();
057: for (int index = 0; index < columns.size(); index++) {
058: TableColumnModel columnModel = (TableColumnModel) columns
059: .get(index);
060: String columnName = columnModel.getText();
061: int currentIndex = componentColumnModel
062: .getColumnIndex(columnName);
063: int destinationIndex = columnModel.getColumnOrder();
064: // is it a valid destination index?
065: if (destinationIndex >= 0
066: && destinationIndex <= componentColumnModel
067: .getColumnCount()) {
068: componentColumnModel.moveColumn(currentIndex,
069: columnModel.getColumnOrder());
070: } else if (currentIndex != index) {
071: // was trying to be moved... so just put it at the end
072: componentColumnModel.moveColumn(currentIndex, columns
073: .size() - 1);
074: }
075: }
076: }
077:
078: private void applyColumnWidths(JTableComponent component,
079: JTableModel model) {
080: List columns = model.getColumns();
081: for (int i = 0; i < columns.size(); i++) {
082: TableColumnModel columnModel = (TableColumnModel) columns
083: .get(i);
084: if (columnModel.isHidden()) {
085: component.setColumnWidth(i, 0);
086: } else {
087: component.setColumnWidth(i, columnModel.getWidth());
088: }
089: }
090: }
091:
092: public Container render(final AbstractSwingMLModel model,
093: final Container parent) {
094: JTableModel tableModel = (JTableModel) model;
095: JTableComponent table = new JTableComponent(tableModel);
096: applyColumnOrder(table, tableModel);
097: applyColumnWidths(table, tableModel);
098: applyColumnHeaderRenderers(table, tableModel);
099: new TableCellColorDecorator(table);
100: new TableCellComboDecorator(table);
101: new TableCellLabelDecorator(table);
102: new TableCellIconDecorator(table);
103:
104: Dimension d = new Dimension(tableModel.getWidth(), tableModel
105: .getHeight());
106: if (parent instanceof JScrollPane) {
107: JScrollPane scrollPane = (JScrollPane) parent;
108: scrollPane.setViewportView(table);
109: scrollPane.setPreferredSize(d);
110: } else {
111: table.setPreferredSize(d);
112:
113: String orientation = tableModel.getOrientation();
114: if (orientation != null) {
115: parent.add(table, orientation);
116: } else {
117: parent.add(table);
118: }
119: }
120:
121: return table;
122: }
123:
124: }
|