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 javax.swing.JTree;
025: import javax.swing.tree.TreeModel;
026: import javax.swing.tree.TreePath;
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 JTreeRenderDelegate extends JComponentRenderDelegate {
038: /** the instance **/
039: public static TemplateRenderDelegate INSTANCE = new JTreeRenderDelegate();
040:
041: /**
042: * Constructor
043: */
044: public JTreeRenderDelegate() {
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 renderNode(BridgePeer peer,
058: BridgeRenderContext context, Object output, TreePath path,
059: String treePath) throws RenderingException {
060: JTree tree = (JTree) peer.getComponentObject();
061: TreeModel model = tree.getModel();
062: Component com = null;
063: Object value = path.getLastPathComponent();
064:
065: boolean enableFlag = true;
066: boolean edit = tree.isEditable();
067: if (edit) {
068: com = (Component) tree.getCellEditor()
069: .getTreeCellEditorComponent(tree, value, true,
070: true, model.isLeaf(value), 0);
071: } else {
072: com = (Component) tree.getCellRenderer()
073: .getTreeCellRendererComponent(tree, value, true,
074: true, model.isLeaf(value), 0, true);
075: }
076: BridgePeer childPeer = context.getContext().getPeer(com);
077: if (childPeer == null) {
078: tree.add(com);
079: com.addNotify();
080: childPeer = context.getContext().getPeer(com);
081: }
082: //setting the size?
083:
084: // masquerade the peer id
085: if (edit) {
086: String origId = childPeer.getId();
087: String newId = peer.getId() + "_" + treePath;
088: childPeer.setId(newId); //fake it
089: try {
090: context.renderOutput(com, output);
091: } finally {
092: childPeer.setId(origId); //set it back
093: }
094: } else { //just render
095: try {
096: context.renderOutput(com, output);
097: } finally {
098: com.setEnabled(enableFlag);
099: }
100: }
101: }
102: }
|