]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExporter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / wizard / AnnotationTypeExporter.java
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.annotation.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.utils.ui.dialogs.ShowMessage;
43
44 /**
45  * @author Tuukka Lehtonen
46  * @author Teemu Mätäsniemi
47  */
48 public class AnnotationTypeExporter implements IRunnableWithProgress {
49
50     ExportPlan exportModel;
51
52     public AnnotationTypeExporter(ExportPlan exportModel) {
53         this.exportModel = exportModel;
54     }
55
56     @Override
57     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
58         SubMonitor progress = SubMonitor.convert(monitor, 50);
59         try {
60             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
61         } catch (IOException e) {
62             throw new InvocationTargetException(e);
63         } catch (DatabaseException e) {
64             throw new InvocationTargetException(e);
65         } catch (BindingException e) {
66             throw new InvocationTargetException(e);
67         } finally {
68             monitor.done();
69         }
70     }
71
72     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
73         try {
74             doExport(mon, exportModel.exportLocation, exportModel.model.getResource());
75
76         } catch (DatabaseException e) {
77             e.printStackTrace();
78             Logger.defaultLogError(e);
79             mon.setCanceled(true);
80             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
81         } finally {
82             mon.setWorkRemaining(0);
83         }
84     }
85     
86     public static void doExport(File location, Resource annotation)  throws DatabaseException, IOException {
87         NullProgressMonitor monitor = new NullProgressMonitor();
88         doExport(monitor, location, annotation);
89     }
90     
91     public static void doExport(IProgressMonitor monitor, File location, final Resource annotation)  throws DatabaseException, IOException {
92         // TODO: figure out a way to make the TG go directly into a file
93         // instead of having it all in memory at once.
94
95         monitor.beginTask("Exporting annotation type...", 100);
96         Session session = Simantics.getSession();
97         SimanticsClipboard clipboard = session.syncRequest(new Read<SimanticsClipboard>() {
98             @Override
99             public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
100                 CopyHandler ch = graph.adapt(annotation, CopyHandler.class);
101                 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
102                 ch.copyToClipboard(graph, clipboard);
103                 return clipboard;
104             }
105         });
106         for (Set<Representation> object : clipboard.getContents()) {
107             TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
108             monitor.worked(95);
109
110             monitor.setTaskName("Writing transferable graph...");
111             DataContainers.writeFile(location, new DataContainer(
112                     Constants.ANNOTATION_TYPE_FORMAT, Constants.ANNOTATION_TYPE_CURRENT_VERSION,
113                     new Variant(TransferableGraph1.BINDING, tg)));
114
115             monitor.worked(5);
116         }
117     }           
118 }