Package pyjamas :: Package ui :: Module CheckBox
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.CheckBox

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  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  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17   
 18  from pyjamas.ui.ButtonBase import ButtonBase 
 19  from pyjamas.ui import Event 
 20  from pyjamas.ui import Focus 
 21   
 22  _CheckBox_unique_id=0; 
23 24 -class CheckBox(ButtonBase):
25 26 _props = [("name", "Name", "Name", None), 27 ]
28 - def __init__(self, label=None, asHTML=False, **ka):
29 ka['StyleName'] = ka.get('StyleName', "gwt-CheckBox") 30 if label: 31 if asHTML: 32 ka['HTML'] = label 33 else: 34 ka['Text'] = label 35 element = ka.pop('Element', None) or DOM.createInputCheck() 36 self.initElement(element, **ka)
37 38 @classmethod
39 - def _getProps(self):
40 return ButtonBase._getProps() + self._props
41 42
43 - def sinkEvents(self, eventBitsToAdd):
44 """ Unlike other widgets the CheckBox sinks on its inputElement, 45 not its wrapper 46 """ 47 eventBitsToAdd |= DOM.getEventsSunk(self.inputElem) 48 DOM.sinkEvents(self.inputElem, eventBitsToAdd)
49
50 - def initElement(self, element, **ka):
51 self.inputElem = element 52 self.labelElem = DOM.createLabel() 53 element = ka.pop('Element', None) or DOM.createSpan() 54 ButtonBase.__init__(self, element, **ka) 55 56 self.sinkEvents(Event.FOCUSEVENTS | Event.ONCLICK) 57 58 DOM.appendChild(self.getElement(), self.inputElem) 59 DOM.appendChild(self.getElement(), self.labelElem) 60 61 uid = "check%d" % self.getUniqueID() 62 DOM.setAttribute(self.inputElem, "id", uid) 63 DOM.setAttribute(self.labelElem, "htmlFor", uid)
64 65 # emulate static
66 - def getUniqueID(self):
70
71 - def getHTML(self):
72 return DOM.getInnerHTML(self.labelElem)
73
74 - def getName(self):
75 return DOM.getAttribute(self.inputElem, "name")
76
77 - def getText(self):
78 return DOM.getInnerText(self.labelElem)
79
80 - def setChecked(self, checked):
81 DOM.setBooleanAttribute(self.inputElem, "checked", checked) 82 DOM.setBooleanAttribute(self.inputElem, "defaultChecked", checked)
83
84 - def isChecked(self):
85 """ XXX this function is deprecated: use getChecked 86 """ 87 return self.getChecked()
88
89 - def getChecked(self):
90 if self.isAttached(): 91 propName = "checked" 92 else: 93 propName = "defaultChecked" 94 95 return DOM.getBooleanAttribute(self.inputElem, propName)
96
97 - def isEnabled(self):
98 """ XXX this function is deprecated: use getEnabled 99 """ 100 return self.getEnabled()
101
102 - def getEnabled(self):
103 return not DOM.getBooleanAttribute(self.inputElem, "disabled")
104
105 - def setEnabled(self, enabled):
106 DOM.setBooleanAttribute(self.inputElem, "disabled", not enabled)
107
108 - def setFocus(self, focused):
109 if focused: 110 Focus.focus(self.inputElem) 111 else: 112 Focus.blur(self.inputElem)
113
114 - def setHTML(self, html):
115 DOM.setInnerHTML(self.labelElem, html)
116
117 - def setName(self, name):
118 DOM.setAttribute(self.inputElem, "name", name)
119
120 - def setTabIndex(self, index):
121 Focus.setTabIndex(self.inputElem, index)
122
123 - def setText(self, text):
124 DOM.setInnerText(self.labelElem, text)
125
126 - def onDetach(self):
127 self.setChecked(self.isChecked()) 128 ButtonBase.onDetach(self)
129 130 Factory.registerClass('pyjamas.ui.CheckBox', 'CheckBox', CheckBox) 131