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 13:49:20 UTC

[myfaces-tobago] branch tobago-4.x 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 tobago-4.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


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

commit 4dc3b0fe6dc55d2c7fb45dd9589f79b216d98e12
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Thu Apr 2 15:47:08 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  | 75 ++++++-----------
 .../tobago/example/demo/TreeListboxController.java |  7 +-
 .../myfaces/tobago/example/demo/category-tree.json | 94 ++++++++++++++++++++++
 .../tobago-bootstrap/_version/js/tobago-tree.js    |  2 +-
 6 files changed, 175 insertions(+), 61 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 4f4caa9..eef1e02 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;
@@ -46,6 +48,8 @@ import java.util.List;
 
 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;
@@ -110,9 +114,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 d2f96c3..d2a3733 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,12 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
-import org.apache.myfaces.tobago.context.Markup;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
 
 import javax.swing.tree.DefaultMutableTreeNode;
+import java.io.InputStreamReader;
 
 public class CategoryTree {
 
@@ -29,63 +32,31 @@ 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 astro = createNode("Astronomy", "astro");
-    astro.insert(createNode("Education", "edu"), 0);
-    astro.insert(createNode("Pictures", "pic"), 0);
-    science.insert(astro, 2);
-    tree.insert(science, 4);
-    return tree;
-  }
 
-  public static DefaultMutableTreeNode createNode(final String name, final String id) {
-    return new DefaultMutableTreeNode(new Node(name, id));
+    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);
   }
 
-  public static DefaultMutableTreeNode createSample2() {
-    final DefaultMutableTreeNode tree = new DefaultMutableTreeNode(new Node("1 Category"));
-    tree.add(new DefaultMutableTreeNode(new Node("1.1 Sports")));
-    tree.add(new DefaultMutableTreeNode(new Node("1.2 Movies")));
-    final DefaultMutableTreeNode temp = new DefaultMutableTreeNode(new Node("1.3 Science"));
-    tree.add(temp);
-    final DefaultMutableTreeNode music = new DefaultMutableTreeNode(new Node("1.4 Music"));
-    tree.add(music);
-    tree.add(new DefaultMutableTreeNode(new Node("1.5 Games")));
-    temp.add(new DefaultMutableTreeNode(new Node("1.3.1 Geography (strong markup)", Markup.STRONG)));
-    temp.add(new DefaultMutableTreeNode(new Node("1.3.2 Mathematics (strong markup)", Markup.STRONG)));
-    final DefaultMutableTreeNode temp2 = new DefaultMutableTreeNode(new Node("1.3.3 Pictures"));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.1 Education")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.2 Family")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.3 Comercial")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.4 Summer (disabled)", true)));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.5 Winter (disabled)", true)));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.6 Red")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.7 Black")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.8 White")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.9 Good")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.10 Evil")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.11 Flower")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.12 Animal")));
-    temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.13 Personal")));
-    temp.add(temp2);
-    final DefaultMutableTreeNode bulk = new DefaultMutableTreeNode(new Node("1.6 Bulk"));
-    for (int i = 0; i < 5; i++) {
-      bulk.add(new DefaultMutableTreeNode(new Node("1.6." + (i + 1) + " Some 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));
+      }
     }
-    tree.add(bulk);
 
     return tree;
   }
 
+  public static DefaultMutableTreeNode createNode(final String name, final String id) {
+    return new DefaultMutableTreeNode(new Node(name, id));
+  }
 }
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 fb06d30..e772dcb 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
@@ -42,6 +42,8 @@ public class TreeListboxController implements Serializable {
 
   public TreeListboxController() {
     sample = CategoryTree.createSample();
+    state = new TreeState();
+    state.getExpandedState().expandAll();
   }
 
   public String submit() {
@@ -60,9 +62,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"
+                }
+              ]
+            }
+          ]
+        }
+      ]
+    }
+  ]
+}
diff --git a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/resources/tobago/standard/tobago-bootstrap/_version/js/tobago-tree.js b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/resources/tobago/standard/tobago-bootstrap/_version/js/tobago-tree.js
index c56bf02..9132dca 100644
--- a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/resources/tobago/standard/tobago-bootstrap/_version/js/tobago-tree.js
+++ b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/resources/tobago/standard/tobago-bootstrap/_version/js/tobago-tree.js
@@ -267,7 +267,7 @@ Tobago.TreeListbox.initNextLevel = function() {
   var option = jQuery(this);
   var select = option.closest(".tobago-treeListbox-level").next()
       .find("[data-tobago-tree-parent='" + option.attr("id") + "']");
-  if (select.length == 1) {
+  if (select.length === 1) {
     option.data("tobago-select", select);
   } else {
     var empty = option.closest(".tobago-treeListbox-level").next().children(":first");