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

Source Code for Module muntjac.ui.text_field

  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 text editor component.""" 
 17   
 18  from warnings import warn 
 19   
 20  from muntjac.ui.abstract_text_field import AbstractTextField 
 21  from muntjac.data.property import IProperty 
 22   
 23   
24 -class TextField(AbstractTextField):
25 """A text editor component that can be bound to any bindable IProperty. 26 The text editor supports both multiline and single line modes, default 27 is one-line mode. 28 29 Since C{TextField} extends C{AbstractField} it implements the 30 L{IBuffered} interface. A C{TextField} is in write-through mode by default, 31 so L{AbstractField.setWriteThrough} must be called to enable buffering. 32 33 @author: Vaadin Ltd. 34 @author: Richard Lincoln 35 @version: 1.1.2 36 """ 37 38 CLIENT_WIDGET = None #ClientWidget(VTextField, LoadStyle.EAGER) 39
40 - def __init__(self, *args):
41 """Constructs a C{TextField} with optional caption, dataSource and/or 42 value. 43 44 @param args: tuple of the form 45 - () 46 - (caption) 47 1. the caption string for the editor 48 - (dataSource) 49 1. the IProperty to be edited with this editor 50 - (caption, dataSource) 51 1. the caption string for the editor 52 2. the IProperty to be edited with this editor 53 - (caption, text) 54 1. the caption string for the editor 55 2. the initial text content of the editor 56 """ 57 #: Tells if input is used to enter sensitive information that is not 58 # echoed to display. Typically passwords. 59 self._secret = False 60 61 #: Number of visible rows in a multiline TextField. Value 0 implies a 62 # single-line text-editor. 63 self._rows = 0 64 65 #: Tells if word-wrapping should be used in multiline mode. 66 self._wordwrap = True 67 68 nargs = len(args) 69 if nargs == 0: 70 super(TextField, self).__init__() 71 self.setValue('') 72 elif nargs == 1: 73 if isinstance(args[0], IProperty): 74 super(TextField, self).__init__() 75 dataSource, = args 76 self.setPropertyDataSource(dataSource) 77 else: 78 caption, = args 79 TextField.__init__(self) 80 self.setCaption(caption) 81 elif nargs == 2: 82 if isinstance(args[1], IProperty): 83 caption, dataSource = args 84 TextField.__init__(self, dataSource) 85 self.setCaption(caption) 86 else: 87 super(TextField, self).__init__() 88 caption, value = args 89 self.setValue(value) 90 self.setCaption(caption) 91 else: 92 raise ValueError, 'too many arguments'
93 94
95 - def isSecret(self):
96 """Gets the secret property. If a field is used to enter secret 97 information the information is not echoed to display. 98 99 @return: C{True} if the field is used to enter secret 100 information, C{False} otherwise. 101 102 @deprecated: Use L{PasswordField} instead for secret text input. 103 """ 104 warn('use PasswordField instead', DeprecationWarning) 105 106 return self._secret
107 108
109 - def setSecret(self, secret):
110 """Sets the secret property on and off. If a field is used to enter 111 secret information the information is not echoed to display. 112 113 @param secret: 114 the value specifying if the field is used to enter secret 115 information. 116 @deprecated: Use L{PasswordField} instead for secret text input. 117 """ 118 warn('use PasswordField instead', DeprecationWarning) 119 120 if self._secret != secret: 121 self._secret = secret 122 self.requestRepaint()
123 124
125 - def paintContent(self, target):
126 if self.isSecret(): 127 target.addAttribute('secret', True) 128 129 rows = self.getRows() 130 if rows != 0: 131 target.addAttribute('rows', rows) 132 target.addAttribute('multiline', True) 133 134 if not self.isWordwrap(): 135 # Wordwrap is only painted if turned off to minimize 136 # communications 137 target.addAttribute('wordwrap', False) 138 139 super(TextField, self).paintContent(target)
140 141
142 - def getRows(self):
143 """Gets the number of rows in the editor. If the number of rows is set 144 to 0, the actual number of displayed rows is determined implicitly by 145 the adapter. 146 147 @return: number of explicitly set rows. 148 @deprecated: Use L{TextArea} for a multi-line text input. 149 """ 150 warn('use TextArea for a multi-line text input', DeprecationWarning) 151 152 return self._rows
153 154
155 - def setRows(self, rows):
156 """Sets the number of rows in the editor. 157 158 @param rows: 159 the number of rows for this editor. 160 @deprecated: Use L{TextArea} for a multi-line text input. 161 """ 162 warn('use TextArea for a multi-line text input', DeprecationWarning) 163 164 if rows < 0: 165 rows = 0 166 167 if self._rows != rows: 168 self._rows = rows 169 self.requestRepaint()
170 171
172 - def isWordwrap(self):
173 """Tests if the editor is in word-wrap mode. 174 175 @return: C{True} if the component is in the word-wrap mode, 176 C{False} if not. 177 @deprecated: Use L{TextArea} for a multi-line text input. 178 """ 179 warn('use TextArea for a multi-line text input', DeprecationWarning) 180 return self._wordwrap
181 182
183 - def setWordwrap(self, wordwrap):
184 """Sets the editor's word-wrap mode on or off. 185 186 @param wordwrap: 187 the boolean value specifying if the editor should be in 188 word-wrap mode after the call or not. 189 190 @deprecated: Use L{TextArea} for a multi-line text input. 191 """ 192 warn('use TextArea for a multi-line text input', DeprecationWarning) 193 if self._wordwrap != wordwrap: 194 self._wordwrap = wordwrap 195 self.requestRepaint()
196 197
198 - def setHeight(self, height, unit=None):
199 """Sets the height of the L{TextField} instance. 200 201 Setting height for L{TextField} also has a side-effect that puts 202 L{TextField} into multiline mode (aka "textarea"). Multiline mode 203 can also be achieved by calling L{setRows}. The height 204 value overrides the number of rows set by L{setRows}. 205 206 If you want to set height of single line L{TextField}, call 207 L{setRows} with value 0 after setting the height. Setting 208 rows to 0 resets the side-effect. 209 210 You should use L{TextArea} instead of L{TextField} for multiline 211 text input. 212 """ 213 if unit is None: 214 # will call setHeight(float, int) the actually does the magic. 215 # Method is overridden just to document side-effects. 216 super(TextField, self).setHeight(height) 217 else: 218 super(TextField, self).setHeight(height, unit) 219 if height > 1 and self.__class__ == TextField: 220 # In html based terminals we most commonly want to make 221 # component to be textarea if height is defined. Setting row 222 # field above 0 will render component as textarea. 223 self.setRows(2)
224