BrightSide Workbench Full Report + Source Code
Multilinebox.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.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.util.Messages;
25 import org.turro.i18n.I_;
26 import org.turro.util.Chars;
27 import org.turro.zkoss.dialog.InputDialog;
28 import org.turro.zkoss.dialog.Listeners;
29 import org.zkoss.zk.ui.WrongValueException;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zk.ui.ext.AfterCompose;
34 import org.zkoss.zul.Textbox;
35 
40 public class Multilinebox extends Textbox implements AfterCompose, EventListener {
41 
42  private int scale;
43 
44  public Multilinebox(String value, int scale) throws WrongValueException {
45  super(value);
46  this.scale = scale;
47  }
48 
49  public Multilinebox() {
50  }
51 
52  @Override
53  public void setValue(String value) throws WrongValueException {
54  if(value != null && value.contains("\n")) {
55  super.setValue(formatValue(value));
56  } else {
57  super.setValue(value);
58  }
59  }
60 
61  @Override
62  public String getValue() throws WrongValueException {
63  String tmp = super.getValue();
64  return tmp == null ? null : tmp.replaceAll(Chars.nl().regexp().toString(), "\n");
65  }
66 
67  public static String formatValue(String value) {
68  return value == null ? null : ((String) value).replaceAll("\\n", Chars.nl().toString());
69  }
70 
71  @Override
72  public void afterCompose() {
73  addEventListener(Events.ON_CLICK, this);
74  }
75 
76  @Override
77  public void onEvent(Event event) throws Exception {
78  if(isReadonly()) {
79  Collection c = Listeners.cancelListener(this, Events.ON_BLUR);
80  Messages.info(I_.get("Reference")).add(this.getValue()).show();
81  Listeners.activateListeners(this, Events.ON_BLUR, c);
82  } else {
83  final Collection c = Listeners.cancelListener(this, Events.ON_BLUR);
84  InputDialog.getInput(getPage(), I_.get("Reference"), "Description",
85  this.getValue(), null, scale, new Command() {
86  @Override
87  public Object execute(Context context) {
88  String value = (String) context.get("value");
89  if(value != null) {
90  Multilinebox.this.setValue(value);
91  }
92  return null;
93  }
94  }, new Command() {
95  @Override
96  public Object execute(Context context) {
97  Listeners.activateListeners(Multilinebox.this, Events.ON_BLUR, c);
98  return null;
99  }
100  });
101  }
102  }
103 
104 }
Messages add(String word)
Definition: Messages.java:50
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static void activateListeners(Component component, String eventType, Collection< EventListener > listeners)
Definition: Listeners.java:42
static Collection< EventListener > cancelListener(Component component, String eventType)
Definition: Listeners.java:32
Multilinebox(String value, int scale)
static String formatValue(String value)