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

Source Code for Module muntjac.ui.default_field_factory

  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  from datetime import datetime 
 17   
 18  try: 
 19      from cStringIO import StringIO 
 20  except ImportError, e: 
 21      from StringIO import StringIO 
 22   
 23  from muntjac.data.item import IItem 
 24  from muntjac.ui.table_field_factory import ITableFieldFactory 
 25  from muntjac.ui.form_field_factory import IFormFieldFactory 
26 27 28 -class DefaultFieldFactory(IFormFieldFactory, ITableFieldFactory):
29 """This class contains a basic implementation for both 30 L{IFormFieldFactory} and L{ITableFieldFactory}. The class is 31 singleton, use L{get} method to get reference to the instance. 32 33 There are also some static helper methods available for custom built 34 field factories. 35 """ 36 37 INSTANCE = None # set below 38 39 @classmethod
40 - def get(cls):
41 """Singleton method to get an instance of DefaultFieldFactory. 42 43 @return: an instance of DefaultFieldFactory 44 """ 45 return cls.INSTANCE
46 47
48 - def createField(self, *args):
49 nargs = len(args) 50 if nargs == 3: 51 item, propertyId, _ = args 52 typ = item.getItemProperty(propertyId).getType() 53 field = self.createFieldByPropertyType(typ) 54 field.setCaption(self.createCaptionByPropertyId(propertyId)) 55 return field 56 elif nargs == 4: 57 container, itemId, propertyId, _ = args 58 containerProperty = \ 59 container.getContainerProperty(itemId, propertyId) 60 typ = containerProperty.getType() 61 field = self.createFieldByPropertyType(typ) 62 field.setCaption(self.createCaptionByPropertyId(propertyId)) 63 return field 64 else: 65 raise ValueError, 'invalid number of arguments'
66 67 68 @classmethod
69 - def createCaptionByPropertyId(cls, propertyId):
70 """If name follows method naming conventions, convert the name to 71 spaced upper case text. For example, convert "firstName" to 72 "First Name" 73 74 @return: the formatted caption string 75 """ 76 name = str(propertyId) 77 78 if len(name) > 0: 79 if (name.find(' ') < 0 80 and name[0] == name[0].lower() 81 and name[0] != name[0].upper()): 82 out = StringIO() 83 out.write(name[0].upper()) 84 i = 1 85 while i < len(name): 86 j = i 87 while j < len(name): 88 c = name[j] 89 if c.lower() != c and c.upper() == c: 90 break 91 j += 1 92 93 if j == len(name): 94 out.write(name[i:]) 95 else: 96 out.write(name[i:j]) 97 out.write(' ' + name[j]) 98 i = j + 1 99 name = out.getvalue() 100 out.close() 101 102 return name
103 104 105 @classmethod
106 - def createFieldByPropertyType(cls, typ):
107 """Creates fields based on the property type. 108 109 The default field type is L{TextField}. Other field types 110 generated by this method: 111 112 - B{Boolean}: L{CheckBox}. 113 - B{Date}: L{DateField}(resolution: day). 114 - B{IItem}: L{Form}. 115 - B{default field type}: L{TextField}. 116 117 @param typ: 118 the type of the property 119 @return: the most suitable generic L{Field} for given type 120 """ 121 from muntjac.ui.date_field import DateField # FIXME: circular import 122 from muntjac.ui.text_field import TextField 123 from muntjac.ui.check_box import CheckBox 124 from muntjac.ui.form import Form 125 126 # Null typed properties can not be edited 127 if typ is None: 128 return None 129 130 # IItem field 131 if issubclass(typ, IItem): 132 return Form() 133 134 # Date field 135 if issubclass(typ, datetime): 136 df = DateField() 137 df.setResolution(DateField.RESOLUTION_DAY) 138 return df 139 140 # Boolean field 141 if issubclass(typ, bool): 142 return CheckBox() 143 144 return TextField()
145 146 DefaultFieldFactory.INSTANCE = DefaultFieldFactory() 147