]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartGroupPasteHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartGroupPasteHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.ui;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.simantics.charts.ontology.ChartResource;
22 import org.simantics.databoard.util.ObjectUtils;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.WriteResultRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.PasteHandlerAdapter;
28 import org.simantics.db.layer0.request.PossibleModel;
29 import org.simantics.db.layer0.util.ClipboardUtils;
30 import org.simantics.db.layer0.util.PasteEventHandler;
31 import org.simantics.db.layer0.util.SimanticsClipboard;
32 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
33 import org.simantics.db.layer0.util.SimanticsKeys;
34 import org.simantics.utils.ui.dialogs.ShowError;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class ChartGroupPasteHandler extends PasteHandlerAdapter {
40
41     private Resource chartGroup;
42
43     public ChartGroupPasteHandler(Resource chartGroup) {
44         this.chartGroup = chartGroup;
45     }
46
47     @Override
48     public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
49         final List<Set<Representation>> copied = new ArrayList<>();
50         final List<Resource> cut = new ArrayList<>();
51         for (Set<Representation> object : clipboard.getContents()) {
52             Collection<Resource> rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_COPY_RESOURCES);
53             if (rs != null) {
54                 copied.add(object);
55             } else {
56                 rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
57                 if (rs != null)
58                     cut.addAll(rs);
59             }
60         }
61
62         if (copied.isEmpty() && cut.isEmpty())
63             return Collections.emptyList();
64
65         return graph.syncRequest(new WriteResultRequest<Collection<Resource>>() {
66             @Override
67             public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
68
69                 ChartResource CHART = ChartResource.getInstance(graph);
70
71                 Resource expectedModel = graph.syncRequest( new PossibleModel(chartGroup) );
72                 if (!copied.isEmpty()) {
73                     Set<Set<Representation>> sources = new HashSet<>();
74                     for (Set<Representation> r : copied) {
75                         Collection<Resource> srcs = ClipboardUtils.accept(r, SimanticsKeys.KEY_COPY_RESOURCES);
76                         for(Resource src : srcs) {
77                             if (graph.isInstanceOf(src, CHART.Chart)) {
78                                 Resource model = graph.syncRequest(new PossibleModel(src));
79                                 if (ObjectUtils.objectEquals(expectedModel, model)) {
80                                     sources.add(r);
81                                 }
82                             }
83                         }
84                     }
85                     return ChartGroupDropActionFactory.copyChartsRequest(chartGroup, sources).perform(graph);
86                 } else if (!cut.isEmpty()) {
87                     Set<Resource> sources = new HashSet<>();
88                     for (Resource r : cut) {
89                         if (graph.isInstanceOf(r, CHART.Chart)) {
90                             Resource model = graph.syncRequest(new PossibleModel(r));
91                             if (ObjectUtils.objectEquals(expectedModel, model))
92                                 sources.add(r);
93                         }
94                     }
95                     String error = ChartGroupDropActionFactory.validateDrop(graph, chartGroup, sources);
96                     if (error != null) {
97                         ShowError.showError("Cut Failed", error, (Throwable) null);
98                         return Collections.emptyList();
99                     }
100                     return ChartGroupDropActionFactory.moveChartsRequest(chartGroup, sources).perform(graph);
101                 }
102                 
103                 return Collections.emptyList();
104                 
105             }
106         });
107     }
108
109     @SuppressWarnings("rawtypes")
110     @Override
111     public Object getAdapter(Class adapter) {
112         if (Resource.class == adapter)
113             return chartGroup;
114         return null;
115     }
116
117 }