BrightSide Workbench Full Report + Source Code
zul/dossier/CategoryTree.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.zul.dossier;
19 
20 import java.util.Collection;
21 import java.util.List;
22 import org.turro.dossier.dossier.CategoryWrapper;
23 import org.turro.dossier.entity.Category;
24 import org.turro.dossier.entity.CategoryParticipant;
25 import org.turro.dossier.search.CategoryResults;
26 import org.turro.dossier.zul.menu.DossierMenu;
27 import org.turro.elephant.util.Messages;
28 import org.turro.i18n.I_;
29 import org.turro.zul.frame.Framework;
30 import org.zkoss.zk.ui.Component;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zul.*;
36 
41 public class CategoryTree extends Div implements AfterCompose {
42 
43  @Override
44  public void afterCompose() {
45  populateTree();
46  }
47 
48  private void populateTree() {
49  List<Category> children = new CategoryResults().getCategoryRootList();
50  if(!children.isEmpty()) {
51  Box cbox = null;
52  if(children.size() > 3) {
53  cbox = new Vbox();
54  } else {
55  cbox = new Hbox();
56  }
57  appendChild(cbox);
58  createNodes(cbox, children);
59  }
60  }
61 
62  private void createNodes(Box box, Collection<Category> list) {
63  box.setAlign("center");
64  box.setStyle("padding:10px;");
65  for(final Category cat : list) {
66  Groupbox gb = new Groupbox();
67  box.appendChild(gb);
68  gb.setMold("3d");
69  gb.appendChild(new Caption("#" + cat.getId() + " " + cat.getDescription()));
70  Vbox vbox = new Vbox();
71  vbox.setAlign("center");
72  vbox.setSclass("participant");
73  gb.appendChild(vbox);
74  for(CategoryParticipant cp : cat.getParticipants()) {
75  Hbox hbox = new Hbox();
76  vbox.appendChild(hbox);
77  hbox.appendChild(new Label(cp.getName()));
78  hbox.appendChild(new Label("[" + I_.byKey(cp.getRole().toString()) + "]"));
79  }
80  vbox.appendChild(new Separator());
81  Button edit = new Button(I_.get("Edit"), "/_zul/images/edit.png");
82  edit.addEventListener(Events.ON_CLICK, new EventListener() {
83  @Override
84  public void onEvent(Event event) throws Exception {
85  DossierMenu.showCategory(cat.getId());
86  Framework.getCurrent().invalidateSelected();
87  }
88  });
89  if(new CategoryWrapper(cat).canDelete()) {
90  Button delete = new Button(I_.get("Delete"), "/_zul/images/edit-delete.png");
91  delete.addEventListener(Events.ON_CLICK, new EventListener() {
92  @Override
93  public void onEvent(Event event) throws Exception {
94  Messages.confirmDeletion().show(() -> {
95  new CategoryWrapper(cat).delete();
96  Framework.getCurrent().invalidateSelected();
97  });
98  }
99  });
100  vbox.appendChild(new Hbox(new Component[] {edit, delete}));
101  } else {
102  vbox.appendChild(new Hbox(new Component[] {edit}));
103  }
104  vbox.appendChild(new Separator());
105  Collection<Category> children = cat.getChildren();
106  if(!children.isEmpty()) {
107  Box cbox = null;
108  if(children.size() > 3) {
109  cbox = new Vbox();
110  } else {
111  cbox = new Hbox();
112  }
113  vbox.appendChild(cbox);
114  createNodes(cbox, children);
115  }
116  }
117  }
118 
119 }