BrightSide Workbench Full Report + Source Code
GenericTree.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.tree;
19 
20 import java.util.Collection;
21 import org.zkoss.zk.ui.event.Event;
22 import org.zkoss.zk.ui.event.Events;
23 import org.zkoss.zk.ui.ext.AfterCompose;
24 import org.zkoss.zul.Tree;
25 import org.zkoss.zul.Treechildren;
26 import org.zkoss.zul.Treeitem;
27 
32 public abstract class GenericTree<V> extends Tree implements AfterCompose {
33 
34  private boolean selectFirst = true;
35  private V internalValue;
36 // private Collection<V> internalValues;
37  protected boolean populated = false;
38 
39  @Override
40  public void afterCompose() {
41  populateList();
42  populated = true;
43  if(internalValue != null) {
44  setObjectValue(internalValue);
45  internalValue = null;
46 // } else if (internalValues != null) {
47 // setObjectValues(internalValues);
48 // internalValues = null;
49  } else {
50  setObjectValue(null);
51  }
52  }
53 
54  public void clearItems() {
55  getItems().clear();
56  populated = false;
57  }
58 
59  public void setObjectValue(V value) {
60  if(!populated) {
61  internalValue = value;
62  }
63  if(value == null && selectFirst && getItemCount() > 0) {
64  setSelectedFirstItem();
65  } else {
66  for(Object item : getItems()) {
67  if(item instanceof Treeitem) {
68  if(equals((V) ((Treeitem) item).getValue(), (V) value)) {
69  setSelectedItem((Treeitem) item);
70  }
71  }
72  }
73  }
74  }
75 
76 // public void setObjectValues(Collection<V> collection) {
77 // if(!populated) {
78 // internalValues = collection;
79 // }
80 // setSelectedItems(new ValueTreeitemAdapter<V>(this, collection) {
81 // @Override
82 // protected boolean equals(V value, V obj) {
83 // return GenericTree.this.equals(value, obj);
84 // }
85 // });
86 // }
87 
88  public V getObjectValue() {
89  if(!populated) {
90  return internalValue;
91  }
92  Treeitem li = getSelectedItem();
93  if(li != null && li.getValue() != null) {
94  return (V) li.getValue();
95  }
96  return null;
97  }
98 
99 // public Collection<V> getObjectValues() {
100 // if(!populated) {
101 // return internalValues;
102 // }
103 // return new TreeitemValueAdapter<V>(getSelectedItems(), true);
104 // }
105 
106  public void selectItemByText(String text) {
107  if(text != null && text.length() > 3) {
108  for(Object item : getItems()) {
109  if(item instanceof Treeitem) {
110  String l = ((Treeitem) item).getLabel();
111  if(l.toLowerCase().contains(text.toLowerCase())) {
112  setSelectedItem((Treeitem) item);
113  showItem((Treeitem) item);
114  Events.postEvent(new Event(Events.ON_SELECT, GenericTree.this));
115  }
116  }
117  }
118  }
119  }
120 
121  public void showItem(Treeitem item) {
122  while(item != null) {
123  item.setOpen(true);
124  item = item.getParentItem();
125  }
126  }
127 
128  protected boolean equals(V value, V obj) {
129  return (value == null && obj == null) ||
130  (value != null && value.equals(obj));
131  }
132 
133  protected void addTreeitem(Treeitem parent, Treeitem child) {
134  if(parent.getTreechildren() == null) {
135  parent.appendChild(new Treechildren());
136  }
137  parent.getTreechildren().appendChild(child);
138  }
139 
140  protected abstract void populateList();
141 
142  private void setSelectedFirstItem() {
143  for(Treeitem ti : (Collection<Treeitem>) getItems()) {
144  if(ti.getValue() != null) {
145  setSelectedItem(ti);
146  break;
147  }
148  }
149  }
150 
151 }
void addTreeitem(Treeitem parent, Treeitem child)
void selectItemByText(String text)
boolean equals(V value, V obj)