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

Source Code for Module library.pyjamas.ui.TextBoxBase

  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  from __pyjamas__ import console, JS 
 18  from pyjamas.ui.FocusWidget import FocusWidget 
 19  from pyjamas.ui.ChangeListener import ChangeHandler 
 20  from pyjamas.ui.InputListener import InputHandler 
 21  from pyjamas.ui import Event 
22 23 -class TextBoxBase(FocusWidget, ChangeHandler, InputHandler):
24 ALIGN_CENTER = "center" 25 ALIGN_JUSTIFY = "justify" 26 ALIGN_LEFT = "left" 27 ALIGN_RIGHT = "right" 28 29 _props = [("name", "Name", "Name", None), 30 ("align", "Text Alignment", "TextAlign", None), 31 ] 32
33 - def __init__(self, element, **kwargs):
34 self.currentEvent = None 35 36 FocusWidget.__init__(self, element, **kwargs) 37 ChangeHandler.__init__(self) 38 InputHandler.__init__(self)
39 40 @classmethod
41 - def _getProps(self):
42 return FocusWidget._getProps() + self._props
43
44 - def cancelKey(self):
45 if self.currentEvent is not None: 46 DOM.eventPreventDefault(self.currentEvent)
47
48 - def getCursorPos(self):
49 element = self.getElement() 50 try: 51 return element.selectionStart 52 except: 53 return 0
54
55 - def getName(self):
56 return DOM.getAttribute(self.getElement(), "name")
57
58 - def getSelectedText(self):
59 start = self.getCursorPos() 60 length = self.getSelectionLength() 61 text = self.getText() 62 return text[start:start + length]
63
64 - def getSelectionLength(self):
65 element = self.getElement() 66 try: 67 return element.selectionEnd - element.selectionStart 68 except: 69 return 0
70 71 # have to override Focus here for TextBoxBase 72 # because FocusWidget (actually FocusMixin) assumes that 73 # CreateFocusable has been used to create the element. 74 # in "input box" type scenarios, it hasn't: it's created 75 # by TextBox class etc.
76 - def setFocus(self, focused):
77 if (focused): 78 self.getElement().focus() 79 else: 80 self.getElement().blur()
81
82 - def getText(self):
83 return DOM.getAttribute(self.getElement(), "value")
84
85 - def selectAll(self):
86 length = len(self.getText()) 87 if length > 0: 88 self.setSelectionRange(0, length)
89
90 - def setCursorPos(self, pos):
91 self.setSelectionRange(pos, 0)
92
93 - def setKey(self, key):
94 if self.currentEvent is not None: 95 DOM.eventSetKeyCode(self.currentEvent, key)
96
97 - def setName(self, name):
98 DOM.setAttribute(self.getElement(), "name", name)
99
100 - def setSelectionRange(self, pos, length):
101 if length < 0: 102 # throw new IndexOutOfBoundsException("Length must be a positive integer. Length: " + length); 103 console.error("Length must be a positive integer. Length: " + length) 104 105 if (pos < 0) or (length + pos > len(self.getText())): 106 #throw new IndexOutOfBoundsException("From Index: " + pos + " To Index: " + (pos + length) + " Text Length: " + getText().length()); 107 console.error("From Index: %d " % pos + " To Index: %d " % (pos + length) + " Text Length: %d " % len(self.getText())) 108 109 element = self.getElement() 110 element.setSelectionRange(pos, pos + length)
111
112 - def setText(self, text):
113 DOM.setAttribute(self.getElement(), "value", text)
114
115 - def setTextAlignment(self, align):
116 DOM.setStyleAttribute(self.getElement(), "textAlign", align)
117
118 - def getTextAlignment(self, align):
119 return DOM.getStyleAttribute(self.getElement(), "textAlign")
120 121 122 # TODO: work out if TextBoxBase is appropriate to create in Factory. 123 # Factory.registerClass('pyjamas.ui.TextBoxBase', 'TextBoxBase', TextBoxBase) 124