-package org.simantics.modeling.template2d;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.container.DataContainer;\r
-import org.simantics.databoard.container.DataContainers;\r
-import org.simantics.databoard.container.DataFormatException;\r
-import org.simantics.databoard.container.FormatHandler;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
-import org.simantics.graph.db.TGStatusMonitor;\r
-import org.simantics.graph.db.TransferableGraphs;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-\r
-/**\r
- * Headless utility facade for diagram template I/O.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class DiagramTemplates {\r
-\r
- /**\r
- * @param monitor\r
- * @param session the database session to use for importing\r
- * @param templateFile\r
- * the template file to import\r
- * @param targetContainer\r
- * the database container resource to make the imported template\r
- * a part of\r
- * @return the root resource describing imported template\r
- * @throws IOException\r
- * @throws DatabaseException\r
- */\r
- public static Resource importTemplate(\r
- IProgressMonitor monitor,\r
- final Session session,\r
- File templateFile,\r
- Resource targetContainer)\r
- throws IOException, DatabaseException\r
- {\r
- final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);\r
- final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);\r
- FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {\r
- @Override\r
- public Resource process(DataContainer container) throws Exception {\r
- TransferableGraphs.importGraph1WithMonitor(\r
- session,\r
- (TransferableGraph1)container.content.getValue(),\r
- advisor,\r
- new TGMonitor(mon));\r
- return advisor.getRoot();\r
- }\r
- };\r
- return importTemplate(templateFile, targetContainer, handler);\r
- }\r
-\r
- /**\r
- * @param monitor\r
- * @param graph\r
- * database transaction handle to use for importing\r
- * @param templateFile\r
- * the template file to import\r
- * @param targetContainer\r
- * the database container resource to make the imported template\r
- * a part of\r
- * @return the root resource describing imported template\r
- * @throws IOException\r
- * @throws DatabaseException\r
- */\r
- public static Resource importTemplate(\r
- IProgressMonitor monitor,\r
- final WriteGraph graph,\r
- File templateFile,\r
- Resource targetContainer)\r
- throws IOException, DatabaseException\r
- {\r
- final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);\r
- final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);\r
- FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {\r
- @Override\r
- public Resource process(DataContainer container) throws Exception {\r
- TransferableGraphs.importGraph1(graph,\r
- (TransferableGraph1) container.content.getValue(),\r
- advisor,\r
- new TGMonitor(mon));\r
- return advisor.getRoot();\r
- }\r
- };\r
- return importTemplate(templateFile, targetContainer, handler);\r
- }\r
-\r
- private static Resource importTemplate(\r
- File modelFile,\r
- Resource target,\r
- FormatHandler<Resource> handler)\r
- throws IOException, DatabaseException\r
- {\r
- try {\r
- Map<String, FormatHandler<Resource>> handlers = new HashMap<>(2);\r
- handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V1, handler);\r
- handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V2, handler);\r
- return DataContainers.readFile(modelFile, handlers);\r
- } catch (DataFormatException e) {\r
- throw new IOException(e);\r
- } catch (Exception e) {\r
- if (e instanceof RuntimeException)\r
- throw (RuntimeException)e;\r
- else\r
- throw new IOException(e);\r
- }\r
- }\r
-\r
- private abstract static class DiagramTemplateFormatHandler implements FormatHandler<Resource> {\r
- @Override\r
- public Binding getBinding() {\r
- return TransferableGraph1.BINDING;\r
- }\r
- }\r
-\r
- private static class TGMonitor implements TGStatusMonitor {\r
- private final SubMonitor mon;\r
- private int last = 0;\r
- public TGMonitor(SubMonitor mon) {\r
- this.mon = mon;\r
- }\r
- @Override\r
- public void status(int percentage) {\r
- if (percentage > last) {\r
- mon.worked(percentage - last);\r
- last = percentage;\r
- }\r
- }\r
- @Override\r
- public boolean isCanceled() {\r
- return mon.isCanceled();\r
- }\r
- }\r
-\r
-}\r
+package org.simantics.modeling.template2d;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.container.DataContainer;
+import org.simantics.databoard.container.DataContainers;
+import org.simantics.databoard.container.DataFormatException;
+import org.simantics.databoard.container.FormatHandler;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
+import org.simantics.graph.db.TGStatusMonitor;
+import org.simantics.graph.db.TransferableGraphs;
+import org.simantics.graph.representation.TransferableGraph1;
+
+/**
+ * Headless utility facade for diagram template I/O.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class DiagramTemplates {
+
+ /**
+ * @param monitor
+ * @param session the database session to use for importing
+ * @param templateFile
+ * the template file to import
+ * @param targetContainer
+ * the database container resource to make the imported template
+ * a part of
+ * @return the root resource describing imported template
+ * @throws IOException
+ * @throws DatabaseException
+ */
+ public static Resource importTemplate(
+ IProgressMonitor monitor,
+ final Session session,
+ File templateFile,
+ Resource targetContainer)
+ throws IOException, DatabaseException
+ {
+ final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
+ final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
+ FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
+ @Override
+ public Resource process(DataContainer container) throws Exception {
+ TransferableGraphs.importGraph1WithMonitor(
+ session,
+ (TransferableGraph1)container.content.getValue(),
+ advisor,
+ new TGMonitor(mon));
+ return advisor.getRoot();
+ }
+ };
+ return importTemplate(templateFile, targetContainer, handler);
+ }
+
+ /**
+ * @param monitor
+ * @param graph
+ * database transaction handle to use for importing
+ * @param templateFile
+ * the template file to import
+ * @param targetContainer
+ * the database container resource to make the imported template
+ * a part of
+ * @return the root resource describing imported template
+ * @throws IOException
+ * @throws DatabaseException
+ */
+ public static Resource importTemplate(
+ IProgressMonitor monitor,
+ final WriteGraph graph,
+ File templateFile,
+ Resource targetContainer)
+ throws IOException, DatabaseException
+ {
+ final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
+ final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
+ FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
+ @Override
+ public Resource process(DataContainer container) throws Exception {
+ TransferableGraphs.importGraph1(graph,
+ (TransferableGraph1) container.content.getValue(),
+ advisor,
+ new TGMonitor(mon));
+ return advisor.getRoot();
+ }
+ };
+ return importTemplate(templateFile, targetContainer, handler);
+ }
+
+ private static Resource importTemplate(
+ File modelFile,
+ Resource target,
+ FormatHandler<Resource> handler)
+ throws IOException, DatabaseException
+ {
+ try {
+ Map<String, FormatHandler<Resource>> handlers = new HashMap<>(2);
+ handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V1, handler);
+ handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V2, handler);
+ return DataContainers.readFile(modelFile, handlers);
+ } catch (DataFormatException e) {
+ throw new IOException(e);
+ } catch (Exception e) {
+ if (e instanceof RuntimeException)
+ throw (RuntimeException)e;
+ else
+ throw new IOException(e);
+ }
+ }
+
+ private abstract static class DiagramTemplateFormatHandler implements FormatHandler<Resource> {
+ @Override
+ public Binding getBinding() {
+ return TransferableGraph1.BINDING;
+ }
+ }
+
+ private static class TGMonitor implements TGStatusMonitor {
+ private final SubMonitor mon;
+ private int last = 0;
+ public TGMonitor(SubMonitor mon) {
+ this.mon = mon;
+ }
+ @Override
+ public void status(int percentage) {
+ if (percentage > last) {
+ mon.worked(percentage - last);
+ last = percentage;
+ }
+ }
+ @Override
+ public boolean isCanceled() {
+ return mon.isCanceled();
+ }
+ }
+
+}