1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.jfreechart.chart.element;
\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
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
40 * Drop participant for dropping chart definitions into diagrams
\r
41 * @author Teemu Lempinen
\r
44 public class PopulateChartDropParticipant extends PopulateElementDropParticipant implements IDropTargetParticipant {
\r
46 public PopulateChartDropParticipant(GraphToDiagramSynchronizer synchronizer) {
\r
47 super(synchronizer);
\r
51 public void dragEnter(DropTargetDragEvent dtde, final IDnDContext dp) {
\r
52 Transferable tr = dtde.getTransferable();
\r
53 if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
\r
55 Session session = synchronizer.getSession();
\r
60 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);
\r
62 // Create a structural selection from transfer data
\r
63 if (!(obj instanceof IStructuredSelection)) {
\r
64 obj = LocalObjectTransfer.getTransfer().getObject();
\r
67 if (obj instanceof IStructuredSelection) {
\r
69 IStructuredSelection sel = (IStructuredSelection) obj;
\r
71 // Can drop only one chart at the time
\r
72 if (sel.size() == 1) {
\r
74 // Get the chart definition
\r
75 Resource chart = AdaptionUtils.adaptToSingle(sel, Resource.class);
\r
79 ElementClassDragItem item = session.syncRequest(new UnaryRead<Resource,ElementClassDragItem>(chart) {
\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
97 dp.getHints().setHint(DnDHints.KEY_DND_GRID_COLUMNS, Integer.valueOf(1));
\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
114 dtde.acceptDrag(DnDConstants.ACTION_COPY);
\r