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.common.utils.Logger;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.adapter.CopyHandler;
35 import org.simantics.db.layer0.util.ClipboardUtils;
36 import org.simantics.db.layer0.util.SimanticsClipboard;
37 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
38 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
39 import org.simantics.db.layer0.util.SimanticsKeys;
40 import org.simantics.db.request.Read;
41 import org.simantics.graph.representation.TransferableGraph1;
42 import org.simantics.modeling.template2d.DiagramTemplateConstants;
43 import org.simantics.utils.ui.dialogs.ShowMessage;
46 * @author Tuukka Lehtonen
47 * @author Teemu Mätäsniemi
49 public class DrawingTemplateExporter implements IRunnableWithProgress {
51 ExportPlan exportModel;
53 public DrawingTemplateExporter(ExportPlan exportModel) {
54 this.exportModel = exportModel;
58 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
59 SubMonitor progress = SubMonitor.convert(monitor, 50);
61 exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
62 } catch (IOException e) {
63 throw new InvocationTargetException(e);
64 } catch (DatabaseException e) {
65 throw new InvocationTargetException(e);
66 } catch (BindingException e) {
67 throw new InvocationTargetException(e);
73 void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
74 doExport(mon, exportModel.exportLocation, exportModel.model.getResource());
75 mon.setWorkRemaining(0);
78 public static void doExport(File location, Resource template) throws DatabaseException, IOException {
79 NullProgressMonitor monitor = new NullProgressMonitor();
80 doExport(monitor, location, template);
83 public static void doExport(IProgressMonitor mon, File location, final Resource template) throws DatabaseException, IOException {
85 // TODO: figure out a way to make the TG go directly into a file
86 // instead of having it all in memory at once.
88 mon.beginTask("Exporting diagram template...", 100);
89 Session session = Simantics.getSession();
90 SimanticsClipboard clipboard = session.syncRequest(new Read<SimanticsClipboard>() {
92 public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
93 CopyHandler ch = graph.adapt(template, CopyHandler.class);
94 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
95 ch.copyToClipboard(graph, clipboard);
99 for (Set<Representation> object : clipboard.getContents()) {
100 TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
103 mon.setTaskName("Writing transferable graph...");
104 DataContainers.writeFile(location, new DataContainer(
105 DiagramTemplateConstants.DRAWING_TEMPLATE_FORMAT, DiagramTemplateConstants.DRAWING_TEMPLATE_CURRENT_VERSION,
106 new Variant(TransferableGraph1.BINDING, tg)));
111 } catch (DatabaseException e) {
113 Logger.defaultLogError(e);
114 mon.setCanceled(true);
115 ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");