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

Source Code for Module muntjac.ui.option_group

  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 muntjac.ui.abstract_select import AbstractSelect 
 17  from muntjac.data.container import IContainer 
 18   
 19  from muntjac.event.field_events import \ 
 20      BlurEvent, IBlurListener, IBlurNotifier, FocusEvent, \ 
 21      IFocusListener, IFocusNotifier 
 22   
 23  from muntjac.terminal.gwt.client.ui.v_option_group import VOptionGroup 
 24   
 25   
26 -class OptionGroup(AbstractSelect, IBlurNotifier, IFocusNotifier):
27 """Configures select to be used as an option group.""" 28 29 CLIENT_WIDGET = None #ClientWidget(VOptionGroup, LoadStyle.EAGER) 30
31 - def __init__(self, *args):
32 self._disabledItemIds = set() 33 34 self._htmlContentAllowed = False 35 36 args = args 37 nargs = len(args) 38 if nargs == 0: 39 super(OptionGroup, self).__init__() 40 elif nargs == 1: 41 caption, = args 42 super(OptionGroup, self).__init__(caption) 43 elif nargs == 2: 44 if isinstance(args[1], IContainer): 45 caption, dataSource = args 46 super(OptionGroup, self).__init__(caption, dataSource) 47 else: 48 caption, options = args 49 super(OptionGroup, self).__init__(caption, options) 50 else: 51 raise ValueError, 'too many arguments'
52 53
54 - def paintContent(self, target):
55 target.addAttribute('type', 'optiongroup') 56 if self.isHtmlContentAllowed(): 57 target.addAttribute(VOptionGroup.HTML_CONTENT_ALLOWED, True) 58 super(OptionGroup, self).paintContent(target)
59 60
61 - def paintItem(self, target, itemId):
62 super(OptionGroup, self).paintItem(target, itemId) 63 if not self.isItemEnabled(itemId): 64 target.addAttribute('disabled', True)
65 66
67 - def changeVariables(self, source, variables):
68 super(OptionGroup, self).changeVariables(source, variables) 69 70 if FocusEvent.EVENT_ID in variables: 71 self.fireEvent(FocusEvent(self)) 72 73 if BlurEvent.EVENT_ID in variables: 74 self.fireEvent(BlurEvent(self))
75 76
77 - def addListener(self, listener, iface=None):
78 if (isinstance(listener, IBlurListener) and 79 (iface is None or issubclass(iface, IBlurListener))): 80 self.registerListener(BlurEvent.EVENT_ID, BlurEvent, 81 listener, IBlurListener.blurMethod) 82 83 if (isinstance(listener, IFocusListener) and 84 (iface is None or issubclass(iface, IFocusListener))): 85 self.registerListener(FocusEvent.EVENT_ID, FocusEvent, 86 listener, IFocusListener.focusMethod) 87 88 super(OptionGroup, self).addListener(listener, iface)
89 90
91 - def addCallback(self, callback, eventType=None, *args):
92 if eventType is None: 93 eventType = callback._eventType 94 95 if issubclass(eventType, BlurEvent): 96 self.registerCallback(BlurEvent, callback, 97 BlurEvent.EVENT_ID, *args) 98 99 elif issubclass(eventType, FocusEvent): 100 self.registerCallback(FocusEvent, callback, 101 FocusEvent.EVENT_ID, *args) 102 else: 103 super(OptionGroup, self).addCallback(callback, eventType, *args)
104 105
106 - def removeListener(self, listener, iface=None):
107 if (isinstance(listener, IBlurListener) and 108 (iface is None or issubclass(iface, IBlurListener))): 109 self.withdrawListener(BlurEvent.EVENT_ID, BlurEvent, listener) 110 111 if (isinstance(listener, IFocusListener) and 112 (iface is None or issubclass(iface, IFocusListener))): 113 self.withdrawListener(FocusEvent.EVENT_ID, FocusEvent, listener) 114 115 super(OptionGroup, self).removeListener(listener, iface)
116 117
118 - def removeCallback(self, callback, eventType=None):
119 if eventType is None: 120 eventType = callback._eventType 121 122 if issubclass(eventType, BlurEvent): 123 self.withdrawCallback(BlurEvent, callback, BlurEvent.EVENT_ID) 124 125 elif issubclass(eventType, FocusEvent): 126 self.withdrawCallback(FocusEvent, callback, FocusEvent.EVENT_ID) 127 128 else: 129 super(OptionGroup, self).removeCallback(callback, eventType)
130 131
132 - def setValue(self, newValue, repaintIsNotNeeded=None):
133 if repaintIsNotNeeded is not None and repaintIsNotNeeded is True: 134 # Check that value from changeVariables() doesn't contain unallowed 135 # selections: In the multi select mode, the user has selected or 136 # deselected a disabled item. In the single select mode, the user 137 # has selected a disabled item. 138 if self.isMultiSelect(): 139 currentValueSet = self.getValue() 140 newValueSet = newValue 141 for itemId in currentValueSet: 142 if (not self.isItemEnabled(itemId) 143 and not (itemId in newValueSet)): 144 self.requestRepaint() 145 return 146 147 for itemId in newValueSet: 148 if (not self.isItemEnabled(itemId) 149 and not (itemId in currentValueSet)): 150 self.requestRepaint() 151 return 152 else: 153 if newValue is None: 154 newValue = self.getNullSelectionItemId() 155 156 if not self.isItemEnabled(newValue): 157 self.requestRepaint() 158 return 159 160 super(OptionGroup, self).setValue(newValue, repaintIsNotNeeded)
161 162
163 - def setItemEnabled(self, itemId, enabled):
164 """Sets an item disabled or enabled. In the multiselect mode, a disabled 165 item cannot be selected or deselected by the user. In the single 166 selection mode, a disable item cannot be selected. 167 168 However, programmatical selection or deselection of an disable item is 169 possible. By default, items are enabled. 170 171 @param itemId: 172 the id of the item to be disabled or enabled 173 @param enabled: 174 if true the item is enabled, otherwise the item is disabled 175 """ 176 if itemId is not None: 177 178 if enabled: 179 self._disabledItemIds.remove(itemId) 180 else: 181 self._disabledItemIds.add(itemId) 182 183 self.requestRepaint()
184 185
186 - def isItemEnabled(self, itemId):
187 """Returns true if the item is enabled. 188 189 @param itemId: 190 the id of the item to be checked 191 @return: true if the item is enabled, false otherwise 192 @see: L{setItemEnabled} 193 """ 194 if itemId is not None: 195 return not (itemId in self._disabledItemIds) 196 197 return True
198 199
200 - def setHtmlContentAllowed(self, htmlContentAllowed):
201 """Sets whether html is allowed in the item captions. If set to true, 202 the captions are passed to the browser as html and the developer is 203 responsible for ensuring no harmful html is used. If set to false, the 204 content is passed to the browser as plain text. 205 206 @param htmlContentAllowed: 207 true if the captions are used as html, false if used as plain 208 text 209 """ 210 self._htmlContentAllowed = htmlContentAllowed 211 self.requestRepaint()
212 213
214 - def isHtmlContentAllowed(self):
215 """Checks whether captions are interpreted as html or plain text. 216 217 @return: true if the captions are used as html, false if used as plain 218 text 219 @see: L{setHtmlContentAllowed} 220 """ 221 return self._htmlContentAllowed
222