1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.extremecomponents.table.model;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.beanutils.MethodUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.extremecomponents.table.bean.Column;
29 import org.extremecomponents.table.cell.Cell;
30
31
32
33 /***
34 * @author jeff johnston
35 */
36 public class ProcessGroupColumn implements ProcessColumn
37 {
38 private static Log logger = LogFactory.getLog(ProcessGroupColumn.class);
39
40 private List groups = new ArrayList();
41 private Map lastColumnValue = new HashMap();
42
43 public void beforeFirstRow(TableModel model, String method, List body)
44 {
45 setGroups(model);
46 }
47
48 public void afterLastRow(TableModel model, String method, List body){}
49
50 public String process(TableModel model, Column column, Cell cell, String method)
51 {
52 try
53 {
54 String value = (String)MethodUtils.invokeMethod(cell, method, null);
55 if (column.canGroup())
56 {
57 value = getGroupHtml(column, cell, value);
58 }
59 return value;
60 }
61 catch (Exception e)
62 {
63 logger.debug("Could not invoke the cell method.", e);
64 }
65
66 return null;
67 }
68
69 /***
70 * If this column is grouped then show the only the <td></td> tags.
71 *
72 * TODO: make this work with pdf documents
73 */
74 public String getGroupHtml(Column column, Cell cell, String html)
75 {
76 String value = (String) lastColumnValue.get(column.getProperty());
77
78 String innerHtml = getInnerHtml(html);
79
80 if (StringUtils.isBlank(value))
81 {
82 lastColumnValue.put(column.getProperty(), innerHtml);
83
84 return html;
85 }
86
87 if (!value.equals(innerHtml))
88 {
89 boolean found = false;
90
91 for (Iterator iter = groups.iterator(); iter.hasNext();)
92 {
93 String property = (String) iter.next();
94
95 if (found)
96 {
97 lastColumnValue.remove(property);
98
99 continue;
100 }
101
102 if (property.equals(column.getProperty()))
103 {
104 lastColumnValue.put(column.getProperty(), innerHtml);
105 found = true;
106 }
107 }
108
109 return html;
110 }
111
112 return getOuterHtml(html);
113 }
114
115 /***
116 * Find out if we need to group any of the properties.
117 */
118 public void setGroups(TableModel model)
119 {
120 Iterator iterator = model.getColumnMetaData().getColumns();
121
122 for (Iterator iter = iterator; iter.hasNext();)
123 {
124 Column column = (Column) iter.next();
125
126 if (column.canGroup())
127 {
128 groups.add(column.getProperty());
129 }
130 }
131 }
132
133 /***
134 * Get the html inside the <td></td> tags.
135 */
136 public String getInnerHtml(String html)
137 {
138 String tmp = html.toLowerCase();
139 int tdClose = tmp.indexOf(">");
140 int tdEnd = tmp.lastIndexOf("</td>");
141
142 return tmp.substring(tdClose + 1, tdEnd);
143 }
144
145 /***
146 * Get the the <td></td> tags, excluding the html inside.
147 */
148 public String getOuterHtml(String html)
149 {
150 String tmp = html.toLowerCase();
151 int tdClose = tmp.indexOf(">");
152 int tdEnd = tmp.lastIndexOf("</td>");
153
154 return tmp.substring(0, tdClose) + "</td>";
155 }
156 }