]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/template2d/DiagramTemplates.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / template2d / DiagramTemplates.java
1 package org.simantics.modeling.template2d;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7
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;
23
24 /**
25  * Headless utility facade for diagram template I/O.
26  * 
27  * @author Tuukka Lehtonen
28  */
29 public class DiagramTemplates {
30
31     /**
32      * @param monitor
33      * @param session the database session to use for importing
34      * @param templateFile
35      *            the template file to import
36      * @param targetContainer
37      *            the database container resource to make the imported template
38      *            a part of
39      * @return the root resource describing imported template
40      * @throws IOException
41      * @throws DatabaseException
42      */
43     public static Resource importTemplate(
44             IProgressMonitor monitor,
45             final Session session,
46             File templateFile,
47             Resource targetContainer)
48                     throws IOException, DatabaseException
49     {
50         final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
51         final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
52         FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
53             @Override
54             public Resource process(DataContainer container) throws Exception {
55                 TransferableGraphs.importGraph1WithMonitor(
56                         session,
57                         (TransferableGraph1)container.content.getValue(),
58                         advisor,
59                         new TGMonitor(mon));
60                 return advisor.getRoot();
61             }
62         };
63         return importTemplate(templateFile, targetContainer, handler);
64     }
65
66     /**
67      * @param monitor
68      * @param graph
69      *            database transaction handle to use for importing
70      * @param templateFile
71      *            the template file to import
72      * @param targetContainer
73      *            the database container resource to make the imported template
74      *            a part of
75      * @return the root resource describing imported template
76      * @throws IOException
77      * @throws DatabaseException
78      */
79     public static Resource importTemplate(
80             IProgressMonitor monitor,
81             final WriteGraph graph,
82             File templateFile,
83             Resource targetContainer)
84                     throws IOException, DatabaseException
85     {
86         final SubMonitor mon = SubMonitor.convert(monitor, "Importing diagram template", 100);
87         final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(targetContainer);
88         FormatHandler<Resource> handler = new DiagramTemplateFormatHandler() {
89             @Override
90             public Resource process(DataContainer container) throws Exception {
91                 TransferableGraphs.importGraph1(graph,
92                         (TransferableGraph1) container.content.getValue(),
93                         advisor,
94                         new TGMonitor(mon));
95                 return advisor.getRoot();
96             }
97         };
98         return importTemplate(templateFile, targetContainer, handler);
99     }
100
101     private static Resource importTemplate(
102             File modelFile,
103             Resource target,
104             FormatHandler<Resource> handler)
105                     throws IOException, DatabaseException
106     {
107         try {
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;
117             else
118                 throw new IOException(e);
119         }
120     }
121
122     private abstract static class DiagramTemplateFormatHandler implements FormatHandler<Resource> {
123         @Override
124         public Binding getBinding() {
125             return TransferableGraph1.BINDING;
126         }
127     }
128
129     private static class TGMonitor implements TGStatusMonitor {
130         private final SubMonitor mon;
131         private int last = 0;
132         public TGMonitor(SubMonitor mon) {
133             this.mon = mon;
134         }
135         @Override
136         public void status(int percentage) {
137             if (percentage > last) {
138                 mon.worked(percentage - last);
139                 last = percentage;
140             }
141         }
142         @Override
143         public boolean isCanceled() {
144             return mon.isCanceled();
145         }
146     }
147
148 }