PageRenderTime 224ms CodeModel.GetById 110ms app.highlight 10ms RepoModel.GetById 101ms app.codeStats 0ms

/examples/kitchensink/Trees.py

http://pyjamas.googlecode.com/
Python | 122 lines | 104 code | 18 blank | 0 comment | 7 complexity | 20e2ce01d70bbca1374eca190fa46ef2 MD5 | raw file
  1from Sink import Sink, SinkInfo
  2from ui import Tree, TreeItem
  3
  4class Trees(Sink):
  5    def __init__(self):
  6        self.fProto = [
  7            Proto("Beethoven", [
  8                Proto("Concertos", [
  9                    Proto("No. 1 - C"), 
 10                    Proto("No. 2 - B-Flat Major"), 
 11                    Proto("No. 3 - C Minor"), 
 12                    Proto("No. 4 - G Major"), 
 13                    Proto("No. 5 - E-Flat Major")
 14                ]),
 15                Proto("Quartets", [
 16                    Proto("Six String Quartets"), 
 17                    Proto("Three String Quartets"), 
 18                    Proto("Grosse Fugue for String Quartets")
 19                ]),
 20                Proto("Sonatas", [
 21                    Proto("Sonata in A Minor"), 
 22                    Proto("Sonata in F Major")
 23                ]),
 24                Proto("Symphonies", [
 25                    Proto("No. 1 - C Major"), 
 26                    Proto("No. 2 - D Major"), 
 27                    Proto("No. 3 - E-Flat Major"), 
 28                    Proto("No. 4 - B-Flat Major"), 
 29                    Proto("No. 5 - C Minor"), 
 30                    Proto("No. 6 - F Major"), 
 31                    Proto("No. 7 - A Major"), 
 32                    Proto("No. 8 - F Major"), 
 33                    Proto("No. 9 - D Minor")
 34                ])
 35            ]),
 36        
 37            Proto("Brahms", [
 38                Proto("Concertos", [
 39                    Proto("Violin Concerto"),
 40                    Proto("Double Concerto - A Minor"),
 41                    Proto("Piano Concerto No. 1 - D Minor"),
 42                    Proto("Piano Concerto No. 2 - B-Flat Major")
 43                ]),
 44                Proto("Quartets", [
 45                    Proto("Piano Quartet No. 1 - G Minor"),
 46                    Proto("Piano Quartet No. 2 - A Major"),
 47                    Proto("Piano Quartet No. 3 - C Minor"),
 48                    Proto("String Quartet No. 3 - B-Flat Minor")
 49                ]),
 50                Proto("Sonatas", [
 51                    Proto("Two Sonatas for Clarinet - F Minor"),
 52                    Proto("Two Sonatas for Clarinet - E-Flat Major")
 53                ]),
 54                Proto("Symphonies", [
 55                    Proto("No. 1 - C Minor"),
 56                    Proto("No. 2 - D Minor"),
 57                    Proto("No. 3 - F Major"),
 58                    Proto("No. 4 - E Minor")
 59                ])      
 60            ]),
 61        
 62            Proto("Mozart", [
 63                Proto("Concertos", [
 64                    Proto("Piano Concerto No. 12"),
 65                    Proto("Piano Concerto No. 17"),
 66                    Proto("Clarinet Concerto"),
 67                    Proto("Violin Concerto No. 5"),
 68                    Proto("Violin Concerto No. 4")
 69                ]),
 70            ])
 71        ]
 72
 73        self.fTree = Tree()
 74        
 75        for i in range(len(self.fProto)):
 76            self.createItem(self.fProto[i])
 77            self.fTree.addItem(self.fProto[i].item)
 78        
 79        self.fTree.addTreeListener(self)
 80        self.initWidget(self.fTree)
 81        
 82    def onTreeItemSelected(self, item):
 83        pass
 84    
 85    def onTreeItemStateChanged(self, item):
 86        child = item.getChild(0)
 87        if hasattr(child, "isPendingItem"):
 88            item.removeItem(child)
 89        
 90            proto = item.getUserObject()
 91            for i in range(len(proto.children)):
 92                self.createItem(proto.children[i])
 93                item.addItem(proto.children[i].item)
 94
 95    def createItem(self, proto):
 96        proto.item = TreeItem(proto.text)
 97        proto.item.setUserObject(proto)
 98        if len(proto.children) > 0:
 99            proto.item.addItem(PendingItem())
100
101
102class Proto:
103    def __init__(self, text, children=None):
104        self.children = []
105        self.item = None
106        self.text = text
107        
108        if children != None:
109            self.children = children
110
111
112class PendingItem(TreeItem):
113    def __init__(self):
114        TreeItem.__init__(self, "Please wait...")
115
116    def isPendingItem(self):
117        return True
118
119
120def init():
121    text="GWT has a built-in <code>Tree</code> widget. The tree is focusable and has keyboard support as well."
122    return SinkInfo("Trees", text, Trees)