1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.ui;
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;
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;
37 * @author Tuukka Lehtonen
39 public class ChartGroupPasteHandler extends PasteHandlerAdapter {
41 private Resource chartGroup;
43 public ChartGroupPasteHandler(Resource chartGroup) {
44 this.chartGroup = chartGroup;
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);
56 rs = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
62 if (copied.isEmpty() && cut.isEmpty())
63 return Collections.emptyList();
65 return graph.syncRequest(new WriteResultRequest<Collection<Resource>>() {
67 public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
69 ChartResource CHART = ChartResource.getInstance(graph);
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)) {
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))
95 String error = ChartGroupDropActionFactory.validateDrop(graph, chartGroup, sources);
97 ShowError.showError("Cut Failed", error, (Throwable) null);
98 return Collections.emptyList();
100 return ChartGroupDropActionFactory.moveChartsRequest(chartGroup, sources).perform(graph);
103 return Collections.emptyList();
109 @SuppressWarnings("rawtypes")
111 public Object getAdapter(Class adapter) {
112 if (Resource.class == adapter)