Removed unnecessary dependencies on org.apache.log4j
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / PopulateSelectionDropParticipant.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.participant;
13
14 import java.awt.datatransfer.Transferable;
15 import java.awt.datatransfer.UnsupportedFlavorException;
16 import java.awt.dnd.DnDConstants;
17 import java.awt.dnd.DropTargetDragEvent;
18 import java.awt.dnd.DropTargetDropEvent;
19 import java.awt.dnd.DropTargetEvent;
20 import java.awt.geom.Point2D;
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.simantics.db.Session;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;
28 import org.simantics.g2d.diagram.DiagramHints;
29 import org.simantics.g2d.diagram.DiagramUtils;
30 import org.simantics.g2d.diagram.IDiagram;
31 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
32 import org.simantics.g2d.dnd.DnDHints;
33 import org.simantics.g2d.dnd.ElementClassDragItem;
34 import org.simantics.g2d.dnd.IDnDContext;
35 import org.simantics.g2d.dnd.IDragItem;
36 import org.simantics.g2d.dnd.IDropTargetParticipant;
37 import org.simantics.g2d.element.ElementClass;
38 import org.simantics.g2d.element.ElementUtils;
39 import org.simantics.g2d.element.IElement;
40 import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
41 import org.simantics.ui.dnd.LocalObjectTransfer;
42 import org.simantics.ui.dnd.LocalObjectTransferable;
43 import org.simantics.utils.logging.TimeLogger;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 abstract public class PopulateSelectionDropParticipant extends AbstractDiagramParticipant implements IDropTargetParticipant {
48
49         private static final Logger LOGGER = LoggerFactory.getLogger(PopulateSelectionDropParticipant.class);
50
51         public abstract List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException;
52         
53         protected GraphToDiagramSynchronizer synchronizer;
54
55         public PopulateSelectionDropParticipant(GraphToDiagramSynchronizer synchronizer) {
56                 this.synchronizer = synchronizer;
57         }
58
59         @Override
60         public void dragEnter(DropTargetDragEvent dtde, final IDnDContext dp) {
61
62                 Transferable tr = dtde.getTransferable();
63
64                 if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
65
66                         Session session = synchronizer.getSession();
67
68                         // System.out.println("joo");
69                         Object obj = null;
70
71                         // This must be done to have SWT transfer set the source data
72                         try {
73                                 
74                                 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);
75                                 //System.out.println("GOT FROM AWT: " + obj);
76
77                                 // Check SWT
78                                 if (!(obj instanceof IStructuredSelection)) {
79                                         obj = LocalObjectTransfer.getTransfer().getObject();
80                                         //System.out.println("GOT FROM SWT: " + obj);
81                                 }
82
83                                 if (obj instanceof IStructuredSelection) {
84
85                                         IStructuredSelection sel = (IStructuredSelection) obj;
86                                         if (!sel.isEmpty()) {
87                                                 
88                                                 List<ElementClassDragItem> items = getElements(session, sel);
89
90                                                 for(ElementClassDragItem item : items) {
91                                                         dp.add(item);
92                                                 }
93
94                                                 dp.getHints().setHint(DnDHints.KEY_DND_GRID_COLUMNS, Integer.valueOf(1));
95
96                                         }
97
98                                 }
99
100                         } catch (DatabaseException | IOException | UnsupportedFlavorException e) {
101                                 LOGGER.error("Unexpected failure", e);
102                         }
103
104                 }
105
106                 dtde.acceptDrag(DnDConstants.ACTION_COPY);
107
108         }
109
110         @Override
111         public void drop(DropTargetDropEvent dtde, final IDnDContext dp) {
112             TimeLogger.resetTimeAndLog(getClass(), "drop");
113             
114         final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
115         if (d == null)
116             return;
117
118         DiagramUtils.mutateDiagram(d, m -> {
119             IDragItem items[] = dp.toArray();
120
121             for (IDragItem i : items) {
122                 if (!(i instanceof ElementClassDragItem))
123                     continue;
124
125                 ElementClassDragItem res = (ElementClassDragItem) i;
126                 ElementClass ec = res.getElementClass();
127
128                 Point2D pos = dp.getItemPosition(i);
129                 // System.out.println(pos);
130                 assert (pos != null);
131
132                 IElement element = m.newElement(ec);
133                 element.setHints(res.getHintContext().getHints());
134
135                 ISnapAdvisor snapAdvisor = getContext().getHintStack().getHint(DiagramHints.SNAP_ADVISOR);
136                 if(snapAdvisor != null)
137                     snapAdvisor.snap(pos);
138                 ElementUtils.setPos(element, pos);
139
140                 // Remove only the drag items we've processed.
141                 dp.remove(i);
142             }
143         });
144
145         synchronizer.getCanvasContext().getContentContext().setDirty();
146                 
147         }
148
149         @Override
150         public void dragOver(DropTargetDragEvent dtde, IDnDContext dp) {
151         }
152         
153         @Override
154         public void dragExit(DropTargetEvent dte, IDnDContext dp) {
155         }
156         
157         @Override
158         public void dropActionChanged(DropTargetDragEvent dtde, IDnDContext dp) {
159         }
160
161         @Override
162         public int getAllowedOps() {
163                 return 0;
164         }
165         
166     @Override
167     public double getPriority() {
168         return 9.1;
169     }
170         
171 }