]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java
Option for exporting tg and pgraph with sharedlibrary
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelExporter.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.modeling.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubMonitor;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.simantics.Simantics;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.serialization.SerializationException;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.modeling.ModelingUtils;
29 import org.simantics.modeling.ModelingUtils.LibraryInfo;
30 import org.simantics.utils.ui.dialogs.ShowMessage;
31
32 /**
33  * @author Antti Villberg
34  */
35 public class ModelExporter implements IRunnableWithProgress {
36
37     ExportPlan exportModel;
38
39     public ModelExporter(ExportPlan exportModel) {
40         this.exportModel = exportModel;
41     }
42
43     @Override
44     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
45         SubMonitor progress = SubMonitor.convert(monitor, 50);
46         try {
47             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
48         } catch (IOException e) {
49             throw new InvocationTargetException(e);
50         } catch (DatabaseException e) {
51             throw new InvocationTargetException(e);
52         } catch (BindingException e) {
53             throw new InvocationTargetException(e);
54         } finally {
55             monitor.done();
56         }
57     }
58
59     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
60         try {
61             doExport(mon, exportModel.exportLocation, exportModel.model);
62
63         } catch (DatabaseException e) {
64             e.printStackTrace();
65             Logger.defaultLogError(e);
66             mon.setCanceled(true);
67             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
68         } finally {
69             mon.setWorkRemaining(0);
70         }
71     }
72     
73     public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info) throws DatabaseException, IOException {
74         Simantics.sync(new ReadRequest() {
75
76                         @Override
77                         public void run(ReadGraph graph) throws DatabaseException {
78                         ModelingUtils.exportModel(graph, info.library.getResource(), location.getAbsolutePath(), "", 1);
79                         }
80                 
81         });
82     }
83         
84 }