--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.MessageBox;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class ExportModuleHandler extends AbstractHandler {\r
+\r
+ /**\r
+ * Temporary exception. At this phase, the system will not support exporting modules\r
+ * that depend on other modules. \r
+ * \r
+ * @author TLTEEMU\r
+ *\r
+ */\r
+ class ContainsDependenciesException extends DatabaseException {\r
+ private static final long serialVersionUID = -1533706136673146020L;\r
+ \r
+ private Collection<String> dependencies;\r
+ \r
+ ContainsDependenciesException(Collection<String> dependencies) {\r
+ this.dependencies = dependencies;\r
+ }\r
+ \r
+ public Collection<String> getDependencies() {\r
+ return this.dependencies;\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+ ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+ final Resource modulesymbol = ResourceAdaptionUtils.toSingleResource(sel);\r
+ if(modulesymbol == null) return null;\r
+ \r
+ String name = null;\r
+ try {\r
+ name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
+ Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);\r
+ if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
+ return null;\r
+ \r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ if (configuration == null)\r
+ return null;\r
+ \r
+ ArrayList<String> dependencies = null;\r
+ for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {\r
+ if(dependencies == null)\r
+ dependencies = new ArrayList<String>();\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ String instanceOf = NameUtils.getSafeName(graph, graph.getSingleObject(r, l0.InstanceOf));\r
+ dependencies.add(name + " : " + instanceOf);\r
+ }\r
+ if(dependencies != null && !dependencies.isEmpty())\r
+ throw new ContainsDependenciesException(dependencies);\r
+ \r
+ String name = graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING);\r
+ return name;\r
+ \r
+ }\r
+ \r
+ });\r
+ } catch (ContainsDependenciesException e1) {\r
+ Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+ MessageBox mb = new MessageBox(shell, SWT.OK);\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("This version does not support exporting modules with other module instances.\n\n");\r
+ sb.append("Dependencies:\n");\r
+ for(String s : e1.getDependencies())\r
+ sb.append(" " + s + "\n");\r
+ mb.setMessage(sb.toString());\r
+ mb.setText("Module contains dependencies.");\r
+ mb.open();\r
+ return null;\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ if(name == null) return null;\r
+ \r
+ Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+ FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
+ fd.setText("Export..");\r
+ fd.setFileName(name);\r
+ fd.setFilterPath(Platform.getLocation().toOSString());\r
+ String[] filterExt = {"*.tg"};\r
+ fd.setFilterExtensions(filterExt);\r
+ final String selected = fd.open();\r
+ if(selected == null) return null;\r
+ \r
+ \r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+\r
+ final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);\r
+ if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
+ return;\r
+ String name = graph.syncRequest(new PossibleRelatedValue<String>(component, l0.HasName, Bindings.STRING ));\r
+ final ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+ roots.add(Pair.make(component, name));\r
+\r
+ graph.asyncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+\r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ if (!graph.hasStatement(configuration, l0.PartOf, component)&& \r
+ !graph.hasStatement(modulesymbol, l0.PartOf, component)) {\r
+ // Make sure that configuration and symbol are included.\r
+ // In old versions, they were attached to model, not to module.\r
+ Resource previousPartof = graph.getSingleObject(configuration, l0.PartOf);\r
+\r
+ graph.deny(configuration, l0.PartOf);\r
+ graph.deny(modulesymbol, l0.PartOf);\r
+ graph.claim(configuration, l0.PartOf, l0.ConsistsOf, component);\r
+ graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, component);\r
+\r
+ export(graph, selected, roots, component);\r
+\r
+ graph.deny(configuration, l0.PartOf);\r
+ graph.deny(modulesymbol, l0.PartOf);\r
+ graph.claim(configuration, l0.PartOf, l0.ConsistsOf, previousPartof);\r
+ graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, previousPartof);\r
+ } else {\r
+ // Normal export\r
+ export(graph, selected, roots, component);\r
+ }\r
+ }\r
+ });\r
+\r
+ }\r
+ });\r
+\r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Export module (without dependencies to other modules) and write it to file. \r
+ * Disable existing enumeration replacement for during export. \r
+ * \r
+ * @param graph WriteGraph\r
+ * @param path Path for the exported file\r
+ * @param roots\r
+ * @param component Module\r
+ */\r
+ private void export(WriteGraph graph, String path, ArrayList<Pair<Resource, String>> roots, Resource component) {\r
+ \r
+ // FIXME: Enumeration replacement handling like this is not suitable.\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+\r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ ArrayList<Pair<Resource, Resource>> replacements = new ArrayList<Pair<Resource, Resource>>();\r
+\r
+ for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {\r
+ if(graph.hasStatement(enumeration, sr.ReplacedEnumeration_Inverse)) {\r
+ for(Resource replacement : graph.getObjects(enumeration, sr.ReplacedEnumeration_Inverse)) {\r
+ replacements.add(new Pair<Resource, Resource>(enumeration, replacement));\r
+ }\r
+ }\r
+ }\r
+ \r
+ for(Pair<Resource,Resource> replacement : replacements)\r
+ graph.deny(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second);\r
+ \r
+ TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));\r
+ Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ \r
+ for(Pair<Resource,Resource> replacement : replacements)\r
+ graph.claim(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second);\r
+\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.graph.db.MissingDependencyException;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class ImportModuleHandler extends AbstractHandler {\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
+ if (node == null)\r
+ return null;\r
+\r
+ final Resource model = node.data;\r
+ \r
+ Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+ FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
+ fd.setText("Import Module");\r
+ fd.setFilterPath(Platform.getLocation().toOSString());\r
+ String[] filterExt = {"*.tg"};\r
+ fd.setFilterExtensions(filterExt);\r
+ String selected = fd.open();\r
+ if(selected == null) return null;\r
+\r
+ TransferableGraph1 tg = null;\r
+ try {\r
+ tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ if(tg == null) return null;\r
+\r
+ \r
+ DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute(tg, model, ia);\r
+ } catch (MissingDependencyException e) {\r
+ e.printStackTrace();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ return null;\r
+ }\r
+\r
+}\r