diff browser_side/panels.py @ 84:8f35e9970e7f

browser side: new widget handling: - new design integration improved (ChatPanel is now working) - widget now register themselves for event handling
author Goffi <goffi@goffi.org>
date Sun, 26 Jun 2011 23:23:22 +0200
parents 68d360caeecb
children a8f027738c16
line wrap: on
line diff
--- a/browser_side/panels.py	Sun Jun 26 03:04:17 2011 +0200
+++ b/browser_side/panels.py	Sun Jun 26 23:23:22 2011 +0200
@@ -107,9 +107,11 @@
             _new_panel = MicroblogPanel(self.host, accept_all=True)
             self.host.FillMicroblogPanel(_new_panel)
         else:
-            return
-        self.host.mpanels.remove(self)
-        self.host.mpanels.append(_new_panel)
+            return False
+        if isinstance(self, LiberviaWidget):
+            self.host.unregisterWidget(self)
+            if not isinstance(_new_panel, LiberviaWidget):
+                print ('WARNING: droping an object which is not a class of LiberviaWidget')
         grid = self.getParent()
         row_idx, cell_idx = self._getCellAndRow(grid, event)
         if self.host.selected == self:
@@ -125,19 +127,26 @@
             DOM.setStyleAttribute(td_elt, "width", "%s%%" % _width)
         #FIXME: delete object ? Check the right way with pyjamas
 
-class LiberviaWidget(DropCell, VerticalPanel):
+class LiberviaWidget(DropCell, VerticalPanel, ClickHandler):
     """Libervia's widget which can replace itself with a dropped widget on DnD"""
     
-    def __init__(self, title=''):
+    def __init__(self, host, title='', selectable=False):
+        """Init the widget
+        @param host: SatWebFrontend object
+        @param title: title show in the header of the widget
+        @param selectable: True is widget can be selected by user"""
         VerticalPanel.__init__(self)
         DropCell.__init__(self)
-        self.title_id = HTMLPanel.createUniqueId()
-        self.setting_button_id = HTMLPanel.createUniqueId()
-        self.close_button_id = HTMLPanel.createUniqueId()
+        ClickHandler.__init__(self)
+        self.host = host
+        self.__selectable = selectable
+        self.__title_id = HTMLPanel.createUniqueId()
+        self.__setting_button_id = HTMLPanel.createUniqueId()
+        self.__close_button_id = HTMLPanel.createUniqueId()
         header = AbsolutePanel()
-        self.title = Label(title)
-        self.title.setStyleName('widgetHeader_title')
-        header.add(self.title)
+        self.__title = Label(title)
+        self.__title.setStyleName('widgetHeader_title')
+        header.add(self.__title)
         #header.setCellVerticalAlignment(self.title, HasAlignment.ALIGN_MIDDLE)
         #header.setCellWidth(self.title, '100%')
         button_group_wrapper = SimplePanel()
@@ -156,11 +165,30 @@
         header.addStyleName('widgetHeader')
         self.setSize('100%', '100%')
         self.addStyleName('widget')
+        if self.__selectable:
+            self.addClickListener(self)
+        self.host.registerWidget(self)
+    
+    def onClick(self, sender):
+        self.host.select(self)
 
     def setTitle(self, text):
         """change the title in the header of the widget
         @param text: text of the new title"""
-        self.title.setText(text)
+        self.__title.setText(text)
+
+    def isSelectable(self):
+        return self.__selectable
+
+    def setSelectable(self, selectable):
+        if not self.__selectable:
+            try:
+                self.removeClickListener(self)
+            except ValueError:
+                pass
+        if self.selectable and not self in self._clickListeners:
+            self.addClickListener(self)
+        self.__selectable = selectable
 
     def setWidget(self, widget, scrollable=True):
         """Set the widget that will be in the body of the LiberviaWidget
@@ -365,10 +393,9 @@
         """Panel used to show microblog
         @param title: title of the panel
         @param accept_all: if true, show every message, without filtering jids"""
-        LiberviaWidget.__init__(self, title)
+        LiberviaWidget.__init__(self, host, title)
         #ScrollPanelWrapper.__init__(self)
         #DropCell.__init__(self)
-        self.host = host
         self.accept_all = accept_all
         self.accepted_groups = []
         self.entries = {}
@@ -476,50 +503,40 @@
         self.occupants_list.clear()
         AbsolutePanel.clear(self)
 
-class ChatPanel(DropCell, ClickHandler, ScrollPanelWrapper):
+class ChatPanel(LiberviaWidget):
 
     def __init__(self, host, target, type='one2one'):
         """Panel used for conversation (one 2 one or group chat)
         @param host: SatWebFrontend instance
         @param target: entity (JID) with who we have a conversation (contact's jid for one 2 one chat, or MUC room)
         @param type: one2one for simple conversation, group for MUC"""
-        ScrollPanelWrapper.__init__(self)
-        DropCell.__init__(self)
-        ClickHandler.__init__(self)
+        LiberviaWidget.__init__(self, host, target.bare, selectable = True)
         self.vpanel = VerticalPanel()
         self.vpanel.setSize('100%','100%')
-        self.host = host
         self.type = type
         self.nick = None
         if not target:
             print "ERROR: Empty target !"
             return
         self.target = target
-        title="%s" % target.bare
-        _class = ['mb_panel_header']
-        self.header = HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),html_sanitize(title)))
-        self.header.setStyleName('chatHeader')
-        self.body = AbsolutePanel()
-        self.body.setStyleName('chatPanel_body')
+        self.__body = AbsolutePanel()
+        self.__body.setStyleName('chatPanel_body')
         chat_area = HorizontalPanel()
         chat_area.setStyleName('chatArea')
         if type == 'group':
             self.occupants_list = OccupantsList()
             chat_area.add(self.occupants_list)
-        self.body.add(chat_area)
+        self.__body.add(chat_area)
         self.content = AbsolutePanel()
         self.content.setStyleName('chatContent')
         self.content_scroll = ScrollPanelWrapper(self.content)
         chat_area.add(self.content_scroll)
         chat_area.setCellWidth(self.content_scroll, '100%')
-        self.vpanel.add(self.header)
-        self.vpanel.setCellHeight(self.header, '1%')
-        self.vpanel.add(self.body)
+        self.vpanel.add(self.__body)
+        self.addStyleName('chatPanel')
         self.setWidget(self.vpanel)
-        self.setStyleName('chatPanel')
-        self.addClickListener(self)
 
-    def doDetachChildren(self):
+    """def doDetachChildren(self):
         #We need to force the use of a panel subclass method here,
         #for the same reason as doAttachChildren
         ScrollPanelWrapper.doDetachChildren(self)
@@ -527,11 +544,8 @@
     def doAttachChildren(self):
         #We need to force the use of a panel subclass method here, else
         #the event will not propagate to children
-        ScrollPanelWrapper.doAttachChildren(self)
+        ScrollPanelWrapper.doAttachChildren(self)"""
 
-    def onClick(self, sender):
-        self.host.select(self)
-    
     def setUserNick(self, nick):
         """Set the nick of the user, usefull for e.g. change the color of the user"""
         self.nick = nick
@@ -579,6 +593,7 @@
 
     def printMessage(self, from_jid, msg, timestamp=None):
         """Print message in chat window. Must be implemented by child class"""
+        print "print message:",msg
         _jid=JID(from_jid)
         nick = _jid.node if self.type=='one2one' else _jid.resource 
         mymess = _jid.resource == self.nick if self.type == "group" else  _jid.bare == self.host.whoami.bare #mymess = True if message comes from local user