001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.templaterender.peerdelegate.javax.swing;
022:
023: import java.awt.Component;
024: import java.awt.Rectangle;
025: import javax.swing.JTable;
026: import javax.swing.table.*;
027: import org.onemind.awtbridge.peer.BridgePeer;
028: import org.onemind.awtbridge.render.BridgeRenderContext;
029: import org.onemind.awtbridge.render.RenderingException;
030: import org.onemind.swingweb.templaterender.peerdelegate.TemplateRenderDelegate;
031:
032: /**
033: * The JTable render delegate
034: * @author TiongHiang Lee (thlee@onemindsoft.org)
035: *
036: */
037: public class JTableRenderDelegate extends JComponentRenderDelegate {
038: /** the instance **/
039: public static TemplateRenderDelegate INSTANCE = new JTableRenderDelegate();
040:
041: /**
042: * Constructor
043: */
044: public JTableRenderDelegate() {
045: super ();
046: }
047:
048: /**
049: * Render the cell
050: * @param peer the peer
051: * @param context the context
052: * @param output the output object
053: * @param row the row
054: * @param col the column
055: * @throws RenderingException if there's rendering problem
056: */
057: public void renderCell(BridgePeer peer,
058: BridgeRenderContext context, Object output, int row, int col)
059: throws RenderingException {
060: JTable table = (JTable) peer.getComponentObject();
061: TableModel model = table.getModel();
062: Component com = null;
063: boolean edit = table.isEnabled()
064: && model.isCellEditable(row, col);
065: if (edit) {
066: TableCellEditor editor = table.getCellEditor(row, col);
067: com = table.prepareEditor(editor, row, col);
068: } else {
069: TableCellRenderer renderer = table
070: .getCellRenderer(row, col);
071: com = table.prepareRenderer(renderer, row, col);
072: com.setEnabled(false);
073: }
074: BridgePeer childPeer = context.getContext().getPeer(com);
075: if (childPeer == null) {
076: table.add(com);
077: com.addNotify();
078: childPeer = context.getContext().getPeer(com);
079: }
080: Rectangle rect = table.getCellRect(row, col, false);
081: com.setSize(rect.getSize());
082: // masquerade the peer id
083: String origId = childPeer.getId();
084: String newId = peer.getId() + "_" + row + "_" + col;
085: childPeer.setId(newId); //fake it
086: try {
087: context.renderOutput(com, output);
088: } finally {
089: childPeer.setId(origId); //set it back
090: table.removeEditor(); //make the table not in edit state
091: }
092: }
093:
094: /**
095: * Render the header
096: * @param peer the peer
097: * @param context the context
098: * @param output the output object
099: * @throws RenderingException if there's rendering problem
100: */
101: public void renderHeader(BridgePeer peer,
102: BridgeRenderContext context, Object output)
103: throws RenderingException {
104: JTable tb = (JTable) peer.getComponentObject();
105: JTableHeader header = tb.getTableHeader();
106: if (header != null) {
107: BridgePeer headerPeer = context.getContext()
108: .getPeer(header);
109: if (headerPeer == null) {
110: tb.add(header);
111: header.addNotify();
112: headerPeer = context.getContext().getPeer(header);
113: }
114: context.renderOutput(header, output);
115: }
116: }
117: }
|