1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.component.gchart.model;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.primefaces.model.TreeNode;
32 import org.primefaces.util.Constants;
33
34 public class GChartModelBuilder {
35 private final List<GChartModelRow> rows = new ArrayList<>(0);
36 private GChartType gChartType;
37 private final Map<String, Object> options = new HashMap<>(0);
38 private final List<Object> columns = new ArrayList<>(0);
39
40 public GChartModelBuilder setChartType(final GChartType chartType) {
41 if (gChartType != null) {
42 throw new IllegalStateException("GChart Type already set");
43 }
44 gChartType = chartType;
45
46 return this;
47 }
48
49 public GChartModelBuilder importTreeNode(final TreeNode root) {
50 final String label = String.valueOf(root.getData());
51 final String parentLabel = root.getParent() != null ? String.valueOf(root.getParent().getData()) : Constants.EMPTY_STRING;
52
53 this.addRow(label, parentLabel);
54
55 final List<TreeNode> nodes = root.getChildren();
56 for (final TreeNode node : nodes) {
57 importTreeNode(node);
58 }
59
60 return this;
61 }
62
63 public GChartModelBuilder addColumns(final Object... columns) {
64 this.columns.addAll(Arrays.asList(columns));
65 return this;
66 }
67
68 public GChartModelBuilder addColumns(final Collection<Object> columns) {
69 this.columns.addAll(columns);
70 return this;
71 }
72
73 public GChartModelBuilder addRow(final String label, final Object... objects) {
74 rows.add(new DefaultGChartModelRow(label, Arrays.asList(objects)));
75 return this;
76 }
77
78 public GChartModelBuilder addRow(final String label, final Collection<Object> objects) {
79 rows.add(new DefaultGChartModelRow(label, objects));
80 return this;
81 }
82
83 public GChartModelBuilder addOption(final String name, final Object value) {
84 options.put(name, value);
85 return this;
86 }
87
88 public GChartModel build() {
89 return new DefaultGChartModel(rows, gChartType, options, columns);
90 }
91 }