]> source.dussan.org Git - vaadin-framework.git/commitdiff
fixes #3070, implemented item style generator for tree
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 23 Nov 2009 09:56:59 +0000 (09:56 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 23 Nov 2009 09:56:59 +0000 (09:56 +0000)
svn changeset:9958/svn branch:6.2

src/com/vaadin/terminal/gwt/client/ui/VTree.java
src/com/vaadin/ui/Tree.java
tests/src/com/vaadin/tests/components/tree/ItemStyleGenerator.java [new file with mode: 0644]

index 87b98526c56d74c07af2f53dc49afa5e4a9c3877..87d27caa55e5ecae4ab4efb0ddade298faea89b0 100644 (file)
@@ -15,6 +15,7 @@ import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.SimplePanel;
+import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.BrowserInfo;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
@@ -296,6 +297,13 @@ public class VTree extends FlowPanel implements Paintable {
                 addStyleName(CLASSNAME + "-leaf");
             }
             addStyleName(CLASSNAME);
+            if (uidl.hasAttribute("style")) {
+                addStyleName(CLASSNAME + "-" + uidl.getStringAttribute("style"));
+                Widget.setStyleName(nodeCaptionDiv, CLASSNAME + "-caption-"
+                        + uidl.getStringAttribute("style"), true);
+                childNodeContainer.addStyleName(CLASSNAME + "-children-"
+                        + uidl.getStringAttribute("style"));
+            }
 
             if (uidl.getBooleanAttribute("expanded") && !getState()) {
                 setState(true, false);
index 082b9f46244faf38db5976e2202c2a8b6e469675..825318f32b693e03995b2c536660894ed645516b 100644 (file)
@@ -487,6 +487,13 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
                     target.startTag("leaf");
                 }
 
+                if (itemStyleGenerator != null) {
+                    String stylename = itemStyleGenerator.getStyle(itemId);
+                    if (stylename != null) {
+                        target.addAttribute("style", stylename);
+                    }
+                }
+
                 // Adds the attributes
                 target.addAttribute("caption", getItemCaption(itemId));
                 final Resource icon = getItemIcon(itemId);
@@ -1003,6 +1010,8 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
 
     private int clickListenerCount = 0;
 
+    private ItemStyleGenerator itemStyleGenerator;
+
     public void addListener(ItemClickListener listener) {
         addListener(ItemClickEvent.class, listener,
                 ItemClickEvent.ITEM_CLICK_METHOD);
@@ -1024,4 +1033,43 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
         }
     }
 
+    /**
+     * Sets the {@link ItemStyleGenerator} to be used with this tree.
+     * 
+     * @param itemStyleGenerator
+     *            item style generator or null to remove generator
+     */
+    public void setItemStyleGenerator(ItemStyleGenerator itemStyleGenerator) {
+        if (this.itemStyleGenerator != itemStyleGenerator) {
+            this.itemStyleGenerator = itemStyleGenerator;
+            requestRepaint();
+        }
+    }
+
+    /**
+     * @return the current {@link ItemStyleGenerator} for this tree. Null if
+     *         {@link ItemStyleGenerator} is not set.
+     */
+    public ItemStyleGenerator getItemStyleGenerator() {
+        return itemStyleGenerator;
+    }
+
+    /**
+     * ItemStyleGenerator can be used to add custom styles to tree items. The
+     * CSS class name that will be added to the cell content is
+     * <tt>v-tree-node-[style name]</tt>.
+     */
+    public interface ItemStyleGenerator extends Serializable {
+
+        /**
+         * Called by Tree when an item is painted.
+         * 
+         * @param itemId
+         *            The itemId of the item to be painted
+         * @return The style name to add to this item. (the CSS class name will
+         *         be v-tree-node-[style name]
+         */
+        public abstract String getStyle(Object itemId);
+    }
+
 }
diff --git a/tests/src/com/vaadin/tests/components/tree/ItemStyleGenerator.java b/tests/src/com/vaadin/tests/components/tree/ItemStyleGenerator.java
new file mode 100644 (file)
index 0000000..c4299ec
--- /dev/null
@@ -0,0 +1,59 @@
+package com.vaadin.tests.components.tree;
+
+import java.io.IOException;
+import java.io.StringBufferInputStream;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.CustomLayout;
+import com.vaadin.ui.Tree;
+
+public class ItemStyleGenerator extends TestBase {
+
+    private Component styles;
+    private String css = "<style type=\"text/css\">"
+            + ".v-tree-node-red {color: red;}"
+            + ".v-tree-node-green {color: green;}"
+            + ".v-tree-node-caption-blue {color:blue;}" //
+            + "</style>";
+
+    @Override
+    protected String getDescription() {
+        return "Item Style generator can be used to style items.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 3070;
+    }
+
+    @Override
+    protected void setup() {
+        try {
+            styles = new CustomLayout(new StringBufferInputStream(css));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        addComponent(styles);
+
+        Tree tree = new Tree();
+
+        tree.setItemStyleGenerator(new Tree.ItemStyleGenerator() {
+            public String getStyle(Object itemId) {
+                // simple return itemId as css style name
+                return itemId.toString();
+            }
+        });
+
+        tree.addItem("red");
+        tree.setChildrenAllowed("red", false);
+        tree.addItem("green");
+        tree.addItem("green children");
+        tree.setParent("green children", "green");
+        tree.addItem("blue");
+        tree.addItem("non-blue-childnode");
+        tree.setParent("non-blue-childnode", "blue");
+
+        addComponent(tree);
+    }
+}