1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor.widget;
|
9
|
|
|
10
|
|
import com.tirsen.angkor.RenderContext;
|
11
|
|
import com.tirsen.angkor.Component;
|
12
|
|
|
13
|
|
import java.io.IOException;
|
14
|
|
import java.text.Format;
|
15
|
|
import java.text.MessageFormat;
|
16
|
|
import java.util.ArrayList;
|
17
|
|
import java.util.Iterator;
|
18
|
|
import java.util.List;
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Displays text to the user.
|
22
|
|
*
|
23
|
|
* Has the following functionality:
|
24
|
|
* <li> fetches data from a list of {@link ValueModel}s.
|
25
|
|
* <li> the data is formated according to a {@link Format}.
|
26
|
|
* <li> if the format is not a {@link MessageFormat} only the data from the first model is used.
|
27
|
|
* <li> if the format is {@link MessageFormat} all values are fetched from all models.
|
28
|
|
* <li> if setting the as text it is specified according to the syntax of {@link MessageFormat}.
|
29
|
|
* <li> renders the format in a SPAN-tag with the correct id so it can easily be accessed while testing.
|
30
|
|
*
|
31
|
|
* <br>
|
32
|
|
*
|
33
|
|
* Future functionality:
|
34
|
|
* <li> support for fonts.
|
35
|
|
* <li> support for rendering of png-images with the text according to a specified font
|
36
|
|
* on the server (as a truetype-file or an os-font).
|
37
|
|
* <li> support for cascading style sheets.
|
38
|
|
*
|
39
|
|
* <!-- $Id: TextLabel.java,v 1.6 2002/10/13 19:59:23 tirsen Exp $ -->
|
40
|
|
*
|
41
|
|
* @author $Author: tirsen $
|
42
|
|
* @version $Revision: 1.6 $
|
43
|
|
*/
|
44
|
|
public class TextLabel extends Component
|
45
|
|
{
|
46
|
|
private static final MessageFormat CONSTANT_FORMAT = new MessageFormat("{0}");
|
47
|
|
|
48
|
|
private List models;
|
49
|
|
private Format format = CONSTANT_FORMAT;
|
50
|
|
|
51
|
22
|
public TextLabel()
|
52
|
|
{
|
53
|
|
}
|
54
|
|
|
55
|
1
|
public TextLabel(String label)
|
56
|
|
{
|
57
|
1
|
setLabel(label);
|
58
|
|
}
|
59
|
|
|
60
|
0
|
public TextLabel(ValueModel model)
|
61
|
|
{
|
62
|
0
|
setModel(model);
|
63
|
|
}
|
64
|
|
|
65
|
0
|
public TextLabel(String label, ValueModel model)
|
66
|
|
{
|
67
|
0
|
setLabel(label);
|
68
|
0
|
setModel(model);
|
69
|
|
}
|
70
|
|
|
71
|
5
|
public void setLabel(String label)
|
72
|
|
{
|
73
|
5
|
format = new MessageFormat(label);
|
74
|
|
}
|
75
|
|
|
76
|
|
/**
|
77
|
|
* Sets a single model to fetch data from.
|
78
|
|
*/
|
79
|
16
|
public void setModel(ValueModel model)
|
80
|
|
{
|
81
|
16
|
this.models = new ArrayList(1);
|
82
|
16
|
addModel(model);
|
83
|
|
}
|
84
|
|
|
85
|
|
/**
|
86
|
|
* Adds a model.
|
87
|
|
* If the format is not a {@link MessageFormat} only the first model is used.
|
88
|
|
*/
|
89
|
16
|
public void addModel(ValueModel model)
|
90
|
|
{
|
91
|
0
|
if (models == null) models = new ArrayList(1);
|
92
|
16
|
models.add(model);
|
93
|
|
}
|
94
|
|
|
95
|
|
/**
|
96
|
|
* Gets the text formatted correctly and with the values fetched from the models.
|
97
|
|
*/
|
98
|
11
|
public String getFormattedText()
|
99
|
|
{
|
100
|
11
|
String display;
|
101
|
11
|
if (models != null && !models.isEmpty())
|
102
|
|
{
|
103
|
6
|
if (format instanceof MessageFormat)
|
104
|
|
{
|
105
|
6
|
Object[] values;
|
106
|
|
|
107
|
6
|
values = new Object[models.size()];
|
108
|
6
|
Iterator it = models.iterator();
|
109
|
6
|
for (int i = 0; it.hasNext(); i++)
|
110
|
|
{
|
111
|
6
|
ValueModel model = (ValueModel) it.next();
|
112
|
6
|
values[i] = model.getValue();
|
113
|
|
}
|
114
|
|
|
115
|
6
|
display = format.format(values);
|
116
|
|
}
|
117
|
|
else
|
118
|
|
{
|
119
|
|
// this is not exactly 100% good design, might need some refactoring
|
120
|
|
// if we don't have a message-format format the string with only the first models value
|
121
|
0
|
Object value = ((ValueModel) models.get(0)).getValue();
|
122
|
0
|
display = format.format(value);
|
123
|
|
}
|
124
|
|
}
|
125
|
|
else
|
126
|
|
{
|
127
|
5
|
display = format.format(null);
|
128
|
|
}
|
129
|
|
|
130
|
11
|
return display;
|
131
|
|
}
|
132
|
|
|
133
|
|
/**
|
134
|
|
* Sets a custom format. For example a currency format or a date format.
|
135
|
|
*/
|
136
|
0
|
public void setFormat(Format format)
|
137
|
|
{
|
138
|
0
|
this.format = format;
|
139
|
|
}
|
140
|
|
|
141
|
2
|
public Format getFormat()
|
142
|
|
{
|
143
|
2
|
return format;
|
144
|
|
}
|
145
|
|
|
146
|
0
|
public String toString()
|
147
|
|
{
|
148
|
0
|
return getClass().getName() + "[" + format.toString() + "]";
|
149
|
|
}
|
150
|
|
|
151
|
5
|
public void render(RenderContext context) throws IOException
|
152
|
|
{
|
153
|
5
|
if (isVisible())
|
154
|
|
{
|
155
|
5
|
context.startTag("SPAN id = \"" + uniqueId(context) + "\"");
|
156
|
5
|
context.println(getFormattedText());
|
157
|
5
|
context.endTag("SPAN");
|
158
|
|
}
|
159
|
|
}
|
160
|
|
}
|
161
|
|
|