Hi Dejan,

It is possible to set a ULCCheckBox as a cell renderer on a Tree node (see
the first snippet below).

However, at present it is NOT possible to set a ULCCheckBox as a cell editor
on a Tree node.

I have opened a bug report: https://www.canoo.com/jira/browse/UBA-6920.

However, you could set ULCCheckBox as a renderer and editor on ULCTableTree
(see second snippet below). So as a work around you can create a single
column ULCTableTree.

Thanks and regards,

Janak

>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] Behalf Of Dejan Pecar
>Sent: Monday, May 15, 2006 9:04 PM
>To: [email protected]
>Subject: [ULC-developer] checkbox editor and renderer in a ulctree
>
>
>hi,
>
>i'm trying to use a checkbox as renderer and editor in a tree.
>but i think it's not possible with ulc 6.0.4.
>
>the renderer was possible to get work with an abbrivation of
>UICheckBox on the client side.
>but this seems more than a hack than a proper solution. i needed
>to override
>
>getTableTreeCellRendererComponent(JTableTree tableTree, Object
>value, boolean selected, boolean hasFocus, boolean expanded,
>boolean leaf, Object node, int row,
>
>on UICheckBox and ignore the value == null case.
>
>
>has anybody an idea how to get this work ? (it seems like nobody
>has done this before)
>i would prefer a solution with serverside code only.
>
>regards
>  Dejan
>mailto:[EMAIL PROTECTED]
---------------------------------

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.IEditorComponent;
import com.ulcjava.base.application.IRendererComponent;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCCheckBox;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCTree;
import com.ulcjava.base.application.tree.DefaultMutableTreeNode;
import com.ulcjava.base.application.tree.DefaultTreeModel;
import com.ulcjava.base.application.tree.ITreeCellEditor;
import com.ulcjava.base.application.tree.ITreeCellRenderer;
import com.ulcjava.base.development.DevelopmentRunner;

public class TreeCellRenderSnippet extends AbstractApplication {
        public void start() {
                final DefaultMutableTreeNode root = new DefaultMutableTreeNode(
                                new MyUserObject("root", false));
                root.add(new DefaultMutableTreeNode(new MyUserObject("child 1", 
true),
                                true));
                root.add(new DefaultMutableTreeNode(new MyUserObject("child 2", 
false),
                                true));

                final DefaultMutableTreeNode child3 = new 
DefaultMutableTreeNode(
                                new MyUserObject("child 3", true));
                final DefaultMutableTreeNode grandChild1 = new 
DefaultMutableTreeNode(
                                new MyUserObject("grandchild 1", false), true);
                child3.add(grandChild1);
                DefaultMutableTreeNode grandChild2 = new DefaultMutableTreeNode(
                                new MyUserObject("grandchild 2", true), true);
                child3.add(grandChild2);
                root.add(child3);
                root.add(new DefaultMutableTreeNode(new MyUserObject("child 4", 
false),
                                true));

                ULCBoxPane contentPane = new ULCBoxPane(1, 0);
                final ULCTree tree = new ULCTree();
                final DefaultTreeModel treeModel = new DefaultTreeModel(root);
                tree.setModel(treeModel);

                tree.setCellRenderer(new MyTreeCellRenderer());
                tree.setCellEditor(new MyTreeCellEditor());
                tree.setEditable(true);

                contentPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, tree);

                ULCFrame frame = new ULCFrame("TreeNodeLabelSnippet");
                frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
                frame.add(contentPane);
                frame.setVisible(true);
        }

        public static void main(String[] args) {
                
DevelopmentRunner.setApplicationClass(TreeCellRenderSnippet.class);
                DevelopmentRunner.main(args);
        }

        public static class MyTreeCellRenderer extends ULCCheckBox implements
                        ITreeCellRenderer {

                public MyTreeCellRenderer() {
                }

                public IRendererComponent getTreeCellRendererComponent(ULCTree 
tree,
                                Object value, boolean selected, boolean 
expanded, boolean leaf,
                                boolean hasFocus) {
                        DefaultMutableTreeNode node = ((DefaultMutableTreeNode) 
value);
                        MyUserObject element = (MyUserObject) 
node.getUserObject();
                        this.setText(element.getText() + element.toString());
                        return this;
                }
        }

        public static class MyTreeCellEditor extends ULCCheckBox implements
                        ITreeCellEditor {

                public MyTreeCellEditor() {
                }

                public IEditorComponent getTreeCellEditorComponent(ULCTree tree,
                                Object value, boolean selected, boolean 
expanded, boolean leaf) {
                        return this;
                }

        }

        class MyUserObject {
                String text;

                boolean selected;

                public MyUserObject(String text, boolean selected) {
                        this.text = text;
                        this.selected = selected;
                }

                public boolean isSelected() {
                        return selected;
                }

                public void setSelected(boolean newValue) {
                        selected = newValue;
                }

                public String getText() {
                        return text;
                }

                public void setText(String newValue) {
                        text = newValue;
                }

                public String toString() {
                        return  Boolean.toString(selected); //"[" + text + "/" 
+ selected + "]";
                }
        }
}

---------------------------------
import com.ulcjava.base.application.*;
import com.ulcjava.base.application.tabletree.*;
import com.ulcjava.base.application.tree.AbstractTreeModel;
import com.ulcjava.base.application.tree.ITreeCellRenderer;
import com.ulcjava.base.application.util.ULCIcon;
import com.ulcjava.base.application.util.Dimension;
import com.ulcjava.base.development.DevelopmentRunner;

/*
 * Copyright C 2000-2003 Canoo Engineering AG, Switzerland.
 */

public class TreeCheckBoxTest extends AbstractApplication {
    private ULCTableTree fTableTree;

    public void start() {
        ULCFrame frame = new ULCFrame("TreeCheckBoxTest");
        fTableTree = new ULCTableTree(new AbstractTableTreeModel() {
            public boolean isCellEditable(Object node, int columnIndex) {
                return true;
            }

            public void setValueAt(Object value, Object node, int column) {
            }

            public int getColumnCount() {
                return 1;
            }

            public Object getValueAt(Object node, int column) {
                return Boolean.FALSE;
            }

            public Object getRoot() {
                return "Label";
            }

            public Object getChild(Object parent, int index) {
                return parent + " : " + index;
            }

            public int getChildCount(Object parent) {
                return 3;
            }

            public boolean isLeaf(Object node) {
                return ((String)node).lastIndexOf(":") > 20;
            }

            public int getIndexOfChild(Object parent, Object child) {
                String childString = (String)child;
                String parentString = (String)parent;
                String indexString =
childString.substring(parentString.length() + 3);
                return Integer.parseInt(indexString);
            }
        });

        // make the table tree look like a tree
        fTableTree.setTableTreeHeader(null);
        fTableTree.setShowGrid(false);
        fTableTree.setIntercellSpacing(new Dimension(0, 0));
        fTableTree.setShowsRootHandles(true);

        // set renderer and editor
        fTableTree.getColumnModel().getColumn(0).setCellEditor(new
MyCellEditor(new ULCCheckBox()));
        fTableTree.getColumnModel().getColumn(0).setCellRenderer(new
MyTreeCellRenderer());

        frame.add(fTableTree);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        DevelopmentRunner.setApplicationClass(TreeCheckBoxTest.class);
        DevelopmentRunner.main(args);
    }

    private static class MyCellEditor extends DefaultCellEditor {
        public MyCellEditor(ULCCheckBox checkBox) {
            super(checkBox);
        }

        public IEditorComponent getTableTreeCellEditorComponent(ULCTableTree
tableTree, Object value, boolean selected, boolean expanded, boolean leaf,
Object node) {
            ULCCheckBox checkBox = (ULCCheckBox)getEditorComponent();
            checkBox.setText((String)node);
            return checkBox;
        }
    }

    private class MyTreeCellRenderer extends ULCCheckBox implements
ITableTreeCellRenderer {
        public IRendererComponent
getTableTreeCellRendererComponent(ULCTableTree tableTree, Object value,
boolean selected, boolean hasFocus, boolean expanded, boolean leaf, Object
node) {
            // adapt the color of the checkbox to the color of the tabletree
            setBackground(selected ? fTableTree.getSelectionBackground() :
fTableTree.getBackground());
            setForeground(selected ? fTableTree.getSelectionForeground() :
fTableTree.getForeground());

            setText((String)node);
            return this;
        }
    }
}

---------------------------------

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to