BrightSide Workbench Full Report + Source Code
CategoryItem.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.dossier.dossier;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import org.turro.dossier.db.DossierPU;
23 import org.turro.dossier.entity.Category;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.i18n.I_;
27 import org.turro.jpa.Dao;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zk.ui.ext.AfterCompose;
32 import org.zkoss.zul.Treechildren;
33 import org.zkoss.zul.Treeitem;
34 
39 public class CategoryItem extends Treeitem implements AfterCompose {
40 
41  private Treechildren children;
42  private boolean loadOnDemand;
43 
44  public CategoryItem(Category category, boolean loadOnDemand) {
45  super();
46  this.loadOnDemand = loadOnDemand;
47  setValue(category);
48  setLabel(category == null ? I_.get("All") : category.getDescription());
49  setTooltiptext(getLabel());
50  }
51 
52  @Override
53  public CategoryTree getTree() {
54  return (CategoryTree) super.getTree();
55  }
56 
57  public Category getCategory() {
58  return (Category) getValue();
59  }
60 
61  public void showContents() {
62  fillCategory();
63  setOpen(true);
64  }
65 
66  public void reloadContents() {
67  if(children != null) {
68  children.getChildren().clear();
69  showContents();
70  }
71  }
72 
73  public boolean isLeaf() {
74  return getCategoryChildren().isEmpty();
75  }
76 
77  private void initLoadOnDemand() {
78  if(!loadOnDemand) return;
79 
80  setOpen(false);
81 
82  addEventListener(Events.ON_OPEN, new EventListener() {
83  @Override
84  public void onEvent(Event event) throws Exception {
85  // load files and add folders
86  if(isOpen()) {
87  fillCategory();
88  }
89  }
90  });
91  }
92 
93  public CategoryItem addCategory(Category category, boolean loadOnDemand) {
94  CategoryItem cat = new CategoryItem(category, loadOnDemand);
95  children.appendChild(cat);
96  cat.afterCompose();
97  return cat;
98  }
99 
100  private void fillCategory() {
101  if(children != null && children.getChildren().isEmpty()) {
102  for(Category cat : getCategoryChildren()) {
103  addCategory(cat, loadOnDemand);
104  }
105  }
106  }
107 
108  private void addChildrenSpace() {
109  if(!isLeaf()) {
110  children = new Treechildren();
111  appendChild(children);
112  if(!loadOnDemand) {
113  fillCategory();
114  setOpen(true);
115  }
116  }
117  }
118 
119  public boolean isLoadOnDemand() {
120  return loadOnDemand;
121  }
122 
123  public void setLoadOnDemand(boolean loadOnDemand) {
124  this.loadOnDemand = loadOnDemand;
125  }
126 
127  public Collection<Category> getCategoryChildren() {
128  boolean all = Application.getApplication().isInRole("dossier:all");
129  Dao dao = new DossierPU();
130  WhereClause wc = new WhereClause();
131  wc.addClause("select category from Category as category");
132  if(getCategory() == null) {
133  wc.addClause("where parent is null");
134  } else {
135  wc.addClause("where parent = :category");
136  wc.addNamedValue("category", getCategory());
137  }
138 
139  wc.addClause("order by category.description");
140  ArrayList<Category> list = new ArrayList<Category>();
141  for(Object o : dao.getResultList(wc)) {
142  if(all || new CategoryWrapper((Category) (o)).isChildrenParticipant()) {
143  list.add((Category) o);
144  }
145  }
146  return list;
147  }
148 
149  @Override
150  public void afterCompose() {
151  initLoadOnDemand();
152  addChildrenSpace();
153  }
154 
155 }
CategoryItem addCategory(Category category, boolean loadOnDemand)
Collection< Category > getCategoryChildren()
CategoryItem(Category category, boolean loadOnDemand)
void setLoadOnDemand(boolean loadOnDemand)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41