+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.charts.editor;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.simantics.Simantics;\r
+import org.simantics.charts.Activator;\r
+import org.simantics.charts.query.AddChartItem;\r
+import org.simantics.charts.query.ChartItemDescriptor;\r
+import org.simantics.databoard.util.ObjectUtils;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.layer0.util.ClipboardUtils;\r
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
+import org.simantics.db.layer0.util.SimanticsKeys;\r
+import org.simantics.g2d.canvas.ICanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+\r
+/**\r
+ * Handles paste to TimeSeriesEditor\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class ChartPasteHandler2 extends AbstractCanvasParticipant implements ICanvasParticipant {\r
+\r
+ private Resource chart;\r
+\r
+ public ChartPasteHandler2(Resource chart) {\r
+ this.chart = chart;\r
+ }\r
+\r
+ @EventHandler(priority = 0)\r
+ public boolean handleCommandEvent(CommandEvent e) {\r
+\r
+ if (e.command == Commands.PASTE) {\r
+\r
+ final List<Resource> resources = new ArrayList<Resource>();\r
+ for (Set<Representation> object : Simantics.getClipboard().getContents()) {\r
+ Collection<Resource> rs;\r
+ try {\r
+ rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_COPY_RESOURCES);\r
+ if (rs != null)\r
+ resources.addAll(rs);\r
+ } catch (DatabaseException ex) {\r
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+ "Failed to retrieve clipboard contents.", ex));\r
+ }\r
+ }\r
+\r
+ if (resources.isEmpty())\r
+ return true;\r
+\r
+ Simantics.getSession().asyncRequest(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Resource expectedModel = graph.syncRequest(new PossibleModel(chart));\r
+ if (expectedModel == null || chart == null)\r
+ return;\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+ List<ChartItemDescriptor> refs = new ArrayList<ChartItemDescriptor>();\r
+ for (Resource r : resources) {\r
+ if (graph.isInstanceOf(r, MOD.Subscription_Item)) {\r
+ Resource model = graph.syncRequest(new PossibleModel(r));\r
+ if (ObjectUtils.objectEquals(expectedModel, model))\r
+ refs.add(AddChartItem.createDescriptor(graph, r));\r
+ }\r
+ }\r
+\r
+ if (!refs.isEmpty()) {\r
+ AddChartItem.addAndMoveChartItems(chart, refs, Collections.<Resource> emptySet())\r
+ .perform(graph);\r
+ }\r
+ }\r
+ });\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+\r
+ }\r
+\r
+}\r