diff browser_side/dialog.py @ 90:9729ed7171a6

browser side: Widgets' setting (col & row span can be changed
author Goffi <goffi@goffi.org>
date Tue, 28 Jun 2011 01:54:36 +0200
parents 9d8e79ac4c9c
children dd54bc13ac81
line wrap: on
line diff
--- a/browser_side/dialog.py	Mon Jun 27 20:10:29 2011 +0200
+++ b/browser_side/dialog.py	Tue Jun 28 01:54:36 2011 +0200
@@ -31,6 +31,7 @@
 from pyjamas.ui.Frame import Frame
 from pyjamas.ui import HasAlignment
 from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER
+from pyjamas import DOM
 
 class ContactsChooser(DialogBox):
 
@@ -249,3 +250,61 @@
         sender.setText('')
         self.list_box.setItemSelected(self.list_box.getItemCount()-1, "selected")
 
+class IntSetter(HorizontalPanel):
+    """This class show a bar with button to set an int value"""
+
+    def __init__(self, label, value=0, value_max=None, visible_len=3):
+        """initialize the intSetter
+        @param label: text shown in front of the setter
+        @param value: initial value
+        @param value_max: limit value, None or 0 for unlimited"""
+        HorizontalPanel.__init__(self)
+        self.value = value
+        self.value_max = value_max
+        _label = Label(label)
+        self.add(_label)
+        self.setCellWidth(_label, "100%")
+        minus_button = Button("-", self.onMinus)
+        self.box = TextBox()
+        self.box.setVisibleLength(visible_len)
+        self.box.setText(str(value))
+        self.box.addInputListener(self)
+        plus_button = Button("+", self.onPlus)
+        self.add(minus_button)
+        self.add(self.box)
+        self.add(plus_button)
+        self.valueChangedListener = []
+
+    def addValueChangeListener(self, listener):
+        self.valueChangedListener.append(listener)
+
+    def removeValueChangeListener(self, listener):
+        if listener in self.valueChangedListener:
+            self.valueChangedListener.remove(listener)
+
+    def _callListeners(self):
+        for listener in self.valueChangedListener:
+            listener(self.value)
+    
+    def onMinus(self, sender):
+        self.value=max(0,self.value-1)
+        self.box.setText(str(self.value))
+        self._callListeners()
+        
+    def onPlus(self, sender):
+        self.value+=1
+        if self.value_max:
+            self.value = min(self.value,value_max)
+        self.box.setText(str(self.value))
+        self._callListeners()
+
+    def onInput(self, sender):
+        """Accept only valid integer && normalize print (no leading 0)"""
+        try:
+            self.value = int(self.box.getText())
+        except ValueError:
+            pass
+        if self.value_max:
+            self.value = min(self.value,value_max)
+        self.box.setText(str(self.value))
+        self._callListeners()