BrightSide Workbench Full Report + Source Code
InputEnum.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.dialog;
19 
20 import java.util.Arrays;
21 import org.turro.i18n.I_;
22 import org.zkoss.zk.ui.ext.AfterCompose;
23 import org.zkoss.zul.Radio;
24 import org.zkoss.zul.Radiogroup;
25 
30 public class InputEnum<V extends Enum<V>> extends Radiogroup implements AfterCompose {
31 
32  private V[] values;
33  private boolean populated;
34  private V internalValue;
35 
36  public InputEnum(V[] values) {
37  this.values = values;
38  }
39 
40  public V getObjectValue() {
41  if(!populated) {
42  return internalValue;
43  } else {
44  int i = getSelectedIndex();
45  return i > -1 ? values[i] : null;
46  }
47  }
48 
49  public void setObjectValue(V objectValue) {
50  if(!populated) {
51  this.internalValue = objectValue;
52  } else {
53  setSelectedIndex(Arrays.binarySearch(values, objectValue));
54  }
55  }
56 
57  protected void populateList() {
58  if(values == null) return;
59  for(V v : values) {
60  Radio r = new Radio(getString(v), getImage(v));
61  appendChild(r);
62  }
63  }
64 
65  protected String getString(V v) {
66  return I_.byKey(v.toString());
67  }
68 
69  protected String getImage(V v) {
70  return null;
71  }
72 
73  @Override
74  public void afterCompose() {
75  populateList();
76  populated = true;
77  if(internalValue != null) {
78  setObjectValue(internalValue);
79  internalValue = null;
80  }
81  }
82 
83 }
static String byKey(String key)
Definition: I_.java:83