]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
168c2a26d2a9a64f508fafa130db01e1e8b0414e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.trend.chart.element;\r
13 \r
14 import java.awt.datatransfer.Transferable;\r
15 import java.awt.datatransfer.UnsupportedFlavorException;\r
16 import java.awt.dnd.DnDConstants;\r
17 import java.awt.dnd.DropTargetDragEvent;\r
18 import java.awt.geom.AffineTransform;\r
19 import java.io.IOException;\r
20 \r
21 import org.eclipse.jface.viewers.IStructuredSelection;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.Session;\r
25 import org.simantics.db.common.request.UnaryRead;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
28 import org.simantics.g2d.dnd.DnDHints;\r
29 import org.simantics.g2d.dnd.ElementClassDragItem;\r
30 import org.simantics.g2d.dnd.IDnDContext;\r
31 import org.simantics.g2d.dnd.IDropTargetParticipant;\r
32 import org.simantics.g2d.element.ElementHints;\r
33 import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant;\r
34 import org.simantics.sysdyn.JFreeChartResource;\r
35 import org.simantics.ui.dnd.LocalObjectTransfer;\r
36 import org.simantics.ui.dnd.LocalObjectTransferable;\r
37 import org.simantics.ui.utils.AdaptionUtils;\r
38 \r
39 /**\r
40  * Drop participant for dropping chart definitions into diagrams\r
41  * @author Teemu Lempinen\r
42  *\r
43  */\r
44 public class PopulateChartDropParticipant extends PopulateElementDropParticipant implements IDropTargetParticipant {\r
45 \r
46     public PopulateChartDropParticipant(GraphToDiagramSynchronizer synchronizer) {\r
47         super(synchronizer);\r
48     }\r
49 \r
50     @Override\r
51     public void dragEnter(DropTargetDragEvent dtde, final IDnDContext dp) {\r
52         Transferable tr = dtde.getTransferable();\r
53         if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
54 \r
55             Session session = synchronizer.getSession();\r
56 \r
57             Object obj = null;\r
58 \r
59             try {\r
60                 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);\r
61                 \r
62                 // Create a structural selection from transfer data\r
63                 if (!(obj instanceof IStructuredSelection)) {\r
64                     obj = LocalObjectTransfer.getTransfer().getObject();\r
65                 }\r
66 \r
67                 if (obj instanceof IStructuredSelection) {\r
68 \r
69                     IStructuredSelection sel = (IStructuredSelection) obj;\r
70                     \r
71                     // Can drop only one chart at the time\r
72                     if (sel.size() == 1) {\r
73                         \r
74                         // Get the chart definition\r
75                         Resource chart = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
76                         if (chart == null)\r
77                             return;\r
78 \r
79                         ElementClassDragItem item = session.syncRequest(new UnaryRead<Resource,ElementClassDragItem>(chart) {\r
80 \r
81                             @Override\r
82                             public ElementClassDragItem perform(ReadGraph graph) throws DatabaseException {\r
83                                 if(graph.isInstanceOf(parameter, JFreeChartResource.getInstance(graph).Chart)) {\r
84                                     ElementClassDragItem item = new ElementClassDragItem(ChartElementFactory.create(graph, parameter));\r
85                                     AffineTransform initialTr = AffineTransform.getScaleInstance(1, 1);\r
86                                     item.getHintContext().setHint(ElementHints.KEY_TRANSFORM, initialTr);\r
87                                     return item;\r
88                                 } else {\r
89                                     return null;\r
90                                 }\r
91 \r
92                             }\r
93                         });\r
94 \r
95                         if(item != null) {\r
96                             dp.add(item);\r
97                             dp.getHints().setHint(DnDHints.KEY_DND_GRID_COLUMNS, Integer.valueOf(1));\r
98                         }\r
99 \r
100                     }\r
101 \r
102                 }\r
103 \r
104             } catch (UnsupportedFlavorException e) {\r
105                 e.printStackTrace();\r
106             } catch (IOException e) {\r
107                 e.printStackTrace();\r
108             } catch (DatabaseException e) {\r
109                 e.printStackTrace();\r
110             }\r
111 \r
112         }\r
113 \r
114         dtde.acceptDrag(DnDConstants.ACTION_COPY);\r
115     }\r
116 }\r