BrightSide Workbench Full Report + Source Code
LabelEditable.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.label;
19 
20 import java.util.Collection;
21 import org.zkoss.zk.ui.event.Event;
22 import org.zkoss.zk.ui.event.EventListener;
23 import org.zkoss.zk.ui.event.Events;
24 import org.zkoss.zk.ui.ext.AfterCompose;
25 import org.zkoss.zul.*;
26 import org.zkoss.zul.impl.InputElement;
27 
32 public class LabelEditable extends Hbox implements AfterCompose {
33 
34  private String label, image, oldLabel;
35  private boolean editing = false, editable = true;
36  private int editorCols;
37  private Collection<String> values;
38 
39  public LabelEditable(String label, String image) {
40  this.label = label;
41  this.image = image;
42  }
43 
44  public LabelEditable(String label) {
45  this.label = label;
46  }
47 
48  public LabelEditable() {
49  }
50 
51  public boolean isEditable() {
52  return editable;
53  }
54 
55  public void setEditable(boolean editable) {
56  this.editable = editable;
57  }
58 
59  public boolean isEditing() {
60  return editing;
61  }
62 
63  public void setEditing(boolean editing) {
64  this.editing = editing;
65  }
66 
67  public int getEditorCols() {
68  return editorCols;
69  }
70 
71  public void setEditorCols(int editorCols) {
72  this.editorCols = editorCols;
73  }
74 
75  public String getImage() {
76  return image;
77  }
78 
79  public void setImage(String image) {
80  this.image = image;
81  }
82 
83  public String getLabel() {
84  return label;
85  }
86 
87  public void setLabel(String label) {
88  this.label = label;
89  }
90 
91  public String getOldLabel() {
92  return oldLabel;
93  }
94 
95  public Collection<String> getValues() {
96  return values;
97  }
98 
99  public void setValues(Collection<String> values) {
100  this.values = values;
101  }
102 
103  @Override
104  public void afterCompose() {
105  createComponent();
106  }
107 
108  private void createComponent() {
109  getChildren().clear();
110  if(!editable) {
111  Label l = new Label(label);
112  l.setSclass("softLabel");
113  appendChild(l);
114  } else {
115  if(!editing) {
116  Toolbarbutton tb = new Toolbarbutton(label, image);
117  tb.setSclass("softLabel");
118  tb.addEventListener(Events.ON_CLICK, new EventListener() {
119  @Override
120  public void onEvent(Event event) throws Exception {
121  editing = true;
122  createComponent();
123  Events.postEvent(new Event(Events.ON_CHANGING, LabelEditable.this, LabelEditable.this));
124  }
125  });
126  appendChild(tb);
127  } else {
128  final InputElement tb;
129  if(values != null && !values.isEmpty()) {
130  tb = new Combobox(label);
131  ((Combobox) tb).setModel(new ListModelList(values));
132  } else {
133  tb = new Textbox(label);
134  }
135  if(editorCols > 0) {
136  tb.setCols(editorCols);
137  } else if(editorCols == -1) {
138  tb.setHflex("1");
139  }
140  tb.setSclass("softLabel");
141  tb.addEventListener(Events.ON_OK, new EventListener() {
142  @Override
143  public void onEvent(Event event) throws Exception {
144  oldLabel = label;
145  label = tb.getText();
146  editing = false;
147  createComponent();
148  Events.postEvent(new Event(Events.ON_CHANGE, LabelEditable.this, LabelEditable.this));
149  }
150  });
151  tb.addEventListener(Events.ON_CANCEL, new EventListener() {
152  @Override
153  public void onEvent(Event event) throws Exception {
154  editing = false;
155  createComponent();
156  }
157  });
158  tb.addEventListener(Events.ON_BLUR, new EventListener() {
159  @Override
160  public void onEvent(Event event) throws Exception {
161  editing = false;
162  createComponent();
163  }
164  });
165  appendChild(tb);
166  tb.select();
167  tb.setFocus(true);
168  }
169  }
170  }
171 
172 }
LabelEditable(String label, String image)
Collection< String > getValues()
void setValues(Collection< String > values)
void setEditable(boolean editable)