Trees.py :  » Ajax » pyjamas » src » examples » infohierarchy » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » examples » infohierarchy » Trees.py
from pyjamas.ui.Tree import Tree
from pyjamas.ui.TreeItem import TreeItem
from pyjamas.ui.Composite import Composite
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HTML import HTML
from pyjamas import Window

from pyjamas.JSONService import JSONProxy

class Trees(Composite):
    def __init__(self):
        Composite.__init__(self)

        self.fProto = []
        self.fTree = Tree()
        
        self.fTree.addTreeListener(self)
        self.initWidget(self.fTree)
        self.remote = InfoServicePython()
        self.remote.index("", 1, self)
        
    def protoise_tree(self, data):

        if not data:
            return []

        res = []
        for i in range(len(data)):
            d = data[i]
            name = d[0]
            children = d[1]

            res.append(Proto(name, self.protoise_tree(children)))
        return res

    def create_tree(self, data):

        self.fProto = self.protoise_tree(data)
        
        for i in range(len(self.fProto)):
            p = self.fProto[i]
            p.pathify()
            self.createItem(p)
            self.fTree.addItem(p.item)

    def onRemoteResponse(self, response, request_info):
        if request_info.method == "index":
            self.create_tree(response)

    def onRemoteError(self, code, message, request_info):
        RootPanel().add(HTML("Server Error or Invalid Response: ERROR " + code + " - " + message))

    def onTreeItemSelected(self, item):
        pass
    
    def onTreeItemStateChanged(self, item):
        child = item.getChild(0)
        if hasattr(child, "isPendingItem"):
            item.removeItem(child)
        
            proto = item.getUserObject()
            for i in range(len(proto.children)):
                self.createItem(proto.children[i])
                item.addItem(proto.children[i].item)

    def createItem(self, proto):
        proto.item = TreeItem(proto.text)
        proto.item.setUserObject(proto)
        if len(proto.children) > 0:
            proto.item.addItem(PendingItem())


class Proto:
    def __init__(self, text, children=None):
        self.children = []
        self.item = None
        self.text = text
        self.root = '/'
        
        if children is not None:
            self.children = children

    def pathify(self):
        """ cascade setup of full path
        """

        for c in self.children:
            c.root = self.root + self.text + "/"
            c.pathify()


class PendingItem(TreeItem):
    def __init__(self):
        TreeItem.__init__(self, "Please wait...")

    def isPendingItem(self):
        return True


class InfoServicePython(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "/infoservice/EchoService.py",
                                    ["index"])


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.