1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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.charts.editor;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.simantics.Simantics;
23 import org.simantics.charts.Activator;
24 import org.simantics.charts.query.AddChartItem;
25 import org.simantics.charts.query.ChartItemDescriptor;
26 import org.simantics.databoard.util.ObjectUtils;
27 import org.simantics.db.Resource;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.request.WriteRequest;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.request.PossibleModel;
32 import org.simantics.db.layer0.util.ClipboardUtils;
33 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
34 import org.simantics.db.layer0.util.SimanticsKeys;
35 import org.simantics.g2d.canvas.ICanvasParticipant;
36 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
37 import org.simantics.modeling.ModelingResources;
38 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
39 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
40 import org.simantics.scenegraph.g2d.events.command.Commands;
43 * Handles paste to TimeSeriesEditor
45 * @author toni.kalajainen
47 public class ChartPasteHandler2 extends AbstractCanvasParticipant implements ICanvasParticipant {
49 private Resource chart;
51 public ChartPasteHandler2(Resource chart) {
55 @EventHandler(priority = 0)
56 public boolean handleCommandEvent(CommandEvent e) {
58 if (e.command == Commands.PASTE) {
60 final List<Resource> resources = new ArrayList<Resource>();
61 for (Set<Representation> object : Simantics.getClipboard().getContents()) {
62 Collection<Resource> rs;
64 rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_COPY_RESOURCES);
67 } catch (DatabaseException ex) {
68 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
69 "Failed to retrieve clipboard contents.", ex));
73 if (resources.isEmpty())
76 Simantics.getSession().asyncRequest(new WriteRequest() {
78 public void perform(WriteGraph graph) throws DatabaseException {
79 Resource expectedModel = graph.syncRequest(new PossibleModel(chart));
80 if (expectedModel == null || chart == null)
82 ModelingResources MOD = ModelingResources.getInstance(graph);
83 List<ChartItemDescriptor> refs = new ArrayList<ChartItemDescriptor>();
84 for (Resource r : resources) {
85 if (graph.isInstanceOf(r, MOD.Subscription_Item)) {
86 Resource model = graph.syncRequest(new PossibleModel(r));
87 if (ObjectUtils.objectEquals(expectedModel, model))
88 refs.add(AddChartItem.createDescriptor(graph, r));
92 if (!refs.isEmpty()) {
93 AddChartItem.addAndMoveChartItems(chart, refs, Collections.<Resource> emptySet())