Package muntjac :: Package ui :: Module rich_text_area
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.rich_text_area

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 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 #ClientWidget(VRichTextArea, LoadStyle.LAZY) 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 # Value formatter used to format the string contents. 54 self._format = None 55 56 # Null representation. 57 self._nullRepresentation = 'null' 58 59 # Is setting to null from non-null value allowed by setting with 60 # null representation. 61 self._nullSettingAllowed = False 62 63 # Temporary flag that indicates all content will be selected after 64 # the next paint. Reset to false after painted. 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 # Adds the content as variable 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 # IE6 cannot support multi-classname selectors properly 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 # Set selection range functionality is currently being 124 # planned/developed for GWT RTA. Only selecting all is currently 125 # supported. Consider moving selectAll and other selection related 126 # functions to AbstractTextField at that point to share the 127 # implementation. Some third party components extending 128 # AbstractTextField might however not want to support them. 129 self._selectAll = True 130 self.focus() 131 self.requestRepaint()
132 133
134 - def getFormattedValue(self):
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 # Sets the text 164 if 'text' in variables and not self.isReadOnly(): 165 166 # Only do the setting if the string representation of the 167 # value has been updated 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 # If the modified status changes, or if we have a formatter, 182 # repaint is needed after all. 183 if (self._format is not None 184 or wasModified != self.isModified()): 185 self.requestRepaint()
186 187
188 - def getType(self):
189 return str
190 191
192 - def getNullRepresentation(self):
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
207 - def isNullSettingAllowed(self):
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
262 - def getFormat(self):
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
272 - def setFormat(self, frmt):
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
285 - def isEmpty(self):
286 return super(RichTextArea, self).isEmpty() or (len(str(self)) == 0)
287