BrightSide Workbench Full Report + Source Code
LabelExtended.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.Date;
21 import org.turro.elephant.util.DateFormats;
22 import org.turro.elephant.util.DecimalFormats;
23 import org.turro.i18n.I_;
24 import org.zkoss.zul.Label;
25 
30 public class LabelExtended extends Label {
31 
32  private boolean showTooltip = false;
33  private int maxChars = 0, wordWrap = 0;
34  private String format;
35 
36 // @Override
37 // public String getEncodedText() {
38 // String value = super.getValue();
39 // setValue(Application.getString(value));
40 // return super.getEncodedText();
41 // }
42 
43  public LabelExtended setFormat(String format) {
44  this.format = format;
45  return this;
46  }
47 
48  public int getMaxChars() {
49  return maxChars;
50  }
51 
52  public LabelExtended setCSS(String style) {
53  setStyle(style);
54  return this;
55  }
56 
57  public LabelExtended setMaxChars(int maxChars) {
58  this.maxChars = maxChars;
59  return this;
60  }
61 
62  public LabelExtended setString(String value) {
63  setValue(value);
64  return this;
65  }
66 
67  public LabelExtended setResourceValue(String resourceValue) {
68  if(resourceValue.startsWith("#")) {
69  // LabelBundle legacy
70  setValue(I_.get(resourceValue.substring(1)));
71  } else {
72  setValue(I_.get(resourceValue));
73  }
74  return this;
75  }
76 
77  public LabelExtended setEnum(Enum e) {
78  if(e != null) {
79  setValue(I_.byKey(e.toString()));
80  }
81  return this;
82  }
83 
84  public LabelExtended setDate(Date date) {
85  if(date != null) {
86  setValue(DateFormats.format(date, true));
87  }
88  return this;
89  }
90 
91  public LabelExtended setDateTime(Date date) {
92  if(date != null) {
93  setValue(DateFormats.format(date, false));
94  }
95  return this;
96  }
97 
98  public LabelExtended setBoolean(boolean value) {
99  setBoolean(Boolean.toString(value));
100  return this;
101  }
102 
103  public LabelExtended setBoolean(String value) {
104  setValue(I_.get(value));
105  return this;
106  }
107 
108  public LabelExtended setDouble(double value) {
109  setValue(format == null ? DecimalFormats.format(value) :
110  DecimalFormats.format(value, format));
111  return this;
112  }
113 
114  public LabelExtended setLong(long value) {
115  setValue(format == null ? DecimalFormats.format(value) :
116  DecimalFormats.format(value, format));
117  return this;
118  }
119 
120  public boolean isShowTooltip() {
121  return showTooltip;
122  }
123 
124  public LabelExtended setShowTooltip(boolean showTooltip) {
125  this.showTooltip = showTooltip;
126  return this;
127  }
128 
129  public int getWordWrap() {
130  return wordWrap;
131  }
132 
133  public LabelExtended setWordWrap(int wordWrap) {
134  this.wordWrap = wordWrap;
135  return this;
136  }
137 
138  @Override
139  public void setValue(String value) {
140  if(value != null) {
141  if(maxChars > 0 && value.length() > maxChars) {
142  if(showTooltip) {
143  setTooltiptext(value);
144  }
145  value = value.substring(0, lastIndexOfWhiteChar(value, maxChars)) + "...";
146  }
147  if(wordWrap > 0) {
148  if(isPre()) {
149  value = insertLF(value, wordWrap);
150  }
151  }
152  }
153  super.setValue(value);
154  }
155 
156  private String insertLF(String s, int from) {
157  StringBuffer sb = new StringBuffer(s);
158  int p = 0, c = 0;
159  while(p < sb.length() - 1) {
160  p++; c++;
161  if(c > from && sb.charAt(p) == ' ') {
162  sb.setCharAt(p, '\n');
163  c = 0;
164  }
165  else if(sb.charAt(p) == '\n')
166  c = 0;
167  }
168  return sb.toString();
169  }
170 
171  private int lastIndexOfWhiteChar(String value, int maxChars) {
172  int maxLast = 0, last;
173  last = value.lastIndexOf(' ', maxChars);
174  if(last > maxLast) maxLast = last;
175  last = value.lastIndexOf('\n', maxChars);
176  if(last > maxLast) maxLast = last;
177  last = value.lastIndexOf('.', maxChars);
178  if(last > maxLast) maxLast = last;
179  last = value.lastIndexOf(',', maxChars);
180  if(last > maxLast) maxLast = last;
181  return maxLast;
182  }
183 
184 }
static final String format(Date d, boolean dateOnly)
static String format(Number value, String pattern)
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
LabelExtended setFormat(String format)
LabelExtended setBoolean(String value)
LabelExtended setBoolean(boolean value)
LabelExtended setDateTime(Date date)
LabelExtended setWordWrap(int wordWrap)
LabelExtended setCSS(String style)
LabelExtended setString(String value)
LabelExtended setDate(Date date)
LabelExtended setDouble(double value)
LabelExtended setShowTooltip(boolean showTooltip)
LabelExtended setLong(long value)
LabelExtended setResourceValue(String resourceValue)
LabelExtended setMaxChars(int maxChars)