View Javadoc

1   /**
2    * This file Copyright (c) 2013-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.workbench.tree.drop;
35  
36  import static info.magnolia.jcr.util.NodeUtil.*;
37  
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
40  import info.magnolia.ui.workbench.tree.HierarchicalJcrContainer;
41  import info.magnolia.ui.workbench.tree.MoveHandler;
42  import info.magnolia.ui.workbench.tree.MoveLocation;
43  
44  import javax.jcr.Item;
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.RepositoryException;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.event.DataBoundTransferable;
53  import com.vaadin.event.Transferable;
54  import com.vaadin.event.dd.DragAndDropEvent;
55  import com.vaadin.event.dd.DropHandler;
56  import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
57  import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion;
58  import com.vaadin.shared.ui.dd.VerticalDropLocation;
59  import com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails;
60  import com.vaadin.ui.TreeTable;
61  
62  /**
63   * Generic implementation of {@link DropHandler} handling basic {@link Item}.
64   * This Handler implementation uses constrained defined in
65   * {@link DropConstraint} implemented class.
66   */
67  public class TreeViewDropHandler implements MoveHandler, DropHandler {
68  
69      private final Logger log = LoggerFactory.getLogger(getClass());
70  
71      private TreeTable tree;
72      private DropConstraint constraint;
73      private AcceptCriterion serverSideCriterion;
74  
75      public TreeViewDropHandler() {
76          createAcceptCriterion();
77      }
78  
79      public TreeViewDropHandler(TreeTable tree, DropConstraint constraint) {
80          this.tree = tree;
81          this.constraint = constraint;
82          createAcceptCriterion();
83      }
84  
85      @Override
86      public void drop(DragAndDropEvent dropEvent) {
87          // Called whenever a drop occurs on the component
88  
89          // Make sure the drag source is the same tree
90          Transferable t = dropEvent.getTransferable();
91  
92          // First acceptance criteria.
93          // Make sure the drag source is the same tree
94          if (t.getSourceComponent() != tree || !(t instanceof DataBoundTransferable)) {
95              return;
96          }
97  
98          AbstractSelectTargetDetails target = (AbstractSelectTargetDetails) dropEvent.getTargetDetails();
99          // Get ids of the dragged item and the target item
100         Object sourceItemId = getSourceId(dropEvent);
101         Object targetItemId = target.getItemIdOver();
102         // On which side of the target the item was dropped
103         VerticalDropLocation location = target.getDropLocation();
104 
105         if (location == null) {
106             log.debug("DropLocation is null. Do nothing.");
107             return;
108         }
109 
110         moveNode(sourceItemId, targetItemId, location);
111 
112     }
113 
114     /**
115      * Accept dragged Elements.
116      */
117     @Override
118     public AcceptCriterion getAcceptCriterion() {
119         return serverSideCriterion;
120     }
121 
122     /**
123      * Move a node within a tree onto, above or below another node depending on
124      * the drop location.
125      *
126      * @param sourceItemId
127      * id of the item to move
128      * @param targetItemId
129      * id of the item onto which the source node should be moved
130      * @param location
131      * VerticalDropLocation indicating where the source node was
132      * dropped relative to the target node
133      */
134     private void moveNode(Object sourceItemId, Object targetItemId, VerticalDropLocation location) {
135         log.debug("DropLocation: {}", location.name());
136         // Get Item from tree
137         HierarchicalJcrContainer container = (HierarchicalJcrContainer) tree.getContainerDataSource();
138         JcrItemAdapter sourceItem = (JcrItemAdapter) container.getItem(sourceItemId);
139         JcrItemAdapter targetItem = (JcrItemAdapter) container.getItem(targetItemId);
140 
141         // Sorting goes as
142         // - If dropped ON a node, we append it as a child
143         // - If dropped on the TOP part of a node, we move/add it before
144         // the node
145         // - If dropped on the BOTTOM part of a node, we move/add it
146         // after the node
147 
148         if (location == VerticalDropLocation.MIDDLE) {
149             if (constraint.allowedAsChild(sourceItem, targetItem)) {
150                 // move first in the container
151                 moveItem(sourceItem, targetItem, MoveLocation.INSIDE);
152                 container.setParent(sourceItemId, targetItemId);
153             }
154         } else {
155             Object parentId = container.getParent(targetItemId);
156             if (location == VerticalDropLocation.TOP) {
157                 if (parentId != null && constraint.allowedBefore(sourceItem, targetItem)) {
158                     // move first in the container
159                     moveItem(sourceItem, targetItem, MoveLocation.BEFORE);
160                     container.setParent(sourceItemId, parentId);
161                 }
162             } else if (location == VerticalDropLocation.BOTTOM) {
163                 if (parentId != null && constraint.allowedAfter(sourceItem, targetItem)) {
164                     moveItem(sourceItem, targetItem, MoveLocation.AFTER);
165                     container.setParent(sourceItemId, parentId);
166                 }
167             }
168         }
169     }
170 
171     /**
172      * Create a serverSide {@link AcceptCriterion} based on the
173      * {@link DropConstraint} implementation.
174      */
175     private void createAcceptCriterion() {
176         serverSideCriterion = new ServerSideCriterion() {
177 
178             @Override
179             public boolean accept(DragAndDropEvent dragEvent) {
180                 Object sourceItemId = getSourceId(dragEvent);
181                 HierarchicalJcrContainer container = (HierarchicalJcrContainer) tree.getContainerDataSource();
182                 JcrItemAdapter sourceItem = (JcrItemAdapter) container.getItem(sourceItemId);
183 
184                 return constraint.allowedToMove(sourceItem);
185             }
186         };
187 
188     }
189 
190     private Object getSourceId(DragAndDropEvent dropEvent) {
191         Transferable t = dropEvent.getTransferable();
192         return ((DataBoundTransferable) t).getItemId();
193     }
194 
195     /**
196      * Performs check for moving items. Evaluates true when first node is of type Node and source and target are not same.
197      */
198     public boolean basicMoveCheck(Item source, Item target) {
199         try {
200             // target must be node, to allow moving in
201             if (!target.isNode()) {
202                 return false;
203             }
204             // Source and origin are the same... do nothing
205             if (target.getPath().equals(source.getPath()) && target.getSession().getWorkspace().getName().equals(source.getSession().getWorkspace().getName())) {
206                 return false;
207             }
208             // Source can not be a child of target.
209             return !NodeUtil.isSame((Node) target, source.getParent());
210         } catch (RepositoryException re) {
211             log.debug("Cannot determine whether drag and drop is possible due to: {}", re.getMessage(), re);
212             return false;
213         }
214     }
215 
216     /**
217      * Performs move of the source node into target node or next to target node depending on the value of MoveLocation.
218      * Move is performed in session-write mode and as such requires explicit call to session.save() after performing the operation.
219      */
220     public void moveNode(Node nodeToMove, Node newParent, MoveLocation location) throws RepositoryException {
221         if (!basicMoveCheck(nodeToMove, newParent)) {
222             return;
223         }
224         switch (location) {
225         case INSIDE:
226             String newPath = combinePathAndName(newParent.getPath(), nodeToMove.getName());
227             nodeToMove.getSession().move(nodeToMove.getPath(), newPath);
228             break;
229         case BEFORE:
230             moveNodeBefore(nodeToMove, newParent);
231             break;
232         case AFTER:
233             moveNodeAfter(nodeToMove, newParent);
234             break;
235         }
236     }
237 
238     /**
239      * Performs move of the source node or property into target node or next to target node depending on the value of MoveLocation.
240      * This method will persist move by calling session.save() explicitly. And will return true/false depending on whether move was successful or not.
241      */
242     @Override
243     public boolean moveItem(com.vaadin.data.Item source, com.vaadin.data.Item target, MoveLocation location) {
244         Item sourceItem = ((JcrItemAdapter) source).getJcrItem();
245         Item targetItem = ((JcrItemAdapter) target).getJcrItem();
246         if (!basicMoveCheck(sourceItem, targetItem)) {
247             return false;
248         }
249         try {
250             final Node targetNode = (Node) targetItem;
251             if (sourceItem.isNode()) {
252                 moveNode((Node) sourceItem, targetNode, location);
253             } else {
254                 NodeUtil.moveProperty((Property) sourceItem, targetNode);
255             }
256             sourceItem.getSession().save();
257             return true;
258         } catch (RepositoryException re) {
259             log.debug("Cannot execute drag and drop action", re);
260             return false;
261         }
262     }
263 
264     @Override
265     public DropHandler asDropHandler() {
266         return this;
267     }
268 }