1 package org.simantics.modeling.template2d;
4 import java.io.IOException;
5 import java.util.HashMap;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.core.runtime.SubMonitor;
10 import org.simantics.databoard.binding.Binding;
11 import org.simantics.databoard.container.DataContainer;
12 import org.simantics.databoard.container.DataContainers;
13 import org.simantics.databoard.container.DataFormatException;
14 import org.simantics.databoard.container.FormatHandler;
15 import org.simantics.db.Resource;
16 import org.simantics.db.Session;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
20 import org.simantics.graph.db.TGStatusMonitor;
21 import org.simantics.graph.db.TransferableGraphs;
22 import org.simantics.graph.representation.TransferableGraph1;
25 * Headless utility facade for diagram template I/O.
27 * @author Tuukka Lehtonen
29 public class DiagramTemplates {
33 * @param session the database session to use for importing
35 * the template file to import
36 * @param targetContainer
37 * the database container resource to make the imported template
39 * @return the root resource describing imported template
41 * @throws DatabaseException
43 public static Resource importTemplate(
44 IProgressMonitor monitor,
45 final Session session,
47 Resource targetContainer)
48 throws IOException, DatabaseException
50 final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
51 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
52 FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
54 public Resource process(DataContainer container) throws Exception {
55 TransferableGraphs.importGraph1WithMonitor(
57 (TransferableGraph1)container.content.getValue(),
60 return advisor.getRoot();
63 return importTemplate(templateFile, targetContainer, handler);
69 * database transaction handle to use for importing
71 * the template file to import
72 * @param targetContainer
73 * the database container resource to make the imported template
75 * @return the root resource describing imported template
77 * @throws DatabaseException
79 public static Resource importTemplate(
80 IProgressMonitor monitor,
81 final WriteGraph graph,
83 Resource targetContainer)
84 throws IOException, DatabaseException
86 final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
87 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
88 FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
90 public Resource process(DataContainer container) throws Exception {
91 TransferableGraphs.importGraph1(graph,
92 (TransferableGraph1) container.content.getValue(),
95 return advisor.getRoot();
98 return importTemplate(templateFile, targetContainer, handler);
101 private static Resource importTemplate(
104 FormatHandler<Resource> handler)
105 throws IOException, DatabaseException
108 Map<String, FormatHandler<Resource>> handlers = new HashMap<>(2);
109 handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V1, handler);
110 handlers.put(DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT_V2, handler);
111 return DataContainers.readFile(modelFile, handlers);
112 } catch (DataFormatException e) {
113 throw new IOException(e);
114 } catch (Exception e) {
115 if (e instanceof RuntimeException)
116 throw (RuntimeException)e;
118 throw new IOException(e);
122 private abstract static class DiagramTemplateFormatHandler implements FormatHandler<Resource> {
124 public Binding getBinding() {
125 return TransferableGraph1.BINDING;
129 private static class TGMonitor implements TGStatusMonitor {
130 private final SubMonitor mon;
131 private int last = 0;
132 public TGMonitor(SubMonitor mon) {
136 public void status(int percentage) {
137 if (percentage > last) {
138 mon.worked(percentage - last);
143 public boolean isCanceled() {
144 return mon.isCanceled();