BrightSide Workbench Full Report + Source Code
ContextItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.elephant.web;
20 
21 import org.zkoss.zk.ui.event.Event;
22 import org.zkoss.zk.ui.event.Events;
23 import org.zkoss.zul.Treecell;
24 import org.zkoss.zul.Treechildren;
25 import org.zkoss.zul.Treeitem;
26 import org.zkoss.zul.Treerow;
27 
32 public class ContextItem extends Treeitem {
33 
34  private final ElContext context;
35  private boolean loadOnDemand;
36  private Treechildren children;
37 
38  public ContextItem(ElContext context, boolean loadOnDemand) {
39  this.context = context;
40  this.loadOnDemand = loadOnDemand;
41  initLoadOnDemand();
42  addCells();
43  if(!context.getStaticChildren().isEmpty()) addChildrenSpace();
44  }
45 
46  @Override
47  public ContextTree getTree() {
48  return (ContextTree) super.getTree();
49  }
50 
52  return context;
53  }
54 
55  public ContextItem addContext(ElContext context, boolean loadOnDemand) {
56  ContextItem ci = new ContextItem(context, loadOnDemand);
57  children.appendChild(ci);
58  return ci;
59  }
60 
61  public void showContents() {
62  if(children != null) {
63  fillFolder();
64  setOpen(true);
65  }
66  }
67 
68  public void reloadContents() {
69  if(children != null) {
70  children.getChildren().clear();
71  showContents();
72  }
73  }
74 
75  private void addChildrenSpace() {
76  children = new Treechildren();
77  appendChild(children);
78  if(!loadOnDemand) {
79  fillFolder();
80  setOpen(loadOnDemand);
81  }
82  }
83 
84  private void addCells() {
85  Treerow row = new Treerow();
86  appendChild(row);
87  Treecell cell = new Treecell(context.getId());
88  row.appendChild(cell);
89  setImage("/_zul/images/www_context.png");
90  }
91 
92  public boolean isLoadOnDemand() {
93  return loadOnDemand;
94  }
95 
96  public void setLoadOnDemand(boolean loadOnDemand) {
97  this.loadOnDemand = loadOnDemand;
98  }
99 
100  private void initLoadOnDemand() {
101  if(!loadOnDemand) return;
102 
103  setOpen(false);
104 
105  addEventListener(Events.ON_OPEN, (Event event) -> {
106  if(isOpen()) {
107  fillFolder();
108  }
109  });
110  }
111 
112  private void fillFolder() {
113  if(children != null && children.getChildren().isEmpty()) {
114  for(ElContext ctx : context.getStaticChildren()) {
115  addContext(ctx, loadOnDemand);
116  }
117  }
118  }
119 
120 }
ContextItem addContext(ElContext context, boolean loadOnDemand)
ContextItem(ElContext context, boolean loadOnDemand)
void setLoadOnDemand(boolean loadOnDemand)
TreeSet< ElContext > getStaticChildren()
Definition: ElContext.java:201