-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.graphfile.adapters;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.PasteHandlerAdapter;\r
-import org.simantics.db.layer0.util.ClipboardUtils;\r
-import org.simantics.db.layer0.util.PasteEventHandler;\r
-import org.simantics.db.layer0.util.RemoverUtil;\r
-import org.simantics.db.layer0.util.SimanticsClipboard;\r
-import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
-import org.simantics.db.layer0.util.SimanticsKeys;\r
-import org.simantics.graph.db.IImportAdvisor;\r
-import org.simantics.graph.db.TransferableGraphException;\r
-import org.simantics.graph.db.TransferableGraphs;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-\r
-public class SystemResourcePasteHandler extends PasteHandlerAdapter \r
-\r
-{\r
- private Resource resource;\r
-\r
- public SystemResourcePasteHandler(Resource resource) {\r
- this.resource = resource;\r
- }\r
-\r
- public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {\r
- TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);\r
- }\r
-\r
- public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {\r
- TransferableGraphs.importGraph1(graph, tg, advisor);\r
- }\r
-\r
- public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {\r
- defaultExecute(graph, tg, resource, advisor);\r
- }\r
-\r
- /**\r
- * Override this in your inherited class if post processing is done.\r
- */\r
- public void onPasteBegin(WriteGraph graph) throws DatabaseException {\r
- }\r
-\r
- /**\r
- * Override this in your inherited class if post processing is done\r
- * advisor.getTarget() returns an object under which data is copied\r
- * advisor.getRoot() returns the object which have been pasted.\r
- * @param representations TODO\r
- */\r
- public void onPaste(WriteGraph graph, SystemResourcePasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {\r
- advisor.attachRoot(graph);\r
- }\r
-\r
- /**\r
- * Override this in your inherited class if post processing is done.\r
- */\r
- public void onPasteEnd(WriteGraph graph) throws DatabaseException{\r
- }\r
-\r
- public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object) throws DatabaseException {\r
- Collection<Resource> result = new ArrayList<Resource>();\r
- TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);\r
- if (tg != null) {\r
- SystemResourcePasteImportAdvisor advisor = new SystemResourcePasteImportAdvisor(graph, resource); \r
- execute(graph, tg, resource, advisor);\r
- result.add(advisor.getRoot());\r
- try {\r
- onPaste(graph, advisor, object);\r
- } catch (Throwable e) {\r
- }\r
- }\r
- return result;\r
- }\r
-\r
- @Override\r
- public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {\r
- \r
- Collection<Resource> result = new ArrayList<Resource>();\r
- \r
- try {\r
- onPasteBegin(graph);\r
- } catch (Throwable e) {\r
- }\r
-\r
- final Set<Resource> cuts = new HashSet<Resource>();\r
- for(Set<Representation> object : clipboard.getContents()) {\r
- result.addAll(pasteObject(graph, object));\r
- Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);\r
- if (cut != null)\r
- cuts.addAll(cut);\r
- }\r
-\r
- try {\r
- onPasteEnd(graph);\r
- } catch (Throwable e) {\r
- }\r
-\r
- if(!cuts.isEmpty()) {\r
- for (Resource cut : cuts)\r
- RemoverUtil.remove(graph, cut);\r
- }\r
- \r
- return result;\r
- \r
- }\r
-\r
- @SuppressWarnings("rawtypes")\r
- @Override\r
- public Object getAdapter(Class adapter) {\r
- if(Resource.class == adapter) return resource;\r
- return null;\r
- }\r
-\r
- \r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.graphfile.adapters;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.PasteHandlerAdapter;
+import org.simantics.db.layer0.util.ClipboardUtils;
+import org.simantics.db.layer0.util.PasteEventHandler;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.db.layer0.util.SimanticsClipboard;
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
+import org.simantics.db.layer0.util.SimanticsKeys;
+import org.simantics.graph.db.IImportAdvisor;
+import org.simantics.graph.db.TransferableGraphException;
+import org.simantics.graph.db.TransferableGraphs;
+import org.simantics.graph.representation.TransferableGraph1;
+
+public class SystemResourcePasteHandler extends PasteHandlerAdapter
+
+{
+ private Resource resource;
+
+ public SystemResourcePasteHandler(Resource resource) {
+ this.resource = resource;
+ }
+
+ public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
+ TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);
+ }
+
+ public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
+ TransferableGraphs.importGraph1(graph, tg, advisor);
+ }
+
+ public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
+ defaultExecute(graph, tg, resource, advisor);
+ }
+
+ /**
+ * Override this in your inherited class if post processing is done.
+ */
+ public void onPasteBegin(WriteGraph graph) throws DatabaseException {
+ }
+
+ /**
+ * Override this in your inherited class if post processing is done
+ * advisor.getTarget() returns an object under which data is copied
+ * advisor.getRoot() returns the object which have been pasted.
+ * @param representations TODO
+ */
+ public void onPaste(WriteGraph graph, SystemResourcePasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {
+ advisor.attachRoot(graph);
+ }
+
+ /**
+ * Override this in your inherited class if post processing is done.
+ */
+ public void onPasteEnd(WriteGraph graph) throws DatabaseException{
+ }
+
+ public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object) throws DatabaseException {
+ Collection<Resource> result = new ArrayList<Resource>();
+ TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
+ if (tg != null) {
+ SystemResourcePasteImportAdvisor advisor = new SystemResourcePasteImportAdvisor(graph, resource);
+ execute(graph, tg, resource, advisor);
+ result.add(advisor.getRoot());
+ try {
+ onPaste(graph, advisor, object);
+ } catch (Throwable e) {
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
+
+ Collection<Resource> result = new ArrayList<Resource>();
+
+ try {
+ onPasteBegin(graph);
+ } catch (Throwable e) {
+ }
+
+ final Set<Resource> cuts = new HashSet<Resource>();
+ for(Set<Representation> object : clipboard.getContents()) {
+ result.addAll(pasteObject(graph, object));
+ Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
+ if (cut != null)
+ cuts.addAll(cut);
+ }
+
+ try {
+ onPasteEnd(graph);
+ } catch (Throwable e) {
+ }
+
+ if(!cuts.isEmpty()) {
+ for (Resource cut : cuts)
+ RemoverUtil.remove(graph, cut);
+ }
+
+ return result;
+
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Object getAdapter(Class adapter) {
+ if(Resource.class == adapter) return resource;
+ return null;
+ }
+
+
+
+}