BrightSide Workbench Full Report + Source Code
HierarchyGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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 
19 package org.turro.hierarchy;
20 
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.TreeMap;
25 import org.turro.zkoss.grid.PagingGrid;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.Hlayout;
30 import org.zkoss.zul.Row;
31 import org.zkoss.zul.Rows;
32 import org.zkoss.zul.Space;
33 import org.zkoss.zul.Toolbarbutton;
34 
39 public abstract class HierarchyGrid extends PagingGrid {
40 
41  private Collection<Hierarchical> items;
42  private Map<Hierarchical, Boolean> breakdown;
43  private int showBreakdownLevel;
44 
45  public void setCollection(Collection<Hierarchical> items, int showBreakdownLevel) {
46  this.showBreakdownLevel = showBreakdownLevel;
47  this.items = items;
48  addRows();
49  }
50 
51  protected void addRows() {
52  Rows rows = getRows(true);
53  rows.getChildren().clear();
54 
55  breakdown = new TreeMap<Hierarchical, Boolean>();
56 
57  for(Hierarchical value : items) {
58  processValue(value);
59  }
60 
61  refreshRows();
62 
63  setRowCount(rows.getChildren().size());
64  }
65 
66  private void processValue(Hierarchical value) {
67  Row row = new Row();
68  row.setValue(value);
69 
70  getRows().appendChild(row);
71 
72  breakdown.put(value, showBreakdownLevel <= 0 || getLevel(value) < showBreakdownLevel);
73 
74  if(value.children() != null) {
75  for(Iterator it = value.children().iterator(); it.hasNext();) {
76  Object obj = it.next();
77  if(obj instanceof Collection) {
78  for(Object o : (Collection) obj) {
79  processValue((Hierarchical) o);
80  }
81  } else {
82  processValue((Hierarchical) obj);
83  }
84  }
85  }
86 
87  }
88 
89  protected void refreshRows() {
90  for(Object row : getRows().getChildren()) {
91  if(row instanceof Row) {
92  if(((Row) row).getValue() instanceof Hierarchical) {
93  ((Row) row).setVisible(isVisible((Hierarchical) ((Row) row).getValue()));
94  ((Row) row).getChildren().clear();
95  refreshValue((Row) row, (Hierarchical) ((Row) row).getValue());
96  }
97  }
98  }
99  }
100 
101  protected Hlayout getNameLayout(final Hierarchical value) {
102  Hlayout hbox = new Hlayout();
103  Space s = new Space();
104  s.setWidth(getLevel(value) * 15 + "px");
105  hbox.appendChild(s);
106 
107  if(value.children() != null && !value.children().isEmpty()) {
108  Toolbarbutton bd = new Toolbarbutton();
109  bd.setImage(getBreakdownImage(value));
110  bd.addEventListener(Events.ON_CLICK, new EventListener() {
111  @Override
112  public void onEvent(Event event) throws Exception {
113  changeBreakdownState(value);
114  refreshRows();
115  }
116  });
117  hbox.appendChild(bd);
118  }
119 
120  return hbox;
121  }
122 
123  protected abstract void refreshValue(Row row, Hierarchical value);
124 
125  protected String getBreakdownImage(Hierarchical value) {
126  if(isShowBreakdown(value)) {
127  return "/_zul/images/breakdown-open.png";
128  } else {
129  return "/_zul/images/breakdown-closed.png";
130  }
131  }
132 
133  protected void changeBreakdownState(Hierarchical value) {
134  breakdown.put(value, !isShowBreakdown(value));
135  }
136 
137  protected boolean isShowBreakdown(Hierarchical value) {
138  return breakdown.get(value);
139  }
140 
141  protected boolean isVisible(Hierarchical value) {
142  Hierarchical tmp = (Hierarchical) value.parent();
143  while(tmp != null) {
144  if(!isShowBreakdown(tmp)) {
145  return false;
146  }
147  tmp = (Hierarchical) tmp.parent();
148  }
149  return true;
150  }
151 
152  private int getLevel(Hierarchical value) {
153  int count = 0;
154  while((value = (Hierarchical) value.parent()) != null) {
155  count++;
156  }
157  return count;
158  }
159 
160 }
boolean isShowBreakdown(Hierarchical value)
abstract void refreshValue(Row row, Hierarchical value)
void changeBreakdownState(Hierarchical value)
Hlayout getNameLayout(final Hierarchical value)
boolean isVisible(Hierarchical value)
String getBreakdownImage(Hierarchical value)
void setCollection(Collection< Hierarchical > items, int showBreakdownLevel)
Rows getRows(boolean create)