1 | |
package net.sf.jolene.dom; |
2 | |
|
3 | |
import net.sf.jolene.constants.StyleClasses; |
4 | |
import net.sf.jolene.constants.Tags; |
5 | |
import org.apache.commons.beanutils.PropertyUtils; |
6 | |
import org.apache.log4j.LogManager; |
7 | |
import org.apache.log4j.Logger; |
8 | |
|
9 | |
import java.util.ArrayList; |
10 | |
import java.util.Iterator; |
11 | |
import java.util.List; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | 1 | public final class Grid extends HTMLElement { |
22 | |
|
23 | 1 | private static final Logger log = LogManager.getLogger(Grid.class); |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
List<GridRow> rows; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 12 | public Grid() { |
34 | 12 | tag = Tags.table; |
35 | 12 | rows = new ArrayList<GridRow>(0); |
36 | 12 | } |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public void bind(List<GridColumn> header, List<String> fields, List data) throws GridBindException { |
47 | 4 | rows = new ArrayList<GridRow>(data.size()); |
48 | |
|
49 | 4 | if (header.size() > 0) { |
50 | 4 | Iterator<GridColumn> it = header.iterator(); |
51 | 4 | rows.add(new GridRow()); |
52 | 4 | rows.get(0).setHeader(true); |
53 | 13 | while (it.hasNext()) { |
54 | 9 | rows.get(0).cells.add(new GridCell(it.next())); |
55 | |
} |
56 | |
} |
57 | |
|
58 | 4 | if (data == null || data.size() == 0) { |
59 | 0 | return; |
60 | |
} |
61 | |
|
62 | 4 | int rowPosition = 1; |
63 | 4 | Iterator it = data.iterator(); |
64 | |
|
65 | 12 | while (it.hasNext()) { |
66 | |
Object row; |
67 | 8 | row = it.next(); |
68 | 8 | rows.add(new GridRow()); |
69 | |
|
70 | 8 | Iterator<String> fit = fields.iterator(); |
71 | |
|
72 | 8 | int colPosition = 0; |
73 | 26 | while (fit.hasNext()) { |
74 | 18 | String fieldName = fit.next(); |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 18 | GridColumn col = header.get(colPosition); |
80 | |
GridCell cell; |
81 | 18 | cell = new GridCell(col); |
82 | 18 | if (col.getCellObject() != null) { |
83 | 4 | cell.setCellObject(col.getCellObject().clone()); |
84 | |
|
85 | |
int n1; |
86 | 4 | String cellName = cell.getCellObject().getName(); |
87 | 4 | n1 = cellName.indexOf("${"); |
88 | 4 | if (n1 > -1) { |
89 | |
|
90 | |
|
91 | 0 | int n2 = cellName.indexOf("}"); |
92 | 0 | String propertyName = cellName.substring(n1 + 2, n2).trim(); |
93 | |
|
94 | |
try { |
95 | 0 | cellName = cellName.substring(0, n1) + PropertyUtils.getProperty(row, propertyName) + cellName.substring(n2 + 1); |
96 | 0 | } catch (Exception e) { |
97 | 0 | throw new GridBindException(e); |
98 | 0 | } |
99 | 0 | cell.getCellObject().setName(cellName); |
100 | |
} |
101 | |
} |
102 | |
|
103 | 18 | log.debug(fieldName + " " + row.getClass().getName()); |
104 | |
try { |
105 | 18 | cell.setValue("" + PropertyUtils.getProperty(row, fieldName)); |
106 | 0 | } catch (Exception e) { |
107 | 0 | throw new GridBindException(e); |
108 | 18 | } |
109 | 18 | rows.get(rowPosition).cells.add(cell); |
110 | |
|
111 | 18 | if (rowPosition % 2 == 0) { |
112 | 9 | rows.get(rowPosition).setAttribute("class", StyleClasses.EvenRow.toString()); |
113 | |
} else { |
114 | 9 | rows.get(rowPosition).setAttribute("class", StyleClasses.OddRow.toString()); |
115 | |
} |
116 | 18 | colPosition++; |
117 | 18 | } |
118 | 8 | rowPosition++; |
119 | 8 | } |
120 | 4 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
@Override |
129 | |
public Grid clone() { |
130 | 1 | Grid grid = (Grid) super.clone(); |
131 | |
|
132 | 3 | for (int j = 0; j < rows.size(); j++) { |
133 | 2 | GridRow r = rows(j).clone(); |
134 | 8 | for (int i = 0; i < rows(j).cells.size(); i++) { |
135 | 6 | r.cells.add(rows(j).cells(i).clone()); |
136 | |
} |
137 | 2 | grid.rows.add(r); |
138 | |
} |
139 | 1 | return grid; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
@Override |
148 | |
public String toString() { |
149 | |
|
150 | 19 | if (swapWith != null) { |
151 | 0 | return swapWith.toString(); |
152 | |
} |
153 | |
|
154 | 19 | setAttribute("class", StyleClasses.Grid.toString()); |
155 | |
|
156 | 19 | StringBuilder sb = new StringBuilder(super.toString()); |
157 | 19 | String lf = System.getProperty("line.separator"); |
158 | |
|
159 | 19 | sb.append(lf); |
160 | |
|
161 | 19 | Iterator it = rows.iterator(); |
162 | 66 | while (it.hasNext()) { |
163 | 47 | sb.append(it.next().toString()).append(lf); |
164 | |
} |
165 | 19 | sb.append("</").append(tag).append(">").append(lf); |
166 | 19 | return sb.toString(); |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
GridRow rows(int row) { |
174 | 16 | return rows.get(row); |
175 | |
} |
176 | |
} |