X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftemplate2d%2FDiagramTemplates.java;h=6bb45a83f2836577b071080d0f618cb979224b3e;hb=06ee0c4c71cd9e372969da1570e7fcac2c4397a5;hp=8c4b0b8e2dad9dc840c2800b96410b425de2f7db;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/template2d/DiagramTemplates.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/template2d/DiagramTemplates.java index 8c4b0b8e2..6bb45a83f 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/template2d/DiagramTemplates.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/template2d/DiagramTemplates.java @@ -1,148 +1,148 @@ -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 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 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 handler) - throws IOException, DatabaseException - { - try { - Map> 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 { - @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(); - } - } - -} +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 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 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 handler) + throws IOException, DatabaseException + { + try { + Map> 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 { + @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(); + } + } + +}