BrightSide Workbench Full Report + Source Code
GenericRadiogroup.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.zkoss.input;
20 
21 import java.util.Collection;
22 import org.turro.i18n.I_;
23 import org.zkoss.zk.ui.Component;
24 import org.zkoss.zk.ui.event.Event;
25 import org.zkoss.zk.ui.event.EventListener;
26 import org.zkoss.zk.ui.event.Events;
27 import org.zkoss.zk.ui.ext.AfterCompose;
28 import org.zkoss.zul.Div;
29 import org.zkoss.zul.Hlayout;
30 import org.zkoss.zul.Layout;
31 import org.zkoss.zul.Radio;
32 import org.zkoss.zul.Radiogroup;
33 import org.zkoss.zul.Vlayout;
34 
39 public abstract class GenericRadiogroup<E> extends Div implements AfterCompose, EventListener<Event> {
40 
41  private String orient = "horizontal";
42  private Collection<E> collection;
43  private E _internal;
44  private boolean disabled = false, populated = false, allowNone = false;
45  private Layout layout;
46  private Radiogroup radioGroup = new Radiogroup();
47  private Radio clear;
48 
49  public boolean isDisabled() {
50  return disabled;
51  }
52 
53  public void setDisabled(boolean value) {
54  disabled = value;
55  for(Radio r : radioGroup.getItems()) {
56  r.setDisabled(value);
57  }
58  if(clear != null) {
59  clear.setDisabled(value);
60  }
61  }
62 
63  public boolean isAllowNone() {
64  return allowNone;
65  }
66 
67  public void setAllowNone(boolean allowNone) {
68  this.allowNone = allowNone;
69  clearImage();
70  }
71 
72  public Collection<E> getCollection() {
73  return collection;
74  }
75 
76  public void setCollection(Collection<E> collection) {
77  this.collection = collection;
78  }
79 
80  public E getObjectValue() {
81  if(populated) {
82  Radio r = radioGroup.getSelectedItem();
83  if(r != null) {
84  for(E e : collection) {
85  if(r.getLabel().equals(convertToString(e))) {
86  return e;
87  }
88  }
89  }
90  return null;
91  } else {
92  return _internal;
93  }
94  }
95 
96  public void setObjectValue(E v) {
97  _internal = v;
98  if(populated && _internal != null) {
99  for(Component c : layout.getChildren()) {
100  if(c instanceof Radio) {
101  Radio r = (Radio) c;
102  if(r.getLabel().equals(convertToString(_internal))) {
103  r.setSelected(true);
104  return;
105  }
106  }
107  }
108  }
109  radioGroup.setSelectedItem(null);
110  }
111 
112  public String getOrient() {
113  return orient;
114  }
115 
116  public void setOrient(String orient) {
117  this.orient = orient;
118  }
119 
120  @Override
121  public void afterCompose() {
122  if(!collection.isEmpty()) {
123  if("vertical".equals(orient)) {
124  layout = new Vlayout();
125  } else {
126  layout = new Hlayout();
127  }
128  appendChild(radioGroup);
129  appendChild(layout);
130  for(E e : collection) {
131  Radio r = new Radio(convertToString(e));
132  r.setRadiogroup(radioGroup);
133  layout.appendChild(r);
134  }
135  }
136  setObjectValue(_internal);
137  populated = true;
138  clearImage();
139  radioGroup.addEventListener(Events.ON_CHECK, this);
140  }
141 
142  @Override
143  public void onEvent(Event event) throws Exception {
144  Events.postEvent(this, event);
145  }
146 
147  protected abstract String convertToString(E v);
148 
149  private void clearImage() {
150  if(populated && allowNone && clear == null) {
151  clear = new Radio(I_.get("Clear"));
152  clear.addEventListener(Events.ON_CHECK, new EventListener<Event>() {
153  @Override
154  public void onEvent(Event event) throws Exception {
155  if(!disabled) {
156  radioGroup.setSelectedItem(null);
157  }
158  clear.setSelected(false);
159  Events.postEvent(GenericRadiogroup.this, event);
160  }
161  });
162  layout.appendChild(clear);
163  }
164  }
165 
166 }
static String get(String msg)
Definition: I_.java:41
void setCollection(Collection< E > collection)