diff browser_side/dialog.py @ 52:4419ef07bb2b

browser side: adding contact, first draft
author Goffi <goffi@goffi.org>
date Fri, 27 May 2011 01:52:29 +0200
parents f1d2eb9b2523
children f25c4077f6b9
line wrap: on
line diff
--- a/browser_side/dialog.py	Thu May 26 23:03:45 2011 +0200
+++ b/browser_side/dialog.py	Fri May 27 01:52:29 2011 +0200
@@ -25,9 +25,11 @@
 from pyjamas.ui.DialogBox import DialogBox
 from pyjamas.ui.ListBox import ListBox
 from pyjamas.ui.Button import Button
+from pyjamas.ui.TextBox import TextBox
+from pyjamas.ui.Label import Label
 from pyjamas.ui.HTML import HTML
 from pyjamas.ui import HasAlignment
-from pyjamas.ui.KeyboardListener import KEY_ESCAPE
+from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER
 
 class ContactsChooser(DialogBox):
 
@@ -152,3 +154,83 @@
             #needed to prevent request cancellation in Firefox
             event.preventDefault()
         return PopupPanel.onEventPreview(self, event)
+
+class ExtTextBox(TextBox):
+    """Extended TextBox"""
+
+    def __init__(self, *args, **kwargs):
+        if 'enter_cb' in kwargs:
+            self.enter_cb = kwargs['enter_cb']
+            del kwargs['enter_cb']
+        TextBox.__init__(self, *args, **kwargs)
+        self.addKeyboardListener(self)
+
+    def onKeyUp(self, sender, keycode, modifiers):
+        pass
+    
+    def onKeyDown(self, sender, keycode, modifiers):
+        pass
+    
+    def onKeyPress(self, sender, keycode, modifiers):
+        if self.enter_cb and keycode == KEY_ENTER:
+            self.enter_cb(self)
+
+class GroupSelector(DialogBox):
+    
+    def __init__(self, top_widgets, initial_groups, selected_groups, ok_cb = None, cancel_cb = None):
+        DialogBox.__init__(self, centered = True)
+        main_panel = VerticalPanel()
+        self.ok_cb = ok_cb
+        self.cancel_cb = cancel_cb
+        
+        for wid in top_widgets:
+            main_panel.add(wid)
+        
+        main_panel.add(Label('Select in which groups your contact is:'))
+        self.list_box = ListBox()
+        self.list_box.setMultipleSelect(True)
+        self.list_box.setVisibleItemCount(5)
+        self.setAvailableGroups(initial_groups)
+        self.setGroupsSelected(selected_groups)
+        main_panel.add(self.list_box)
+        
+        add_group_panel = HorizontalPanel()
+        add_group_lb = Label('Add group:')
+        add_group_tb = ExtTextBox(enter_cb = self.onGroupInput)
+        add_group_panel.add(add_group_lb)
+        add_group_panel.add(add_group_tb)
+        main_panel.add(add_group_panel)
+        
+        button_panel = HorizontalPanel()
+        button_panel.add(Button("Add", self.onOK))
+        button_panel.add(Button("Cancel", self.onCancel))
+        main_panel.add(button_panel)
+        
+        self.setWidget(main_panel)
+
+    def setAvailableGroups(self, groups):
+        _groups = list(set(groups))
+        _groups.sort()
+        self.list_box.clear()
+        for group in _groups:
+            self.list_box.addItem(group)
+
+    def setGroupsSelected(self, groups):
+        if groups:
+            self.list_box.setItemTextSelection(selected_groups)
+    
+    def onOK(self, sender):
+        self.hide()
+        if self.ok_cb:
+            self.ok_cb(self)
+
+    def onCancel(self, sender):
+        self.hide()
+        if self.cancel_cb:
+            self.cancel_cb(self)
+
+    def onGroupInput(self, sender):
+        self.list_box.addItem(sender.getText())
+        sender.setText('')
+        self.list_box.setItemSelected(self.list_box.getItemCount()-1, "selected")
+