1
2
3
4 from pyjamas.builder.XMLFile import XMLFile
5 from pyjamas import Factory
6 from pyjamas import ui
7 from pyjamas.ui.MultiListener import MultiListener
8 from pyjamas.HTTPRequest import HTTPRequest
9 from pyjamas.ui.Tooltip import TooltipListener
10
11
12
13
14
15
16 eventListeners = dict(
17 onClick = ("addClickListener", "sender"),
18 onDoubleClick = ("addDoubleClickListener", "sender"),
19 onChange = ("addChangeListener", "sender"),
20 onFocus = ("addFocusListener", "sender"),
21 onLostFocus = ("addFocusListener", "sender"),
22 onLoad = ("addLoadListener", "sender"),
23 onError = ("addLoadListener", "sender"),
24 onKeyDown = ("addKeyboardListener", "sender", "keycode", "modifiers"),
25 onKeyUp = ("addKeyboardListener", "sender", "keycode", "modifiers"),
26 onKeyPress = ("addKeyboardListener", "sender", "keycode", "modifiers"),
27 onMouseDown = ("addMouseListener", "sender", "x", "y"),
28 onMouseUp = ("addMouseListener", "sender", "x", "y"),
29 onMouseMove = ("addMouseListener", "sender", "x", "y"),
30 onMouseEnter = ("addMouseListener", "sender"),
31 onMouseLeave = ("addMouseListener", "sender"),
32 onScroll = ("addScrollListener", "sender", "row", "col"),
33 onCellClicked = ("addTableListener", "sender", "row", "col"),
34 onTabSelected = ("addTabListener", "sender", "tabIndex"),
35 onBeforeTabSelected = ("addTabListener", "sender", "tabIndex"),
36 onTreeItemSelected = ("addTreeListener", "sender"),
37 )
38
40 - def __init__(self, builder, eventTarget):
43
45
47 self.builder_text = None
48 self.setText(text)
49
50 - def setText(self, text):
51 if text is None:
52 self.widgets_by_name = {}
53 self.widget_instances = {}
54 self.widget_order = {}
55 self.widgets_by_class = {}
56 self.properties = None
57 self.components = None
58 self.builder_text = None
59 return
60
61 text = str(text)
62 if text == self.builder_text:
63 return
64
65 self.builder_text = text
66
67 self.widgets_by_name = {}
68 self.widget_instances = {}
69 self.widget_order = {}
70 self.widgets_by_class = {}
71 self.properties, self.components = XMLFile(text).parse()
72
73 - def createInstance(self, instancename,
74 eventTarget=None, targetItem=None, index=None):
75
76 widget_instances = {}
77 widgets_by_name = {}
78 widgets_by_class = {}
79 widget_order = []
80
81 def addItem(comp, props, childs, parentInstance, eventTarget):
82 klsname = comp['name']
83 modname = comp.get('module')
84 if modname is None:
85 modname = '.'.join(["pyjamas.ui", klsname])
86 kls = Factory.lookupClass('.'.join([modname, klsname]))
87
88 args = {}
89 wprops = {}
90 if props.has_key("common"):
91 wprops.update(props['common'])
92 if props.has_key("widget"):
93 wprops.update(props['widget'])
94 for n in kls._getProps():
95 name = n[ui.PROP_NAME]
96 if not wprops.has_key(name):
97 continue
98 fname = n[ui.PROP_FNAM]
99 if wprops[name] == '':
100 continue
101 args[fname] = wprops[name]
102
103
104
105 item = kls(**args)
106 if hasattr(item, "_setWeirdProps"):
107 item._setWeirdProps(wprops, BuilderState(self, eventTarget))
108
109 tooltip = wprops.get('tooltip')
110 if tooltip is not None:
111 item.addMouseListener(TooltipListener(tooltip))
112
113 identifier = comp['id']
114 widget_order.append(identifier)
115 widgets_by_name[identifier] = klsname
116 widget_instances[identifier] = item
117 l = widgets_by_class.get(klsname, [])
118 l.append(identifier)
119 widgets_by_class[klsname] = l
120
121
122
123
124 for (index, child) in enumerate(childs):
125 if not child[0].has_key("type") or child[0]["type"] is None:
126 continue
127 childitem = addItem(child[0], child[1], child[2], item,
128 eventTarget)
129 if childitem is None:
130 continue
131 print "childitem", childitem
132 item.addIndexedItem(child[0]["index"], childitem)
133 if not "elements" in props:
134 props["elements"] = {}
135 if not index in props["elements"]:
136 props["elements"][index] = {}
137
138 elemprops = props['elements'][index]
139 print "elemprops", childitem, item, elemprops
140 item.setElementProperties(childitem, elemprops)
141
142
143 cname = child[0]["id"]
144 setattr(item, cname, childitem)
145
146
147 if eventTarget is not None and props.has_key("events"):
148 added_already = []
149
150 for listener_name, listener_fn in props["events"].items():
151 if listener_name in added_already or not listener_fn:
152 continue
153 args = {}
154 args[listener_name] = listener_fn
155 fname = eventListeners[listener_name][0]
156 listener = MultiListener(eventTarget, **args)
157 setattr(item, "_%sListener" % fname, listener)
158
159 listen_add_fn = getattr(item, fname)
160 listen_add_fn(listener)
161 return item
162
163 for frame, props, childs in self.components:
164 if frame["id"] != instancename:
165 continue
166 if index is not None:
167 frame["index"] = index
168 item = addItem(frame, props, childs, targetItem, eventTarget)
169
170
171
172
173
174
175
176
177
178 self.widget_instances[instancename] = widget_instances
179 self.widgets_by_name[instancename] = widgets_by_name
180 self.widgets_by_class[instancename] = widgets_by_class
181 self.widget_order[instancename] = widget_order
182
183 return item
184 return None
185
186
190
191 - def load(self, xml_file):
193
195 self.app.onUILoaded(text)
196
198 self.app.onUILoadError(text, code)
199
201 self.app.onUILoadingTimeout(text)
202