1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.participant;
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;
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.Logger;
28 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;
29 import org.simantics.g2d.diagram.DiagramHints;
30 import org.simantics.g2d.diagram.DiagramMutator;
31 import org.simantics.g2d.diagram.DiagramUtils;
32 import org.simantics.g2d.diagram.IDiagram;
33 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
34 import org.simantics.g2d.dnd.DnDHints;
35 import org.simantics.g2d.dnd.ElementClassDragItem;
36 import org.simantics.g2d.dnd.IDnDContext;
37 import org.simantics.g2d.dnd.IDragItem;
38 import org.simantics.g2d.dnd.IDropTargetParticipant;
39 import org.simantics.g2d.element.ElementClass;
40 import org.simantics.g2d.element.ElementUtils;
41 import org.simantics.g2d.element.IElement;
42 import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
43 import org.simantics.ui.dnd.LocalObjectTransfer;
44 import org.simantics.ui.dnd.LocalObjectTransferable;
45 import org.simantics.utils.datastructures.Callback;
46 import org.simantics.utils.logging.TimeLogger;
48 abstract public class PopulateSelectionDropParticipant extends AbstractDiagramParticipant implements IDropTargetParticipant {
50 public abstract List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException;
52 protected GraphToDiagramSynchronizer synchronizer;
54 public PopulateSelectionDropParticipant(GraphToDiagramSynchronizer synchronizer) {
55 this.synchronizer = synchronizer;
59 public void dragEnter(DropTargetDragEvent dtde, final IDnDContext dp) {
61 Transferable tr = dtde.getTransferable();
63 if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
65 Session session = synchronizer.getSession();
67 // System.out.println("joo");
70 // This must be done to have SWT transfer set the source data
73 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);
74 //System.out.println("GOT FROM AWT: " + obj);
77 if (!(obj instanceof IStructuredSelection)) {
78 obj = LocalObjectTransfer.getTransfer().getObject();
79 //System.out.println("GOT FROM SWT: " + obj);
82 if (obj instanceof IStructuredSelection) {
84 IStructuredSelection sel = (IStructuredSelection) obj;
87 List<ElementClassDragItem> items = getElements(session, sel);
89 for(ElementClassDragItem item : items) {
93 dp.getHints().setHint(DnDHints.KEY_DND_GRID_COLUMNS, Integer.valueOf(1));
99 } catch (UnsupportedFlavorException e) {
100 Logger.defaultLogError(e);
101 } catch (IOException e) {
102 Logger.defaultLogError(e);
103 } catch (DatabaseException e) {
104 Logger.defaultLogError(e);
109 dtde.acceptDrag(DnDConstants.ACTION_COPY);
114 public void drop(DropTargetDropEvent dtde, final IDnDContext dp) {
115 TimeLogger.resetTimeAndLog(getClass(), "drop");
117 final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
121 DiagramUtils.mutateDiagram(d, m -> {
122 IDragItem items[] = dp.toArray();
124 for (IDragItem i : items) {
125 if (!(i instanceof ElementClassDragItem))
128 ElementClassDragItem res = (ElementClassDragItem) i;
129 ElementClass ec = res.getElementClass();
131 Point2D pos = dp.getItemPosition(i);
132 // System.out.println(pos);
133 assert (pos != null);
135 IElement element = m.newElement(ec);
136 element.setHints(res.getHintContext().getHints());
138 ISnapAdvisor snapAdvisor = getContext().getHintStack().getHint(DiagramHints.SNAP_ADVISOR);
139 if(snapAdvisor != null)
140 snapAdvisor.snap(pos);
141 ElementUtils.setPos(element, pos);
143 // Remove only the drag items we've processed.
148 synchronizer.getCanvasContext().getContentContext().setDirty();
153 public void dragOver(DropTargetDragEvent dtde, IDnDContext dp) {
157 public void dragExit(DropTargetEvent dte, IDnDContext dp) {
161 public void dropActionChanged(DropTargetDragEvent dtde, IDnDContext dp) {
165 public int getAllowedOps() {
170 public double getPriority() {