Package muntjac :: Package event :: Module action_manager
[hide private]
[frames] | no frames]

Source Code for Module muntjac.event.action_manager

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  from muntjac.event import action 
 17   
 18  from muntjac.terminal.key_mapper import KeyMapper 
 19  from muntjac.event.shortcut_action import ShortcutAction 
 20   
 21   
22 -class ActionManager(action.IHandler, action.INotifier): #action.IContainer
23 """Notes: 24 25 Empties the keymapper for each repaint to avoid leaks; can cause problems 26 in the future if the client assumes key don't change. (if lazyloading, one 27 must not cache results) 28 """ 29
30 - def __init__(self, viewer=None):
31 #: List of action handlers 32 self.ownActions = None 33 34 #: List of action handlers 35 self.actionHandlers = None 36 37 #: Action mapper 38 self.actionMapper = None 39 40 self._clientHasActions = False 41 42 self.viewer = viewer
43 44
45 - def requestRepaint(self):
46 if self.viewer is not None: 47 self.viewer.requestRepaint()
48 49
50 - def setViewer(self, viewer):
51 if viewer == self.viewer: 52 return 53 54 if self.viewer is not None: 55 self.viewer.removeActionHandler(self) 56 57 self.requestRepaint() # this goes to the old viewer 58 if viewer is not None: 59 viewer.addActionHandler(self) 60 61 self.viewer = viewer 62 self.requestRepaint() # this goes to the new viewer
63 64
65 - def addAction(self, action):
66 if self.ownActions is None: 67 self.ownActions = set() 68 69 if self.ownActions.add(action): 70 self.requestRepaint()
71 72
73 - def removeAction(self, action):
74 if self.ownActions is not None: 75 if self.ownActions.remove(action): 76 self.requestRepaint()
77 78
79 - def addActionHandler(self, actionHandler):
80 if actionHandler == self: 81 # don't add the actionHandler to itself 82 return 83 84 if actionHandler is not None: 85 if self.actionHandlers is None: 86 self.actionHandlers = set() 87 88 if actionHandler not in self.actionHandlers: 89 self.actionHandlers.add(actionHandler) 90 self.requestRepaint()
91 92
93 - def removeActionHandler(self, actionHandler):
94 if (self.actionHandlers is not None 95 and actionHandler in self.actionHandlers): 96 if self.actionHandlers.remove(actionHandler): 97 self.requestRepaint() 98 99 if len(self.actionHandlers) == 0: 100 self.actionHandlers = None
101 102
103 - def removeAllActionHandlers(self):
104 if self.actionHandlers is not None: 105 self.actionHandlers = None 106 self.requestRepaint()
107 108 109
110 - def paintActions(self, actionTarget, paintTarget):
111 112 self.actionMapper = None 113 114 actions = set() 115 if self.actionHandlers is not None: 116 for handler in self.actionHandlers: 117 ac = handler.getActions(actionTarget, self.viewer) 118 if ac is not None: 119 for a in ac: 120 actions.add(a) 121 122 if self.ownActions is not None: 123 actions = actions.union(self.ownActions) 124 125 126 # Must repaint whenever there are actions OR if all actions have 127 # been removed but still exist on client side 128 if (len(actions) > 0) or self._clientHasActions: 129 self.actionMapper = KeyMapper() 130 131 paintTarget.addVariable(self.viewer, "action", "") 132 paintTarget.startTag("actions") 133 134 for a in actions: 135 paintTarget.startTag("action") 136 akey = self.actionMapper.key(a) 137 paintTarget.addAttribute("key", akey); 138 if a.getCaption() is not None: 139 paintTarget.addAttribute("caption", a.getCaption()) 140 141 if a.getIcon() is not None: 142 paintTarget.addAttribute("icon", a.getIcon()) 143 144 if isinstance(a, ShortcutAction): 145 sa = a 146 paintTarget.addAttribute("kc", sa.getKeyCode()) 147 modifiers = sa.getModifiers() 148 if modifiers is not None: 149 smodifiers = [None] * len(modifiers) 150 for i in range(len(modifiers)): 151 smodifiers[i] = str(modifiers[i]) 152 153 paintTarget.addAttribute("mk", smodifiers) 154 155 paintTarget.endTag("action") 156 157 paintTarget.endTag("actions") 158 159 # Update flag for next repaint so we know if we need to paint empty 160 # actions or not (must send actions is client had actions before and 161 # all actions were removed). 162 self._clientHasActions = len(actions) > 0
163 164
165 - def handleActions(self, variables, sender):
166 if 'action' in variables and self.actionMapper is not None: 167 key = variables.get('action') 168 a = self.actionMapper.get(key) 169 target = variables.get('actiontarget') 170 if a is not None: 171 self.handleAction(a, sender, target)
172 173
174 - def getActions(self, target, sender):
175 actions = set() 176 if self.ownActions is not None: 177 for a in self.ownActions: 178 actions.add(a) 179 180 if self.actionHandlers is not None: 181 for h in self.actionHandlers: 182 as_ = h.getActions(target, sender) 183 if as_ is not None: 184 for a in as_: 185 actions.add(a) 186 187 return list(actions)
188 189
190 - def handleAction(self, a, sender, target):
191 if self.actionHandlers is not None: 192 arry = list(self.actionHandlers) 193 for handler in arry: 194 handler.handleAction(a, sender, target) 195 196 if ((self.ownActions is not None) 197 and (a in self.ownActions) 198 and isinstance(a, action.IListener)): 199 a.handleAction(sender, target)
200