]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartGroupDropActionFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartGroupDropActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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.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.Simantics;
22 import org.simantics.charts.ontology.ChartResource;
23 import org.simantics.databoard.util.ObjectUtils;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.CommentMetadata;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.common.request.WriteResultRequest;
30 import org.simantics.db.common.utils.NameUtils;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.SelectionHints;
33 import org.simantics.db.layer0.adapter.DropActionFactory;
34 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
35 import org.simantics.db.layer0.request.PossibleModel;
36 import org.simantics.db.layer0.util.ClipboardUtils;
37 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
38 import org.simantics.db.layer0.util.SimanticsKeys;
39 import org.simantics.graph.db.TransferableGraphs;
40 import org.simantics.graph.representation.TransferableGraph1;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.utils.datastructures.Callback;
43 import org.simantics.utils.ui.ErrorLogger;
44 import org.simantics.utils.ui.ISelectionUtils;
45 import org.simantics.utils.ui.dialogs.ShowError;
46
47 /**
48  * @author Tuukka Lehtonen
49  */
50 public class ChartGroupDropActionFactory implements DropActionFactory {
51
52     @Override
53     public Runnable create(ReadGraph graph, Object target, Object source, int operation) throws DatabaseException {
54         final Resource chartGroup = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN,
55                 Resource.class);
56         if (chartGroup == null)
57             return null;
58         Resource targetModel = graph.syncRequest(new PossibleModel(chartGroup));
59         if (targetModel == null)
60             return null;
61
62         List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
63         Layer0 L0 = Layer0.getInstance(graph); 
64         ChartResource CHART = ChartResource.getInstance(graph);
65         Set<Resource> chartsToMove= new HashSet<Resource>();
66         for (Resource res : srcs) {
67             if (graph.isInstanceOf(res, CHART.Chart)) {
68                 Resource model = graph.syncRequest(new PossibleModel(res));
69                 if (ObjectUtils.objectEquals(targetModel, model)) {
70                     Resource partOf = graph.getPossibleObject(res, L0.PartOf);
71                     if (!ObjectUtils.objectEquals(chartGroup, partOf)) {
72                         chartsToMove.add(res);
73                     }
74                 }
75             }
76         }
77         if (!chartsToMove.isEmpty()) {
78             String error = validateDrop(graph, chartGroup, chartsToMove);
79             if (error != null) {
80                 ShowError.showError("Chart Drop Failed", error, (Exception) null);
81                 return null;
82             }
83             // TODO: support for move & copy
84             return moveCharts(chartGroup, chartsToMove);
85         }
86
87         return null;
88     }
89
90     public static String validateDrop(ReadGraph graph, Resource chartGroup, Set<Resource> chartsToMove) throws DatabaseException {
91         StringBuilder sb = new StringBuilder();
92         String targetName = NameUtils.getSafeName(graph, chartGroup);
93         Layer0 L0 = Layer0.getInstance(graph);
94         // Verify that target doesn't contain charts names as in chartsToMove
95         Set<String> sourceNames = new HashSet<String>();
96         for (Resource chart : chartsToMove) {
97             String chartName = NameUtils.getSafeName(graph, chart);
98             if (!sourceNames.add(chartName)) {
99                 sb.append("Multiple charts with same '" + chartName + "' cannot be moved into the same chart group.\n");
100             }
101         }
102         Set<String> reservedNames = NameUtils.findReservedNames(graph, "", chartGroup, L0.ConsistsOf);
103         for (String sourceName : sourceNames) {
104             if (reservedNames.contains(sourceName)) {
105                 sb.append("Name '" + sourceName + "' already in use in target '" + targetName + "'.\n");
106             }
107         }
108
109         return (sb.length() == 0) ? null : sb.toString();
110     }
111
112     public static Runnable moveCharts(final Resource chartGroup, final Set<Resource> chartsToMove) {
113         return new Runnable() {
114             @Override
115             public void run() {
116                 Simantics.getSession().asyncRequest(new WriteRequest() {
117
118                                         @Override
119                                         public void perform(WriteGraph graph)
120                                                         throws DatabaseException {
121                                 graph.syncRequest(moveChartsRequest(chartGroup, chartsToMove));
122                                         }
123                 }, new Callback<DatabaseException>() {
124                     @Override
125                     public void run(DatabaseException e) {
126                         if (e != null)
127                             ErrorLogger.defaultLogError(e);
128                     }
129                 });
130             }
131         };
132     }
133
134     public static MoveCharts moveChartsRequest(Resource chartGroup, Set<Resource> chartsToMove) {
135         return new MoveCharts(chartGroup, chartsToMove);
136     }
137
138     public static class MoveCharts extends WriteResultRequest<Collection<Resource>> {
139         private Resource chartGroup;
140         private Set<Resource> chartsToMove;
141
142         public MoveCharts(Resource chartGroup, Set<Resource> chartsToMove) {
143             this.chartGroup = chartGroup;
144             this.chartsToMove = chartsToMove;
145         }
146         @Override
147         public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
148             if (chartsToMove.isEmpty())
149                 return Collections.emptyList();
150             CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
151             String targetName = NameUtils.getSafeName(graph, chartGroup);
152             Layer0 L0 = Layer0.getInstance(graph);
153             List<Resource> result = new ArrayList<Resource>();
154             for (Resource chart : chartsToMove) {
155                 graph.deny(chart, L0.PartOf);
156                 graph.claim(chartGroup, L0.ConsistsOf, chart);
157                 cm.add("Moved chart '" + NameUtils.getSafeLabel(graph, chart) + "' to chart group '" + targetName + "'");
158                 result.add(chart);
159             }
160             graph.addMetadata(cm);
161             return result;
162         }
163     }
164
165     public static CopyCharts copyChartsRequest(Resource chartGroup, Set<Set<Representation>> sources) {
166         return new CopyCharts(chartGroup, sources);
167     }
168
169     public static class CopyCharts extends WriteResultRequest<Collection<Resource>> {
170         private Resource chartGroup;
171         private Set<Set<Representation>> sources;
172
173         public CopyCharts(Resource chartGroup, Set<Set<Representation>> sources) {
174             this.chartGroup = chartGroup;
175             this.sources = sources;
176         }
177         @Override
178         public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
179             if (sources.isEmpty())
180                 return Collections.emptyList();
181             CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
182             String targetName = NameUtils.getSafeName(graph, chartGroup);
183             List<Resource> result = new ArrayList<Resource>();
184             for (Set<Representation> r : sources) {
185                 Collection<Resource> srcs = ClipboardUtils.accept(graph, r, SimanticsKeys.KEY_COPY_RESOURCES);
186                 for(Resource src : srcs) {
187                     TransferableGraph1 tg = ClipboardUtils.accept(graph, r, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
188                     DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(chartGroup); 
189                     TransferableGraphs.importGraph1(graph, tg, advisor);
190                     cm.add("Copied chart '" + NameUtils.getSafeLabel(graph, src) + "' to chart group '" + targetName + "'");
191                     result.addAll(advisor.getRoots());
192                 }
193             }
194             graph.addMetadata(cm);
195             return result;
196         }
197     }
198
199 }