BrightSide Workbench Full Report + Source Code
PagingGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.grid;
19 
20 import java.io.ByteArrayOutputStream;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.string.Strings;
28 import org.turro.command.Command;
29 import org.turro.command.Context;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.i18n.I_;
32 import org.turro.zkoss.dialog.InputDialog;
33 import org.turro.zkoss.dialog.InputField;
34 import org.zkoss.exporter.pdf.PdfExporter;
35 import org.zkoss.util.media.AMedia;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.HtmlBasedComponent;
38 import org.zkoss.zk.ui.event.Event;
39 import org.zkoss.zk.ui.event.Events;
40 import org.zkoss.zul.*;
41 
46 public class PagingGrid extends Grid {
47 
48  private int maxResults = 30;
49  private String pagingMold = "default", refineValue = null;
50  private String varColumns;
51  private boolean defaultBehavior = true;
52 
53  public void setColumns(int columns) {
54  setSpan(true);
55  Columns cols = new Columns();
56  for(int i = 0; i < columns; i++) {
57  Column col = new Column();
58  col.setHflex("min");
59  cols.appendChild(col);
60  }
61  appendChild(cols);
62  }
63 
64  public void setColumns(String hflexs) {
65  setSpan(true);
66  String hfs[] = hflexs.split(",");
67  Columns cols = new Columns();
68  for(int i = 0; i < hfs.length; i++) {
69  Column col = new Column();
70  if(hfs[i].startsWith("right-")) {
71  col.setAlign("right");
72  col.setHflex(hfs[i].substring(6));
73  } else {
74  col.setHflex(hfs[i]);
75  }
76  cols.appendChild(col);
77  }
78  appendChild(cols);
79  }
80 
81  public void refine() {
83  getPage(),
84  I_.get("Refine search"),
85  new InputField[] {
86  new InputField("Text", refineValue, null, 0)
87  }, new Command() {
88  @Override
89  public Object execute(Context context) {
90  InputField[] fields = (InputField[]) context.get("fields");
91  if(fields.length > 0) {
92  for(InputField f : fields) {
93  if("Text".equals(f.getLabel())) {
94  setRefineValue((String) f.getValue());
95  }
96  }
97  }
98  return null;
99  }
100  });
101  }
102 
103  public boolean isDefaultBehavior() {
104  return defaultBehavior;
105  }
106 
107  public void setDefaultBehavior(boolean defaultBehavior) {
108  this.defaultBehavior = defaultBehavior;
109  }
110 
111  public String getRefineValue() {
112  return refineValue;
113  }
114 
115  public void setRefineValue(String value) {
116  this.refineValue = value;
117  renderAll();
118  doRefine();
119  }
120 
121  public int getMaxResults() {
122  return maxResults;
123  }
124 
125  public void setMaxResults(int maxResults) {
126  this.maxResults = maxResults;
127  }
128 
129  public String getPagingMold() {
130  return pagingMold;
131  }
132 
133  public void setPagingMold(String pagingMold) {
134  this.pagingMold = pagingMold;
135  }
136 
137  public int getRowCount() {
138  return getRows().getChildren().size();
139  }
140 
141  public void setRowCount(int rows) {
142  if(defaultBehavior) {
143  setVflex(true);
144  if(rows > maxResults) {
145  setMold("paging");
146  //setPageSize((int) (maxResults * 0.80));
147  setAutopaging(true);
148  setPagingPosition("bottom");
149  getPagingChild().setMold(pagingMold);
150  } else {
151  setMold("default");
152  setAutopaging(false);
153  }
154  } else {
155  setMold("paging");
156  setPageSize((int) (maxResults * 0.80));
157  setPagingPosition("both");
158  getPagingChild().setMold(pagingMold);
159  }
160  }
161 
162  public void setHeadersVisible(boolean value) {
163  getColumns(true).setVisible(value);
164  }
165 
166  public boolean isHeadersVisible() {
167  return getColumns().isVisible();
168  }
169 
170  public String getVarColumns() {
171  return varColumns;
172  }
173 
174  public void setVarColumns(String varColumns) {
175  this.varColumns = varColumns;
176  }
177 
178  public Collection<String> getVarColumnsList(String defaultCols) {
179  return varColumns == null ?
180  (defaultCols == null ? new ArrayList<String>() :
181  Arrays.asList(defaultCols.split("\\s*\\,\\s*"))) :
182  Arrays.asList(varColumns.split("\\s*\\,\\s*"));
183  }
184 
185  public void addDetailColumn() {
186  addDetailColumn(null);
187  }
188 
189  public void addDetailColumn(HtmlBasedComponent caption) {
190  Column c = new Column();
191  c.setWidth("40px");
192  if(caption != null) {
193  c.appendChild(caption);
194  }
195  getColumns(true).appendChild(c);
196  }
197 
198  public void openDetails() {
199  for(Component row : getRows().getChildren()) {
200  Detail d = ((Row)row).getDetailChild();
201  if(d != null) {
202  d.setOpen(true);
203  Events.postEvent(new Event(Events.ON_OPEN, d));
204  }
205  }
206  }
207 
208  public boolean isEmpty() {
209  return getRows() == null || getRows().getChildren() == null;
210  }
211 
212  public void clearColumns() {
213  getColumns(true).getChildren().clear();
214  }
215 
216  public void clearRows() {
217  getRows(true).getChildren().clear();
218  }
219 
220  public void clearEmptyGroups() {
221  Rows rows = getRows();
222  if(rows != null) {
223  Iterator<Component> it = (Iterator<Component>) rows.getChildren().iterator();
224  while(it.hasNext()) {
225  Row row = (Row) it.next();
226  if(row instanceof Group) {
227  Group group = (Group) row;
228  if(group.getItemCount() == 0) {
229  it.remove();
230  }
231  }
232  }
233  }
234  }
235 
236  public Columns getColumns(boolean create) {
237  Columns cols = super.getColumns();
238  if(create && cols == null) {
239  cols = new Columns();
240  cols.setSizable(true);
241  appendChild(cols);
242  }
243  return cols;
244  }
245 
246  public Rows getRows(boolean create) {
247  Rows rows = getRows();
248  if(create && rows == null) {
249  rows = new Rows();
250  appendChild(rows);
251  }
252  return rows;
253  }
254 
255  @Deprecated
256  public Foot getFoot(boolean create) {
257  Foot foot = getFoot();
258  if(create && foot == null) {
259  foot = new Foot();
260  appendChild(foot);
261  }
262  return foot;
263  }
264 
265  public Foot createFoot() {
266  Foot foot = getFoot();
267  if(foot != null) {
268  foot.detach();
269  }
270  foot = new Foot();
271  appendChild(foot);
272  return foot;
273  }
274 
275  public void exportToPdf() {
276  try {
277  ByteArrayOutputStream out = new ByteArrayOutputStream();
278  PdfExporter exporter = new PdfExporter();
279  exporter.export(this, out);
280  AMedia amedia = new AMedia("GridReport.pdf", "pdf", "application/pdf", out.toByteArray());
281  Filedownload.save(amedia);
282  out.close();
283  } catch (Exception ex) {
284  Logger.getLogger(PagingGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
285  }
286  }
287 
288  private void doRefine() {
289  Rows rows = getRows();
290  if(rows != null) {
291  for(Component row : rows.getChildren()) {
292  if(!(row instanceof Group)) {
293  if(Strings.isBlank(refineValue)) {
294  row.setVisible(true);
295  } else {
296  boolean show = false;
297  show = processChildren(row, show);
298  row.setVisible(show);
299  }
300  }
301  }
302  for(Component row : rows.getChildren()) {
303  if(row instanceof Group) {
304  Group group = (Group) row;
305  group.setVisible(group.getVisibleItemCount() > 0);
306  }
307  }
308  }
309  }
310 
311  private boolean processChildren(Component comp, boolean show) {
312  for (Component c : (Collection<Component>) comp.getChildren()) {
313  if (c instanceof Label) {
314  Label l = (Label) c;
315  if (Strings.findsIgnoreCase(l.getValue(), refineValue)) {
316  show = true;
317  }
318  } else if (c instanceof Toolbarbutton) {
319  Toolbarbutton l = (Toolbarbutton) c;
320  if (Strings.findsIgnoreCase(l.getLabel(), refineValue)) {
321  show = true;
322  }
323  } else if (c instanceof A) {
324  A a = (A) c;
325  if (Strings.findsIgnoreCase(a.getLabel(), refineValue)) {
326  show = true;
327  }
328  } else {
329  show = processChildren(c, show);
330  }
331  if(show) break;
332  }
333  return show;
334  }
335 
336 }
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
void setColumns(int columns)
Definition: PagingGrid.java:53
Foot getFoot(boolean create)
Columns getColumns(boolean create)
void setVarColumns(String varColumns)
void setPagingMold(String pagingMold)
Rows getRows(boolean create)
void setHeadersVisible(boolean value)
void setColumns(String hflexs)
Definition: PagingGrid.java:64
void setMaxResults(int maxResults)
Collection< String > getVarColumnsList(String defaultCols)
void addDetailColumn(HtmlBasedComponent caption)
void setRefineValue(String value)
void setDefaultBehavior(boolean defaultBehavior)