Package library :: Package pyjamas :: Package builder :: Module Builder
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.builder.Builder

  1  # Copyright (C) 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  2  # Copyright (C) 2011 Janjaap Bos <janjaapbos@gmail.com> 
  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  # All event listeners with a tuple that comprises of the listener add 
 13  # function and the additional (to 'self') parameters that are expected 
 14  # for the listener. E.g., def onClick(self, sender): ... 
 15  # See also pyjamas.ui.MultiListener.MultiListener.combinations 
 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   
39 -class BuilderState(object):
40 - def __init__(self, builder, eventTarget):
41 self.builder = builder 42 self.eventTarget = eventTarget
43
44 -class Builder(object):
45
46 - def __init__(self, text=None):
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) # XMLFile only accepts str not unicode! 62 if text == self.builder_text: # don't redo the xml file if same 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 # create item with properties including weird ones 104 # which can't fit into the name value structure 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 #if parentInstance is not None: 122 # context = parentInstance.getIndexedChild(comp['index']) 123 # context.add(item.componentInstance) 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 # add child (by name) to item 143 cname = child[0]["id"] 144 setattr(item, cname, childitem) 145 146 # make the event target the recipient of all events 147 if eventTarget is not None and props.has_key("events"): 148 added_already = [] 149 #print props["events"] 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 #print "add listener", listener_name, fname 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 #left = frame.get("left") 170 #top = frame.get("top") 171 #if left is not None and top is not None: 172 # item.applicationVO.frame.setPopupPosition(left, top) 173 #if frame.get("visible", True): 174 # item.show() 175 #else: 176 # item.hide() 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
187 -class HTTPUILoader:
188 - def __init__(self, app):
189 self.app = app
190
191 - def load(self, xml_file):
192 HTTPRequest().asyncGet(xml_file, self)
193
194 - def onCompletion(self, text):
195 self.app.onUILoaded(text)
196
197 - def onError(self, text, code):
198 self.app.onUILoadError(text, code)
199
200 - def onTimeout(self, text):
201 self.app.onUILoadingTimeout(text)
202