View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import com.google.gson.Gson;
23  import com.google.gson.GsonBuilder;
24  import com.google.gson.reflect.TypeToken;
25  import org.apache.myfaces.tobago.context.Markup;
26  
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import java.io.InputStreamReader;
29  
30  public class CategoryTree {
31  
32    private CategoryTree() {
33    }
34  
35    public static DefaultMutableTreeNode createSample() {
36  
37      final InputStreamReader reader
38          = new InputStreamReader(AstroData.class.getResourceAsStream("category-tree.json"));
39  
40      final Gson gson = new GsonBuilder().create();
41      final CategoryNode node = gson.fromJson(reader, new TypeToken<CategoryNode>() {
42      }.getType());
43  
44      return buildSubTree(node);
45    }
46  
47    private static DefaultMutableTreeNode buildSubTree(CategoryNode node) {
48  
49      DefaultMutableTreeNode tree = createNode(node.getName(), node.getId());
50  
51      if (node.getChildren() != null) {
52        for (CategoryNode child : node.getChildren()) {
53          tree.add(buildSubTree(child));
54        }
55      }
56  
57      return tree;
58    }
59  
60    public static DefaultMutableTreeNode createNode(final String name, final String id) {
61      return new DefaultMutableTreeNode(new Node(name, id));
62    }
63  
64    public static DefaultMutableTreeNode createSample2() {
65      final DefaultMutableTreeNode tree = new DefaultMutableTreeNode(new Node("1 Category"));
66      tree.add(new DefaultMutableTreeNode(new Node("1.1 Sports")));
67      tree.add(new DefaultMutableTreeNode(new Node("1.2 Movies")));
68      final DefaultMutableTreeNode temp = new DefaultMutableTreeNode(new Node("1.3 Science"));
69      tree.add(temp);
70      final DefaultMutableTreeNode music = new DefaultMutableTreeNode(new Node("1.4 Music"));
71      tree.add(music);
72      tree.add(new DefaultMutableTreeNode(new Node("1.5 Games")));
73      temp.add(new DefaultMutableTreeNode(new Node("1.3.1 Geography (strong markup)", Markup.STRONG)));
74      temp.add(new DefaultMutableTreeNode(new Node("1.3.2 Mathematics (strong markup)", Markup.STRONG)));
75      final DefaultMutableTreeNode temp2 = new DefaultMutableTreeNode(new Node("1.3.3 Pictures"));
76      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.1 Education")));
77      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.2 Family")));
78      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.3 Comercial")));
79      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.4 Summer (disabled)", true)));
80      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.5 Winter (disabled)", true)));
81      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.6 Red")));
82      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.7 Black")));
83      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.8 White")));
84      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.9 Good")));
85      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.10 Evil")));
86      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.11 Flower")));
87      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.12 Animal")));
88      temp2.add(new DefaultMutableTreeNode(new Node("1.3.3.13 Personal")));
89      temp.add(temp2);
90      final DefaultMutableTreeNode bulk = new DefaultMutableTreeNode(new Node("1.6 Bulk"));
91      for (int i = 0; i < 5; i++) {
92        bulk.add(new DefaultMutableTreeNode(new Node("1.6." + (i + 1) + " Some Node")));
93      }
94      tree.add(bulk);
95  
96      return tree;
97    }
98  
99  }