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;
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);
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);
private int clickListenerCount = 0;
+ private ItemStyleGenerator itemStyleGenerator;
+
public void addListener(ItemClickListener listener) {
addListener(ItemClickEvent.class, listener,
ItemClickEvent.ITEM_CLICK_METHOD);
}
}
+ /**
+ * 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);
+ }
+
}
--- /dev/null
+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);
+ }
+}