]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/PopulateSelectionDropParticipant.java
Paster.getGraph() method for overriding classes.
[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.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;
47
48 abstract public class PopulateSelectionDropParticipant extends AbstractDiagramParticipant implements IDropTargetParticipant {
49
50         public abstract List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException;
51         
52         protected GraphToDiagramSynchronizer synchronizer;
53
54         public PopulateSelectionDropParticipant(GraphToDiagramSynchronizer synchronizer) {
55                 this.synchronizer = synchronizer;
56         }
57
58         @Override
59         public void dragEnter(DropTargetDragEvent dtde, final IDnDContext dp) {
60
61                 Transferable tr = dtde.getTransferable();
62
63                 if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
64
65                         Session session = synchronizer.getSession();
66
67                         // System.out.println("joo");
68                         Object obj = null;
69
70                         // This must be done to have SWT transfer set the source data
71                         try {
72                                 
73                                 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);
74                                 //System.out.println("GOT FROM AWT: " + obj);
75
76                                 // Check SWT
77                                 if (!(obj instanceof IStructuredSelection)) {
78                                         obj = LocalObjectTransfer.getTransfer().getObject();
79                                         //System.out.println("GOT FROM SWT: " + obj);
80                                 }
81
82                                 if (obj instanceof IStructuredSelection) {
83
84                                         IStructuredSelection sel = (IStructuredSelection) obj;
85                                         if (!sel.isEmpty()) {
86                                                 
87                                                 List<ElementClassDragItem> items = getElements(session, sel);
88
89                                                 for(ElementClassDragItem item : items) {
90                                                         dp.add(item);
91                                                 }
92
93                                                 dp.getHints().setHint(DnDHints.KEY_DND_GRID_COLUMNS, Integer.valueOf(1));
94
95                                         }
96
97                                 }
98
99                         } catch (UnsupportedFlavorException e) {
100                                 Logger.defaultLogError(e);
101                         } catch (IOException e) {
102                                 Logger.defaultLogError(e);
103                         } catch (DatabaseException e) {
104                                 Logger.defaultLogError(e);
105                         }
106
107                 }
108
109                 dtde.acceptDrag(DnDConstants.ACTION_COPY);
110
111         }
112
113         @Override
114         public void drop(DropTargetDropEvent dtde, final IDnDContext dp) {
115             TimeLogger.resetTimeAndLog(getClass(), "drop");
116             
117         final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
118         if (d == null)
119             return;
120
121         DiagramUtils.mutateDiagram(d, m -> {
122             IDragItem items[] = dp.toArray();
123
124             for (IDragItem i : items) {
125                 if (!(i instanceof ElementClassDragItem))
126                     continue;
127
128                 ElementClassDragItem res = (ElementClassDragItem) i;
129                 ElementClass ec = res.getElementClass();
130
131                 Point2D pos = dp.getItemPosition(i);
132                 // System.out.println(pos);
133                 assert (pos != null);
134
135                 IElement element = m.newElement(ec);
136                 element.setHints(res.getHintContext().getHints());
137
138                 ISnapAdvisor snapAdvisor = getContext().getHintStack().getHint(DiagramHints.SNAP_ADVISOR);
139                 if(snapAdvisor != null)
140                     snapAdvisor.snap(pos);
141                 ElementUtils.setPos(element, pos);
142
143                 // Remove only the drag items we've processed.
144                 dp.remove(i);
145             }
146         });
147
148         synchronizer.getCanvasContext().getContentContext().setDirty();
149                 
150         }
151
152         @Override
153         public void dragOver(DropTargetDragEvent dtde, IDnDContext dp) {
154         }
155         
156         @Override
157         public void dragExit(DropTargetEvent dte, IDnDContext dp) {
158         }
159         
160         @Override
161         public void dropActionChanged(DropTargetDragEvent dtde, IDnDContext dp) {
162         }
163
164         @Override
165         public int getAllowedOps() {
166                 return 0;
167         }
168         
169     @Override
170     public double getPriority() {
171         return 9.1;
172     }
173         
174 }