BrightSide Workbench Full Report + Source Code
MultiLabelBox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.string.Strings;
23 import org.turro.command.Command;
24 import org.turro.zkoss.label.LabelEditable;
25 import org.zkoss.zk.ui.event.Event;
26 import org.zkoss.zk.ui.event.EventListener;
27 import org.zkoss.zk.ui.event.Events;
28 import org.zkoss.zk.ui.ext.AfterCompose;
29 import org.zkoss.zul.Hbox;
30 
36 public abstract class MultiLabelBox<E> extends Hbox implements AfterCompose, EventListener {
37 
38  private E entity;
39  private boolean readOnly;
40 
41  public MultiLabelBox() {
42  setSclass("z-valign-middle multiLabel");
43  setAlign("center");
44  }
45 
46  public E getEntity() {
47  return entity;
48  }
49 
50  public void setEntity(E entity) {
51  this.entity = entity;
52  }
53 
54  public boolean isReadOnly() {
55  return readOnly;
56  }
57 
58  public void setReadOnly(boolean readOnly) {
59  this.readOnly = readOnly;
60  }
61 
62  @Override
63  public void afterCompose() {
64  setVisible(checkVisible());
65  getChildren().clear();
66  LabelEditable le;
67  if(getCollection() != null) {
68  for(E label : getCollection()) {
69  le = new LabelEditable(convertToString(label));
70  le.setEditable(!readOnly && checkEditable());
71  le.setEditorCols(12);
73  appendChild(le);
74  le.addEventListener(Events.ON_CHANGE, this);
75  }
76  }
77  if(!readOnly && checkNew()) {
78  le = new LabelEditable("", getNewImage());
79  le.setEditorCols(12);
81  appendChild(le);
82  le.addEventListener(Events.ON_CHANGE, this);
83  }
84  for(Object obj : getChildren()) {
85  if(obj instanceof AfterCompose) {
86  ((AfterCompose) obj).afterCompose();
87  }
88  }
89  }
90 
91  @Override
92  public void onEvent(Event event) throws Exception {
93  if(event.getData() instanceof LabelEditable) {
94  LabelEditable le = (LabelEditable) event.getData();
95  if(!Strings.isBlank(le.getLabel())) {
96  if(!Strings.isBlank(le.getImage())) {
97  addChoice(le.getLabel(), null);
98  Events.postEvent(new Event(Events.ON_CHANGE, MultiLabelBox.this));
99  } else {
100  changeChoice(le.getOldLabel(), le.getLabel(), null);
101  Events.postEvent(new Event(Events.ON_CHANGE, MultiLabelBox.this));
102  }
103  } else {
104  if(!Strings.isBlank(le.getImage())) {
105  // nothing, insertion cancelled
106  } else {
107  if(!readOnly && checkDelete()) {
108  deleteChoice(le.getOldLabel(), null);
109  Events.postEvent(new Event(Events.ON_CHANGE, MultiLabelBox.this));
110  }
111  }
112  }
113  }
114  afterCompose();
115  }
116 
117  protected abstract boolean checkVisible();
118  protected abstract boolean checkNew();
119  protected abstract boolean checkEditable();
120  protected abstract boolean checkDelete();
121  protected abstract String getNewImage();
122  protected abstract Collection<E> getCollection();
123  protected abstract String convertToString(E label);
124  protected abstract Collection<String> getLabelChoices();
125  protected abstract void addChoice(String choice, Command command);
126  protected abstract void changeChoice(String oldChoice, String choice, Command command);
127  protected abstract void deleteChoice(String oldChoice, Command command);
128 
129 }
abstract Collection< E > getCollection()
abstract void addChoice(String choice, Command command)
abstract void changeChoice(String oldChoice, String choice, Command command)
abstract String convertToString(E label)
abstract void deleteChoice(String oldChoice, Command command)
abstract Collection< String > getLabelChoices()
void setReadOnly(boolean readOnly)
void setValues(Collection< String > values)
void setEditable(boolean editable)