1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.wizard;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.simantics.Simantics;
24 import org.simantics.databoard.binding.error.BindingException;
25 import org.simantics.databoard.binding.mutable.Variant;
26 import org.simantics.databoard.container.DataContainer;
27 import org.simantics.databoard.container.DataContainers;
28 import org.simantics.databoard.serialization.SerializationException;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.adapter.CopyHandler;
34 import org.simantics.db.layer0.util.ClipboardUtils;
35 import org.simantics.db.layer0.util.SimanticsClipboard;
36 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
37 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
38 import org.simantics.db.layer0.util.SimanticsKeys;
39 import org.simantics.db.request.Read;
40 import org.simantics.graph.representation.TransferableGraph1;
41 import org.simantics.modeling.template2d.DiagramTemplateConstants;
42 import org.simantics.utils.ui.dialogs.ShowMessage;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * @author Tuukka Lehtonen
48 * @author Teemu M�t�sniemi
50 public class DrawingTemplateExporter implements IRunnableWithProgress {
52 private static final Logger LOGGER = LoggerFactory.getLogger(DrawingTemplateExporter.class);
54 ExportPlan exportModel;
56 public DrawingTemplateExporter(ExportPlan exportModel) {
57 this.exportModel = exportModel;
61 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
62 SubMonitor progress = SubMonitor.convert(monitor, 50);
64 exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
65 } catch (IOException e) {
66 throw new InvocationTargetException(e);
67 } catch (DatabaseException e) {
68 throw new InvocationTargetException(e);
69 } catch (BindingException e) {
70 throw new InvocationTargetException(e);
76 void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
77 doExport(mon, exportModel.exportLocation, exportModel.model.getResource());
78 mon.setWorkRemaining(0);
81 public static void doExport(File location, Resource template) throws DatabaseException, IOException {
82 NullProgressMonitor monitor = new NullProgressMonitor();
83 doExport(monitor, location, template);
86 public static void doExport(IProgressMonitor mon, File location, final Resource template) throws DatabaseException, IOException {
88 // TODO: figure out a way to make the TG go directly into a file
89 // instead of having it all in memory at once.
91 mon.beginTask("Exporting diagram template...", 100);
92 Session session = Simantics.getSession();
93 SimanticsClipboard clipboard = session.syncRequest(new Read<SimanticsClipboard>() {
95 public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
96 CopyHandler ch = graph.adapt(template, CopyHandler.class);
97 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
98 ch.copyToClipboard(graph, clipboard, SubMonitor.convert(mon, 50));
102 for (Set<Representation> object : clipboard.getContents()) {
103 TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
106 mon.setTaskName("Writing transferable graph...");
107 DataContainers.writeFile(location, new DataContainer(
108 DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT, DiagramTemplateConstants.DRAWING_TEMPLATE_CURRENT_VERSION,
109 new Variant(TransferableGraph1.BINDING, tg)));
112 } catch (DatabaseException e) {
113 LOGGER.error("Drawing template export failed", e);
114 mon.setCanceled(true);
115 ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");