/*
* Copyright (C) 2004 TiongHiang Lee
*
* 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
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.templaterender.peerdelegate.javax.swing;
import java.awt.Component;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.table.*;
import org.onemind.awtbridge.peer.BridgePeer;
import org.onemind.awtbridge.render.BridgeRenderContext;
import org.onemind.awtbridge.render.RenderingException;
import org.onemind.swingweb.templaterender.peerdelegate.TemplateRenderDelegate;
/**
* The JTable render delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class JTableRenderDelegate extends JComponentRenderDelegate
{
/** the instance **/
public static TemplateRenderDelegate INSTANCE = new JTableRenderDelegate();
/**
* Constructor
*/
public JTableRenderDelegate()
{
super();
}
/**
* Render the cell
* @param peer the peer
* @param context the context
* @param output the output object
* @param row the row
* @param col the column
* @throws RenderingException if there's rendering problem
*/
public void renderCell(BridgePeer peer, BridgeRenderContext context, Object output, int row, int col) throws RenderingException
{
JTable table = (JTable) peer.getComponentObject();
TableModel model = table.getModel();
Component com = null;
boolean edit = table.isEnabled() && model.isCellEditable(row, col);
if (edit)
{
TableCellEditor editor = table.getCellEditor(row, col);
com = table.prepareEditor(editor, row, col);
} else
{
TableCellRenderer renderer = table.getCellRenderer(row, col);
com = table.prepareRenderer(renderer, row, col);
com.setEnabled(false);
}
BridgePeer childPeer = context.getContext().getPeer(com);
if (childPeer == null)
{
table.add(com);
com.addNotify();
childPeer = context.getContext().getPeer(com);
}
Rectangle rect = table.getCellRect(row, col, false);
com.setSize(rect.getSize());
// masquerade the peer id
String origId = childPeer.getId();
String newId = peer.getId() + "_" + row + "_" + col;
childPeer.setId(newId); //fake it
try
{
context.renderOutput(com, output);
} finally
{
childPeer.setId(origId); //set it back
table.removeEditor(); //make the table not in edit state
}
}
/**
* Render the header
* @param peer the peer
* @param context the context
* @param output the output object
* @throws RenderingException if there's rendering problem
*/
public void renderHeader(BridgePeer peer, BridgeRenderContext context, Object output) throws RenderingException
{
JTable tb = (JTable) peer.getComponentObject();
JTableHeader header = tb.getTableHeader();
if (header != null)
{
BridgePeer headerPeer = context.getContext().getPeer(header);
if (headerPeer == null)
{
tb.add(header);
header.addNotify();
headerPeer = context.getContext().getPeer(header);
}
context.renderOutput(header, output);
}
}
}