1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Defines a simple C{RichTextArea} to edit HTML format text."""
17
18 from warnings import warn
19
20 from muntjac.ui.abstract_field import AbstractField
21 from muntjac.data.property import IProperty
22
23
24 -class RichTextArea(AbstractField):
25 """A simple RichTextArea to edit HTML format text.
26
27 Note, that using L{TextField.setMaxLength} method in
28 L{RichTextArea} may produce unexpected results as formatting
29 is counted into length of field.
30 """
31
32 CLIENT_WIDGET = None
33
34 - def __init__(self, *args):
35 """Constructs an empty C{RichTextArea} with optional caption, value
36 and/or data source.
37
38 @param args: tuple of the form
39 - ()
40 - (caption)
41 1. the caption for the editor.
42 - (dataSource)
43 1. the data source for the editor value
44 - (caption, dataSource)
45 1. the caption for the editor.
46 2. the data source for the editor value
47 - (caption, value)
48 1. the caption for the editor.
49 2. the initial text content of the editor.
50 """
51 super(RichTextArea, self).__init__()
52
53
54 self._format = None
55
56
57 self._nullRepresentation = 'null'
58
59
60
61 self._nullSettingAllowed = False
62
63
64
65 self._selectAll = False
66
67 nargs = len(args)
68 if nargs == 0:
69 self.setValue('')
70 elif nargs == 1:
71 if isinstance(args[0], IProperty):
72 dataSource, = args
73 self.setPropertyDataSource(dataSource)
74 else:
75 caption, = args
76 RichTextArea.__init__(self)
77 self.setCaption(caption)
78 elif nargs == 2:
79 if isinstance(args[1], IProperty):
80 caption, dataSource = args
81 RichTextArea.__init__(self, dataSource)
82 self.setCaption(caption)
83 else:
84 caption, value = args
85 self.setValue(value)
86 self.setCaption(caption)
87 else:
88 raise ValueError, 'too many arguments'
89
90
91 - def paintContent(self, target):
92 if self._selectAll:
93 target.addAttribute('selectAll', True)
94 self._selectAll = False
95
96
97 value = self.getFormattedValue()
98 if value is None:
99 value = self.getNullRepresentation()
100
101 if value is None:
102 raise ValueError, ('Null values are not allowed if '
103 'the null-representation is null')
104
105 target.addVariable(self, 'text', value)
106
107 super(RichTextArea, self).paintContent(target)
108
109
110 - def setReadOnly(self, readOnly):
111 super(RichTextArea, self).setReadOnly(readOnly)
112
113 if readOnly:
114 self.addStyleName('v-richtextarea-readonly')
115 else:
116 self.removeStyleName('v-richtextarea-readonly')
117
118
119 - def selectAll(self):
120 """Selects all text in the rich text area. As a side effect,
121 focuses the rich text area.
122 """
123
124
125
126
127
128
129 self._selectAll = True
130 self.focus()
131 self.requestRepaint()
132
133
135 """Gets the formatted string value. Sets the field value by using
136 the assigned Format.
137
138 @return: the Formatted value.
139 @see: L{setFormat}
140 @deprecated:
141 """
142 v = self.getValue()
143 if v is None:
144 return None
145 return str(v)
146
147
148 - def getValue(self):
149 v = super(RichTextArea, self).getValue()
150 if self._format is None or v is None:
151 return v
152 try:
153 warn('deprecated', DeprecationWarning)
154 return self._format.format(v)
155 except ValueError:
156 return v
157
158
159 - def changeVariables(self, source, variables):
160
161 super(RichTextArea, self).changeVariables(source, variables)
162
163
164 if 'text' in variables and not self.isReadOnly():
165
166
167
168 newValue = variables.get('text')
169
170 oldValue = self.getFormattedValue()
171 if (newValue is not None
172 and (oldValue is None or self.isNullSettingAllowed())
173 and newValue == self.getNullRepresentation()):
174 newValue = None
175
176 if (newValue != oldValue
177 and (newValue is None or newValue != oldValue)):
178 wasModified = self.isModified()
179 self.setValue(newValue, True)
180
181
182
183 if (self._format is not None
184 or wasModified != self.isModified()):
185 self.requestRepaint()
186
187
190
191
193 """Gets the null-string representation.
194
195 The null-valued strings are represented on the user interface by
196 replacing the null value with this string. If the null representation
197 is set null (not 'null' string), painting null value throws exception.
198
199 The default value is string 'null'.
200
201 @return: the string textual representation for null strings.
202 @see: L{TextField.isNullSettingAllowed}
203 """
204 return self._nullRepresentation
205
206
208 """Is setting nulls with null-string representation allowed.
209
210 If this property is true, writing null-representation string to text
211 field always sets the field value to real null. If this property is
212 false, null setting is not made, but the null values are maintained.
213 Maintenance of null-values is made by only converting the textfield
214 contents to real null, if the text field matches the null-string
215 representation and the current value of the field is null.
216
217 By default this setting is false
218
219 @return: Should the null-string represenation be always
220 converted to null-values.
221 @see: L{TextField.getNullRepresentation}
222 """
223 return self._nullSettingAllowed
224
225
226 - def setNullRepresentation(self, nullRepresentation):
227 """Sets the null-string representation.
228
229 The null-valued strings are represented on the user interface by
230 replacing the null value with this string. If the null representation
231 is set null (not 'null' string), painting null value throws exception.
232
233 The default value is string 'null'
234
235 @param nullRepresentation:
236 Textual representation for null strings.
237 @see: L{TextField.setNullSettingAllowed}
238 """
239 self._nullRepresentation = nullRepresentation
240
241
242 - def setNullSettingAllowed(self, nullSettingAllowed):
243 """Sets the null conversion mode.
244
245 If this property is true, writing null-representation string to text
246 field always sets the field value to real null. If this property is
247 false, null setting is not made, but the null values are maintained.
248 Maintenance of null-values is made by only converting the textfield
249 contents to real null, if the text field matches the null-string
250 representation and the current value of the field is null.
251
252 By default this setting is false.
253
254 @param nullSettingAllowed:
255 Should the null-string representation be always converted
256 to null-values.
257 @see: L{TextField.getNullRepresentation}
258 """
259 self._nullSettingAllowed = nullSettingAllowed
260
261
263 """Gets the value formatter of TextField.
264
265 @return: the format used to format the value.
266 @deprecated: replaced by L{PropertyFormatter}
267 """
268 warn('replaced by PropertyFormatter', DeprecationWarning)
269 return self._format
270
271
273 """Gets the value formatter of TextField.
274
275 @param frmt:
276 the format used to format the value. Null disables the
277 formatting.
278 @deprecated: replaced by L{PropertyFormatter}
279 """
280 warn('replaced by PropertyFormatter', DeprecationWarning)
281 self._format = frmt
282 self.requestRepaint()
283
284
286 return super(RichTextArea, self).isEmpty() or (len(str(self)) == 0)
287