David Botterill's Weblog

« Tim's Hitting the... | Main | NetBeans 6.0: Making... »

20071001 Monday October 01, 2007

Code to Make a Tag Cloud in Wicket

 
       Form form = new Form("form");
        add(form);

        final TextField field = new TextField("field", new Model(""));
        form.add(field);

        final Label label = new Label("selectedValue", new Model(""));
        label.setOutputMarkupId(true);
        form.add(label);

        CategoryCount[] counts = getPluginSystem().getCategoryCounts();
        List countsList = Arrays.asList(counts);

        final DataView repeater = new DataView("cloud", new ListDataProvider(countsList)) {

            protected void populateItem(final Item item) {

                MyLink link = new MyLink("link");

                item.add(link);


                String categoryName = ((CategoryCount) item.getModelObject()).getCategoryName();

                String categoryStart = getCurrentPrefix();
                if (null == categoryName) {
                    link.setVisible(false);
                    return;
                }
                /**
                 * If the category doesn't begin with the typed in prefix, don't add it to the cloud.
                 */
                if (null != categoryStart && null != categoryName) {
                    if (!categoryName.toUpperCase().startsWith(categoryStart.toUpperCase())) {
                        link.setVisible(false);
                        return;
                    }
                }

                Label label = new Label("text", ((CategoryCount) item.getModelObject()).getCategoryName());
                Model model = new Model() {
                };
                AttributeModifier am = new AttributeModifier("style", new Model() {

                    @Override
                    public Object getObject() {
                        return getLinkStyle((CategoryCount) item.getModelObject());
                    }
                });
                label.add(am);

                link.add(label); //the Caption property ought to be picked up by reflection
                //or you could create a subclass of AbstractReadOnlyModel to
                //fetch it if reflection bothers you
            }
        };

        add(repeater);
        /**
         *  We need to set the output markup id so it can be used as an AJAX target
         */
        setOutputMarkupId(true);
        /**
         *  Now set up the AJAX handling
         */
        OnChangeAjaxBehavior onChangeAjaxBehavior = new OnChangeAjaxBehavior() {

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                setCurrentPrefix(field.getModelObjectAsString());
                target.addComponent(getCloudPanel());
            }
        };
        field.add(onChangeAjaxBehavior);
Posted by david ( Oct 01 2007, 07:20:19 PM MDT ) Permalink Comments [0] del.icio.us | digg | technorati

Trackback URL: http://blogs.sun.com/david/entry/code_to_make_a_tag
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed