Package muntjac :: Package terminal :: Package gwt :: Package server :: Module drag_and_drop_service
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.gwt.server.drag_and_drop_service

  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  import logging 
 17   
 18  from muntjac.event.dd.target_details_impl import TargetDetailsImpl 
 19  from muntjac.terminal.variable_owner import IVariableOwner 
 20  from muntjac.terminal.gwt.server.json_paint_target import JsonPaintTarget 
 21  from muntjac.event.dd.drag_and_drop_event import DragAndDropEvent 
 22  from muntjac.event.transferable_impl import TransferableImpl 
 23  from muntjac.event.dd.drop_target import IDropTarget 
 24  from muntjac.event.dd.drag_source import IDragSource 
 25  from muntjac.terminal.gwt.client.ui.dd.v_drag_and_drop_manager import DragEventType 
 26   
 27   
 28  logger = logging.getLogger(__name__) 
 29   
 30   
31 -class DragAndDropService(IVariableOwner):
32
33 - def __init__(self, manager):
34 self._manager = manager 35 36 self._lastVisitId = None 37 self._lastVisitAccepted = False 38 self._dragEvent = None 39 self._acceptCriterion = None
40 41
42 - def changeVariables(self, source, variables):
43 owner = variables.get('dhowner') 44 45 # Validate drop handler owner 46 if not isinstance(owner, IDropTarget): 47 logger.critical('DropHandler owner ' + owner 48 + ' must implement IDropTarget') 49 return 50 51 # owner cannot be null here 52 53 dropTarget = owner 54 self._lastVisitId = variables.get('visitId') 55 56 # request may be dropRequest or request during drag operation 57 # (commonly dragover or dragenter) 58 dropRequest = self.isDropRequest(variables) 59 if dropRequest: 60 self.handleDropRequest(dropTarget, variables) 61 else: 62 self.handleDragRequest(dropTarget, variables)
63 64
65 - def handleDropRequest(self, dropTarget, variables):
66 """Handles a drop request from the VDragAndDropManager. 67 """ 68 dropHandler = dropTarget.getDropHandler() 69 if dropHandler is None: 70 # No dropHandler returned so no drop can be performed. 71 logger.info('IDropTarget.getDropHandler() returned null ' 72 'for owner: ' + dropTarget) 73 return 74 75 # Construct the Transferable and the DragDropDetails for the drop 76 # operation based on the info passed from the client widgets (drag 77 # source for Transferable, drop target for DragDropDetails). 78 transferable = self.constructTransferable(dropTarget, variables) 79 dropData = self.constructDragDropDetails(dropTarget, variables) 80 81 dropEvent = DragAndDropEvent(transferable, dropData) 82 83 if dropHandler.getAcceptCriterion().accept(dropEvent): 84 dropHandler.drop(dropEvent)
85 86
87 - def handleDragRequest(self, dropTarget, variables):
88 """Handles a drag/move request from the VDragAndDropManager. 89 """ 90 self._lastVisitId = variables.get('visitId') 91 92 self._acceptCriterion = \ 93 dropTarget.getDropHandler().getAcceptCriterion() 94 95 # Construct the Transferable and the DragDropDetails for the drag 96 # operation based on the info passed from the client widgets (drag 97 # source for Transferable, current target for DragDropDetails). 98 transferable = self.constructTransferable(dropTarget, variables) 99 dragDropDetails = self.constructDragDropDetails(dropTarget, variables) 100 101 self._dragEvent = DragAndDropEvent(transferable, dragDropDetails) 102 103 self._lastVisitAccepted = self._acceptCriterion.accept(self._dragEvent)
104 105
106 - def constructDragDropDetails(self, dropTarget, variables):
107 """Construct DragDropDetails based on variables from client drop 108 target. Uses DragDropDetailsTranslator if available, otherwise a 109 default DragDropDetails implementation is used. 110 """ 111 rawDragDropDetails = variables.get('evt') 112 113 dropData = dropTarget.translateDropTargetDetails(rawDragDropDetails) 114 115 if dropData is None: 116 # Create a default DragDropDetails with all the raw variables 117 dropData = TargetDetailsImpl(rawDragDropDetails, dropTarget) 118 119 return dropData
120 121
122 - def isDropRequest(self, variables):
123 return self.getRequestType(variables) == DragEventType.DROP
124 125
126 - def getRequestType(self, variables):
127 typ = int( variables.get('type') ) 128 return DragEventType.values()[typ]
129 130
131 - def constructTransferable(self, dropHandlerOwner, variables):
132 sourceComponent = variables.get('component') 133 134 variables = variables.get('tra') 135 136 transferable = None 137 if (sourceComponent is not None 138 and isinstance(sourceComponent, IDragSource)): 139 transferable = sourceComponent.getTransferable(variables) 140 141 if transferable is None: 142 transferable = TransferableImpl(sourceComponent, variables) 143 144 return transferable
145 146
147 - def isEnabled(self):
148 return True
149 150
151 - def isImmediate(self):
152 return True
153 154
155 - def printJSONResponse(self, outWriter):
156 if self._isDirty(): 157 outWriter.write(', \"dd\":') 158 jsonPaintTarget = JsonPaintTarget(self._manager, outWriter, False) 159 jsonPaintTarget.startTag('dd') 160 jsonPaintTarget.addAttribute('visitId', self._lastVisitId) 161 if self._acceptCriterion is not None: 162 jsonPaintTarget.addAttribute('accepted', 163 self._lastVisitAccepted) 164 self._acceptCriterion.paintResponse(jsonPaintTarget) 165 jsonPaintTarget.endTag('dd') 166 jsonPaintTarget.close() 167 self._lastVisitId = -1 168 self._lastVisitAccepted = False 169 self._acceptCriterion = None 170 self._dragEvent = None
171 172
173 - def _isDirty(self):
174 if self._lastVisitId > 0: 175 return True 176 return False
177