diff src/browser/sat_browser/list_manager.py @ 739:4545d48dee60

browser_side: improve ListPanel and TitlePanel
author souliane <souliane@mailoo.org>
date Thu, 19 Nov 2015 16:42:39 +0100
parents fe3c2357a8c9
children b6510fd9ae15
line wrap: on
line diff
--- a/src/browser/sat_browser/list_manager.py	Thu Nov 19 13:14:01 2015 +0100
+++ b/src/browser/sat_browser/list_manager.py	Thu Nov 19 16:42:39 2015 +0100
@@ -18,12 +18,13 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.log import getLogger
-from pyjamas.ui.DragHandler import DragHandler
 log = getLogger(__name__)
+from sat.core.i18n import _
 
 from pyjamas.ui.ClickListener import ClickHandler
 from pyjamas.ui.FocusListener import FocusHandler
 from pyjamas.ui.ChangeListener import ChangeHandler
+from pyjamas.ui.DragHandler import DragHandler
 from pyjamas.ui.KeyboardListener import KeyboardHandler, KEY_ENTER
 from pyjamas.ui.DragWidget import DragWidget
 from pyjamas.ui.ListBox import ListBox
@@ -46,7 +47,8 @@
 class ListItem(HorizontalPanel):
     """This class implements a list item with auto-completion and a delete button."""
 
-    STYLE = {"listItem-box": "listItem-box",
+    STYLE = {"listItem": "listItem",
+             "listItem-box": "listItem-box",
              "listItem-box-invalid": "listItem-box-invalid",
              "listItem-button": "listItem-button",
              }
@@ -63,6 +65,7 @@
         @param validate (callable): method returning a bool to validate the entry
         """
         HorizontalPanel.__init__(self)
+        self.addStyleName(self.STYLE["listItem"])
 
         self.box = AutoCompleteTextBox(StyleName=self.STYLE["listItem-box"])
         self.remove_btn = Button('<span>x</span>', Visible=False)
@@ -107,6 +110,7 @@
 
         if self.last_validity == self.VALID:
             self.box.removeStyleName(self.STYLE["listItem-box-invalid"])
+            self.box.setVisibleLength(max(len(self.text), 10))
         elif self.last_validity == self.INVALID:
             self.box.addStyleName(self.STYLE["listItem-box-invalid"])
         elif self.last_validity == self.DUPLICATE:
@@ -116,6 +120,7 @@
         if self.taglist and self.text:
             self.taglist.tag([self.text])
         self.last_checked_value = self.text
+        self.box.setSelectionRange(len(self.text), 0)  
         self.remove_btn.setVisible(len(self.text) > 0)
                      
     def setFocus(self, focused):
@@ -146,8 +151,6 @@
 
     def onDragStart(self, event):
         """The user starts dragging the item."""
-        self.box.setSelectionRange(len(self.text), 0)
-
         dt = event.dataTransfer
         dt.setData('text/plain', "%s\n%s" % (self.text, "CONTACT_TEXTBOX"))
         dt.setDragImage(self.box.getElement(), 15, 15)
@@ -158,13 +161,16 @@
 
     last_item = None  # the last item is an empty text box for user input
 
-    def __init__(self, manager, key):
+    def __init__(self, taglist):
+        """
+        
+        @param taglist (quick_list_manager.QuickTagList): list manager
+        """
         ClickHandler.__init__(self)
         FocusHandler.__init__(self)
         ChangeHandler.__init__(self)
         KeyboardHandler.__init__(self)
-        self.manager = manager
-        self.key = key
+        self.taglist = taglist
 
     def addItem(self, item):
         raise NotImplementedError
@@ -189,7 +195,7 @@
         """The text box has the focus."""
         #log.debug("onFocus sender type:  %s" % type(sender))
         assert isinstance(sender, AutoCompleteTextBox)
-        sender.setCompletionItems(self.manager.untagged)
+        sender.setCompletionItems(self.taglist.untagged)
 
     def onKeyUp(self, sender, keycode, modifiers):
         """The text box is being modified - or ENTER key has been pressed."""
@@ -213,17 +219,24 @@
             # this method has already been called (by self.onChange) and there's nothing new
             return
         item.refresh()
-        item.box.setSelectionRange(len(item.text), 0)  
         if item == self.last_item and item.last_validity == ListItem.VALID and item.text:
             self.addItem()
 
 class DraggableListItemHandler(ListItemHandler, DragHandler):
     """Implements basic handlers for the DraggableListItem events."""
 
-    def __init__(self, manager, key):
-        ListItemHandler.__init__(self, manager, key)
+    def __init__(self, manager):
+        """
+        
+        @param manager (ListManager): list manager
+        """
+        ListItemHandler.__init__(self, manager)
         DragHandler.__init__(self)
 
+    @property
+    def manager(self):
+        return self.taglist
+
     def onDragStart(self, event):
         """The user starts dragging the item."""
         self.manager.drop_target = None
@@ -246,23 +259,20 @@
     #     - FlowPanel.insert doesn't work
 
     STYLE = {"listPanel": "listPanel"}
+    ACCEPT_NEW_ENTRY = False
 
-    def __init__(self, manager, key, items):
+    def __init__(self, manager, items=None):
         """Initialization with a button for the list name (key) and a DraggableListItem.
 
-        @param manager (ListManager)
-        @param key (unicode): list name
-        @param items (list): items to append
+        @param manager (ListManager): list manager
+        @param items (list): items to be set
         """
         FlowPanel.__init__(self)
-        DraggableListItemHandler.__init__(self, manager, key)
+        DraggableListItemHandler.__init__(self, manager)
         libervia_widget.DropCell.__init__(self, None)
         self.addStyleName(self.STYLE["listPanel"])
         self.manager = manager
-        items.sort()
-        self.addItem()
-        for item in items:
-            self.addItem(unicode(item))
+        self.resetItems(items)
 
         # FIXME: dirty magic strings '@' and '@@'
         self.drop_keys = {"GROUP": lambda host, item_s: self.addItem("@%s" % item_s),
@@ -309,6 +319,8 @@
 
         if count(self.getItems(), text) > 1:
             return ListItem.DUPLICATE  # item already exists in this list so we suggest its deletion
+        if self.ACCEPT_NEW_ENTRY:
+            return ListItem.VALID
         return ListItem.VALID if text in self.manager.items or not text else ListItem.INVALID
 
     def addItem(self, text=""):
@@ -346,6 +358,22 @@
             self.addItem("")
         item.remove()  # this also updates the taglist
 
+    def resetItems(self, items):
+        """Reset the items.
+        
+        @param items (list): items to be set
+        """
+        for child in self.getChildren():
+            child.remove()
+
+        self.addItem()
+        if not items:
+            return
+
+        items.sort()
+        for item in items:
+            self.addItem(unicode(item))
+
 
 class ListManager(FlexTable, quick_list_manager.QuickTagList):
     """Implements a table to manage one or several lists of items."""
@@ -380,7 +408,7 @@
             items = []
 
         self.lists[key] = {"button": Button(key, Title=key, StyleName=self.STYLE["listManager-button"]),
-                           "panel": ListPanel(self, key, items)}
+                           "panel": ListPanel(self, items)}
 
         y, x = len(self.lists), 0
         self.insertRow(y)