]> gerrit.simantics Code Review - simantics/platform.git/blob
bee851607a3a748848fd1f171c96ef14a2877fa1
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Set;
18
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;
44
45 /**
46  * @author Tuukka Lehtonen
47  * @author Teemu Mätäsniemi
48  */
49 public class DrawingTemplateExporter implements IRunnableWithProgress {
50
51     ExportPlan exportModel;
52
53     public DrawingTemplateExporter(ExportPlan exportModel) {
54         this.exportModel = exportModel;
55     }
56
57     @Override
58     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
59         SubMonitor progress = SubMonitor.convert(monitor, 50);
60         try {
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);
68         } finally {
69             monitor.done();
70         }
71     }
72
73     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
74         doExport(mon, exportModel.exportLocation, exportModel.model.getResource());
75         mon.setWorkRemaining(0);
76     }
77     
78     public static void doExport(File location, Resource template) throws DatabaseException, IOException {
79         NullProgressMonitor monitor = new NullProgressMonitor();
80         doExport(monitor, location, template);
81     }
82     
83     public static void doExport(IProgressMonitor mon, File location, final Resource template) throws DatabaseException, IOException {
84         try {
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.
87                 
88             mon.beginTask("Exporting diagram template...", 100);
89             Session session = Simantics.getSession();
90             SimanticsClipboard clipboard = session.syncRequest(new Read<SimanticsClipboard>() {
91                 @Override
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);
96                     return clipboard;
97                 }
98             });
99             for (Set<Representation> object : clipboard.getContents()) {
100                 TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
101                 mon.worked(95);
102
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)));
107
108                 mon.worked(5);
109             }
110
111         } catch (DatabaseException e) {
112             e.printStackTrace();
113             Logger.defaultLogError(e);
114             mon.setCanceled(true);
115             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
116         }
117     }
118
119 }