You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2020/04/02 16:19:45 UTC

[myfaces-tobago] branch master updated: TOBAGO-1994: TreeListbox is not working correctly * enhance Demo

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new b009fa9  TOBAGO-1994: TreeListbox is not working correctly * enhance Demo
b009fa9 is described below

commit b009fa99d0048f86578fd0d7f66195b008c03e3c
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Thu Apr 2 18:18:28 2020 +0200

    TOBAGO-1994: TreeListbox is not working correctly
    * enhance Demo
---
 .../renderkit/renderer/TreeListboxRenderer.java    | 12 ++-
 .../myfaces/tobago/example/demo/CategoryNode.java  | 46 +++++++++++
 .../myfaces/tobago/example/demo/CategoryTree.java  | 42 ++++++----
 .../tobago/example/demo/TreeListboxController.java |  6 +-
 .../myfaces/tobago/example/demo/category-tree.json | 94 ++++++++++++++++++++++
 5 files changed, 175 insertions(+), 25 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TreeListboxRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TreeListboxRenderer.java
index 8f41dd8..98e7b6b 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TreeListboxRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TreeListboxRenderer.java
@@ -37,6 +37,8 @@ import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
 import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
 import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
@@ -48,6 +50,8 @@ import static org.apache.myfaces.tobago.util.ComponentUtils.SUB_SEPARATOR;
 
 public class TreeListboxRenderer extends RendererBase {
 
+  private static final Logger LOG = LoggerFactory.getLogger(TreeListboxRenderer.class);
+
   @Override
   public void decode(final FacesContext facesContext, final UIComponent component) {
     final AbstractUITree tree = (AbstractUITree) component;
@@ -87,9 +91,11 @@ public class TreeListboxRenderer extends RendererBase {
     List<Integer> nextLevel = new ArrayList<>();
     Integer size = tree.getSize();
     size = Math.max(size != null ? size : 10, 2); // must be > 1, default is 10, if not set
-    final int depth = tree.getTreeDataModel().getDepth() != -1
-        ? tree.getTreeDataModel().getDepth()
-        : 7;  // XXX not a fix value!!!
+    int depth = tree.getTreeDataModel().getDepth();
+    if (depth < 0) {
+      depth = 7; // XXX
+      LOG.warn("No depth, set to {}!", depth);
+    }
     // todo: use (TreeListbox ?)Layout
 //    final Measure currentWidth = tree.getCurrentWidth();
 //    final Measure width = currentWidth.divide(depth);
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryNode.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryNode.java
new file mode 100644
index 0000000..8637d16
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryNode.java
@@ -0,0 +1,46 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import java.util.List;
+
+public class CategoryNode {
+
+  private String id;
+  private String name;
+  private List<CategoryNode> children;
+
+  public CategoryNode() {
+  }
+
+  public String getId() {
+    return id;
+  }
+
+  public void setId(String id) {
+    this.id = id;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public List<CategoryNode> getChildren() {
+    return children;
+  }
+
+  public void setChildren(List<CategoryNode> children) {
+    this.children = children;
+  }
+
+  @Override
+  public String toString() {
+    return "CategoryNode{" +
+        "id='" + id + '\'' +
+        ", name='" + name + '\'' +
+        ", children=" + children +
+        '}';
+  }
+}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryTree.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryTree.java
index d233237..adde05e 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryTree.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CategoryTree.java
@@ -19,9 +19,13 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
 import org.apache.myfaces.tobago.context.Markup;
 
 import javax.swing.tree.DefaultMutableTreeNode;
+import java.io.InputStreamReader;
 
 public class CategoryTree {
 
@@ -29,23 +33,27 @@ public class CategoryTree {
   }
 
   public static DefaultMutableTreeNode createSample() {
-    final DefaultMutableTreeNode tree = createNode("Root Node", "root");
-    tree.insert(createNode("Sports", "sports"), 0);
-    tree.insert(createNode("Movies", "movies"), 1);
-    final DefaultMutableTreeNode music = createNode("Music", "music");
-    tree.insert(music, 2);
-    music.insert(createNode("Classic", "classic"), 0);
-    music.insert(createNode("Pop", "pop"), 1);
-    music.insert(createNode("World", "world"), 2);
-    tree.insert(createNode("Games", "games"), 3);
-    final DefaultMutableTreeNode science = createNode("Science", "science");
-    science.insert(createNode("Geography", "geography"), 0);
-    science.insert(createNode("Mathematics", "math"), 0);
-    final DefaultMutableTreeNode astronomy = createNode("Astronomy", "astronomy");
-    astronomy.insert(createNode("Education", "edu"), 0);
-    astronomy.insert(createNode("Pictures", "pic"), 0);
-    science.insert(astronomy, 2);
-    tree.insert(science, 4);
+
+    final InputStreamReader reader
+        = new InputStreamReader(AstroData.class.getResourceAsStream("category-tree.json"));
+
+    final Gson gson = new GsonBuilder().create();
+    final CategoryNode node = gson.fromJson(reader, new TypeToken<CategoryNode>() {
+    }.getType());
+
+    return buildSubTree(node);
+  }
+
+  private static DefaultMutableTreeNode buildSubTree(CategoryNode node) {
+
+    DefaultMutableTreeNode tree = createNode(node.getName(), node.getId());
+
+    if (node.getChildren() != null) {
+      for (CategoryNode child : node.getChildren()) {
+        tree.add(buildSubTree(child));
+      }
+    }
+
     return tree;
   }
 
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TreeListboxController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TreeListboxController.java
index 97ca6a8..ba884e5 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TreeListboxController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TreeListboxController.java
@@ -45,6 +45,7 @@ public class TreeListboxController implements Serializable {
     sample = CategoryTree.createSample();
     state = new TreeState();
     state.getSelectedState().select(new TreePath(2, 2)); // world music
+    state.getExpandedState().expandAll();
   }
 
   public String submit() {
@@ -63,9 +64,4 @@ public class TreeListboxController implements Serializable {
   public void setState(TreeState state) {
     this.state = state;
   }
-
-//  public String getSelectedNodes() {
-//    return TreeUtils.getSelectedNodes(sample);
-//  }
-
 }
diff --git a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/category-tree.json b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/category-tree.json
new file mode 100644
index 0000000..c79cfdb
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/category-tree.json
@@ -0,0 +1,94 @@
+{
+  "id": "root",
+  "name": "Category",
+  "children": [
+    {
+      "id": "sports",
+      "name": "Sports"
+    },
+    {
+      "id": "movies",
+      "name": "Movies"
+    },
+    {
+      "id": "music",
+      "name": "Music",
+      "children": [
+        {
+          "id": "classic",
+          "name": "Classic"
+        },
+        {
+          "id": "pop",
+          "name": "Pop"
+        },
+        {
+          "id": "world",
+          "name": "World",
+          "children": [
+            {
+              "id": "carib",
+              "name": "Carib"
+            },
+            {
+              "id": "africa",
+              "name": "Africa"
+            }
+          ]
+        }
+      ]
+    },
+    {
+      "id": "games",
+      "name": "Games"
+    },
+    {
+      "id": "science",
+      "name": "Science",
+      "children": [
+        {
+          "id": "math",
+          "name": "Mathematics",
+          "children": [
+            {
+              "id": "analysis",
+              "name": "Analysis"
+            },
+            {
+              "id": "algebra",
+              "name": "Algebra"
+            }
+          ]
+        },
+        {
+          "id": "geography",
+          "name": "Geography"
+        },
+        {
+          "id": "astro",
+          "name": "Astronomy",
+          "children": [
+            {
+              "id": "edu",
+              "name": "Education"
+            },
+            {
+              "id": "pic",
+              "name": "Pictures",
+              "children": [
+                {
+                  "id": "ngc",
+                  "name": "NGC"
+                },
+                {
+                  "id": "messier",
+                  "name": "Messier"
+                }
+              ]
+            }
+          ]
+        }
+      ]
+    }
+  ]
+}