BrightSide Workbench Full Report + Source Code
TextEditable.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.text;
19 
20 import org.turro.i18n.I_;
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.Button;
26 import org.zkoss.zul.Hbox;
27 import org.zkoss.zul.Html;
28 import org.zkoss.zul.Textbox;
29 import org.zkoss.zul.Vbox;
30 
35 public class TextEditable extends Vbox implements AfterCompose {
36 
37  private Textbox editor;
38  private Html html;
39  private String value, caption;
40  private boolean editing = false;
41  private int rows;
42 
43  public String getCaption() {
44  return caption;
45  }
46 
47  public void setCaption(String caption) {
48  this.caption = caption;
49  }
50 
51  public String getValue() {
52  return value;
53  }
54 
55  public void setValue(String value) {
56  this.value = value;
57  }
58 
59  public boolean isEditing() {
60  return editing;
61  }
62 
63  public void setEditing(boolean editing) {
64  this.editing = editing;
65  createComponents();
66  }
67 
68  public int getRows() {
69  return rows;
70  }
71 
72  public void setRows(int rows) {
73  this.rows = rows;
74  }
75 
76  @Override
77  public void afterCompose() {
78  createComponents();
79  }
80 
81  private void createComponents() {
82  setHflex("1");
83  getChildren().clear();
84  if(!editing) {
85  Button b = new Button(I_.get("Edit") + " " + caption, "/_zul/images/edit.png");
86  b.addEventListener(Events.ON_CLICK, new EventListener() {
87  @Override
88  public void onEvent(Event event) throws Exception {
89  setEditing(true);
90  }
91  });
92  appendChild(b);
93  html = new Html(value);
94  appendChild(html);
95  } else {
96  Hbox h = new Hbox();
97  h.setHflex("1");
98  Button b = new Button(I_.get("Save") + " " + caption, "/_zul/images/save.png");
99  b.addEventListener(Events.ON_CLICK, new EventListener() {
100  @Override
101  public void onEvent(Event event) throws Exception {
102  value = editor.getValue();
103  Events.postEvent(new Event(Events.ON_CHANGE, TextEditable.this));
104  setEditing(false);
105  }
106  });
107  h.appendChild(b);
108  b = new Button(I_.get("Cancel"), null);
109  b.addEventListener(Events.ON_CLICK, new EventListener() {
110  @Override
111  public void onEvent(Event event) throws Exception {
112  setEditing(false);
113  }
114  });
115  h.appendChild(b);
116  appendChild(h);
117  editor = new Textbox();
118  editor.setHflex("1");
119  editor.setRows(rows);
120  editor.setValue(value);
121  appendChild(editor);
122  }
123  }
124 
125 }
static String get(String msg)
Definition: I_.java:41
void setCaption(String caption)
void setEditing(boolean editing)