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.annotation.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.utils.ui.dialogs.ShowMessage;
45 * @author Tuukka Lehtonen
46 * @author Teemu Mätäsniemi
48 public class AnnotationTypeExporter implements IRunnableWithProgress {
50 ExportPlan exportModel;
52 public AnnotationTypeExporter(ExportPlan exportModel) {
53 this.exportModel = exportModel;
57 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
58 SubMonitor progress = SubMonitor.convert(monitor, 50);
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);
72 void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
74 doExport(mon, exportModel.exportLocation, exportModel.model.getResource());
76 } catch (DatabaseException e) {
78 Logger.defaultLogError(e);
79 mon.setCanceled(true);
80 ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
82 mon.setWorkRemaining(0);
86 public static void doExport(File location, Resource annotation) throws DatabaseException, IOException {
87 NullProgressMonitor monitor = new NullProgressMonitor();
88 doExport(monitor, location, annotation);
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.
95 monitor.beginTask("Exporting annotation type...", 100);
96 Session session = Simantics.getSession();
97 SimanticsClipboard clipboard = session.syncRequest(new Read<SimanticsClipboard>() {
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);
106 for (Set<Representation> object : clipboard.getContents()) {
107 TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
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)));