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);
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

David Botterill

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today