Hi Tim,

Tim Hooper wrote:
I want to convert the Checkbox Tree control into a Radio button tree control. Seems like it shouldn't be that hard. I've used the Click framework controls, but never made one myself. Any pointers on how to get started?


I just had a look at the CheckboxTree and its not as straightforward to change from checkbox to radio as it could be. Ideally Tree should expose a method such as #createControl which returns a Checkbox but could be overridden to return a Radio.

In the meantime you could create a custom RadioTree which extends CheckboxTree and specify a custom Decorator.

Below is some code which could help.

kind regards

bob


    class RadioTree extends CheckboxTree {
        public RadioTree(String name) {
            super(name);
            setDecorator(new RadioDecorator());
        }

        class RadioDecorator implements Decorator {
            public String render(Object object, Context context) {
                TreeNode treeNode = (TreeNode) object;
                HtmlStringBuffer buffer = new HtmlStringBuffer();

                renderIcon(buffer, treeNode);

                buffer.append("<input ");
                buffer.append(" style=\"margin:0\" type=\"radio\"");
                buffer.appendAttribute("name", SELECT_TREE_NODE_PARAM);
                buffer.appendAttribute("value", treeNode.getId());

                if (treeNode.isSelected()) {
                    buffer.appendAttribute("checked", "checked");
                }

                buffer.elementEnd();

                buffer.elementStart("span");
                if (treeNode.isSelected()) {
                    buffer.appendAttribute("class", "selected");
                } else {
                    buffer.appendAttribute("class", "unselected");
                }
                buffer.closeTag();

                renderValue(buffer, treeNode);
                buffer.elementEnd("span");

                return buffer.toString();
            }

            /**
             * Render the node's value.
             *
             * @param buffer string buffer containing the markup
             * @param treeNode treeNode to render
             */
protected void renderValue(HtmlStringBuffer buffer, TreeNode treeNode) {

                //just print normal value
                if (treeNode.getValue() != null) {
                    buffer.append(treeNode.getValue());
                }
                buffer.append("\n");
            }
        }
    }


Reply via email to