Package pyjamas :: Package ui :: Module BuilderWidget
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.BuilderWidget

  1  """ 
  2  Pyjamas UI BuilderWidget: takes a PyJsGlade builder spec 
  3  and creates a widget. 
  4   
  5  Copyright (C) 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  6   
  7  Create a BuilderWidget as follows:: 
  8   
  9      from pyjamas.builder.Builder import Builder 
 10      from pyjamas.ui.BuilderWidget import BuilderWidget 
 11   
 12  either:: 
 13   
 14      class ApplicationEventReceivingClassWhatever: 
 15   
 16          def onSomeRandomClickThing(self, sender): 
 17              print "some random widget was clicked, it was this one:", sender 
 18   
 19          app = ApplicationEventReceivingClassWhatever() 
 20          b = Builder() 
 21          xml = "<?xml .... ?><pyjsglade> .... </pyjsglade>" 
 22          bw = BuilderWidget(Builder=b, 
 23                             EventReceiver=app, 
 24                             BuilderText=xml_file, 
 25                             InstanceName="WidgetListedInXmlFile") 
 26   
 27  or:: 
 28   
 29      app = ApplicationEventReceivingClassWhatever() 
 30      xml = "<?xml .... ?><pyjsglade> .... </pyjsglade>" 
 31      b = Builder(xml) 
 32      bw = BuilderWidget(Builder=b, 
 33                         EventReceiver=app, 
 34                         InstanceName="WidgetListedInXmlFile") 
 35   
 36  or:: 
 37   
 38      class BuilderWidgetWithIntegratedEventHandling(BuilderWidget): 
 39          def onSomeRandomClickThing(self, sender): 
 40              print "some random widget was clicked, it was this one:", sender 
 41   
 42          b = Builder(xml) 
 43          bw = BuilderWidgetWithIntegratedEventHandling(Builder=b, 
 44                             InstanceName="WidgetListedInXmlFile") 
 45   
 46  or:: 
 47   
 48      b = Builder() 
 49      bw = BuilderWidgetWithIntegratedEventHandling(Builder=b, 
 50                         BuilderText=xml_file, 
 51                         InstanceName="WidgetListedInXmlFile") 
 52   
 53  """ 
 54   
 55  from pyjamas.ui.Composite import Composite 
 56   
 57   
58 -class BuilderWidget(Composite):
59
60 - def __init__(self, **kwargs):
61 62 self.b = None 63 self.text = None 64 self.instance_name = None 65 self.event_receiver = None 66 Composite.__init__(self, **kwargs)
67
68 - def setBuilderText(self, text):
69 self.text = text 70 self.autoCreateInstance()
71
72 - def setBuilder(self, builder):
73 self.b = builder 74 self.autoCreateInstance()
75
76 - def setEventReceiver(self, event_receiver):
77 """ sets the instance where the events named in the builder 78 will be received (callbacks called). 79 passing in None will set the event receiver to be this 80 widget. 81 """ 82 self.event_receiver = event_receiver or self 83 self.autoCreateInstance()
84
85 - def setInstanceName(self, instance_name):
86 self.instance_name = instance_name 87 self.autoCreateInstance()
88
89 - def autoCreateInstance(self):
90 """ when all the required arguments have been set, the 91 widget instance will be created. it's done this way 92 because **kwargs goes through to pyjamas.ui.Applier, 93 and the order in which the setXXX functions will be called 94 cannot be determined or guaranteed (kwargs is a dictionary). 95 """ 96 if self.b and self.text: 97 self.b.setText(self.text) 98 if not self.b or not self.instance_name or not self.event_receiver: 99 return 100 if not self.b.builder_text: 101 return 102 widget = self.b.createInstance(self.instance_name, self.event_receiver) 103 self.initWidget(widget)
104