Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / ChartPasteHandler2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * 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.charts.editor;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Set;
19
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;
41
42 /**
43  * Handles paste to TimeSeriesEditor
44  * 
45  * @author toni.kalajainen
46  */
47 public class ChartPasteHandler2 extends AbstractCanvasParticipant implements ICanvasParticipant {
48
49     private Resource chart;
50
51     public ChartPasteHandler2(Resource chart) {
52         this.chart = chart;
53     }
54
55     @EventHandler(priority = 0)
56     public boolean handleCommandEvent(CommandEvent e) {
57
58         if (e.command == Commands.PASTE) {
59
60             final List<Resource> resources = new ArrayList<Resource>();
61             for (Set<Representation> object : Simantics.getClipboard().getContents()) {
62                 Collection<Resource> rs;
63                 try {
64                     rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_COPY_RESOURCES);
65                     if (rs != null)
66                         resources.addAll(rs);
67                 } catch (DatabaseException ex) {
68                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
69                             "Failed to retrieve clipboard contents.", ex));
70                 }
71             }
72
73             if (resources.isEmpty())
74                 return true;
75
76             Simantics.getSession().asyncRequest(new WriteRequest() {
77                 @Override
78                 public void perform(WriteGraph graph) throws DatabaseException {
79                     Resource expectedModel = graph.syncRequest(new PossibleModel(chart));
80                     if (expectedModel == null || chart == null)
81                         return;
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));
89                         }
90                     }
91
92                     if (!refs.isEmpty()) {
93                         AddChartItem.addAndMoveChartItems(chart, refs, Collections.<Resource> emptySet())
94                                 .perform(graph);
95                     }
96                 }
97             });
98             return true;
99         }
100
101         return false;
102
103     }
104
105 }