changeset 253:19153af4f327

browser_side: isolate the "add contact group" panel in a re-usable class: - also prevent from adding a new group twice by handling an internal list
author souliane <souliane@mailoo.org>
date Sat, 09 Nov 2013 08:53:03 +0100
parents b77940d8a9bf
children 28d3315a8003
files browser_side/dialog.py
diffstat 1 files changed, 26 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/dialog.py	Fri Nov 08 17:07:27 2013 +0100
+++ b/browser_side/dialog.py	Sat Nov 09 08:53:03 2013 +0100
@@ -223,11 +223,12 @@
         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_title="OK", ok_cb=None, cancel_cb=None):
-        DialogBox.__init__(self, centered = True)
+        DialogBox.__init__(self, centered=True)
         main_panel = VerticalPanel()
         self.ok_cb = ok_cb
         self.cancel_cb = cancel_cb
@@ -243,14 +244,11 @@
         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_bt = Button("add", lambda sender: self.onGroupInput(add_group_tb))
-        add_group_panel.add(add_group_lb)
-        add_group_panel.add(add_group_tb)
-        add_group_panel.add(add_group_bt)
-        main_panel.add(add_group_panel)
+        def cb(text):
+            self.list_box.addItem(text)
+            self.list_box.setItemSelected(self.list_box.getItemCount() - 1, "selected")
+
+        main_panel.add(AddGroupPanel(initial_groups, cb))
 
         button_panel = HorizontalPanel()
         button_panel.addStyleName("marginAuto")
@@ -284,19 +282,34 @@
         if self.cancel_cb:
             self.cancel_cb(self)
 
+
+class AddGroupPanel(HorizontalPanel):
+    def __init__(self, groups, cb=None):
+        """
+        @param groups: list of the already existing groups
+        """
+        HorizontalPanel.__init__(self)
+        self.groups = groups
+        self.add(Label('Add group:'))
+        textbox = ExtTextBox(enter_cb=self.onGroupInput)
+        self.add(textbox)
+        self.add(Button("add", lambda sender: self.onGroupInput(textbox)))
+        self.cb = cb
+
     def onGroupInput(self, sender):
         text = sender.getText()
-        for index in xrange(0, self.list_box.getItemCount()):
-            if text == self.list_box.getValue(index):
+        for group in self.groups:
+            if text == group:
                 Window.alert("The group '%s' already exists." % text)
                 return
         for pattern in FORBIDDEN_PATTERNS_IN_GROUP:
             if pattern in text:
                 Window.alert("The pattern '%s' is not allowed in group names." % pattern)
                 return
-        self.list_box.addItem(text)
         sender.setText('')
-        self.list_box.setItemSelected(self.list_box.getItemCount()-1, "selected")
+        self.groups.append(text)
+        if self.cb is not None:
+            self.cb(text)
 
 
 class WheelTextBox(TextBox, MouseWheelHandler):