]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java
Improved usability of shared library export wizard
[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.binding.mutable.Variant;
24 import org.simantics.databoard.serialization.SerializationException;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.common.utils.Logger;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.util.ModelDependenciesBean;
30 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
31 import org.simantics.modeling.ModelingUtils;
32 import org.simantics.modeling.ModelingUtils.LibraryInfo;
33 import org.simantics.utils.ui.dialogs.ShowMessage;
34
35 /**
36  * @author Antti Villberg
37  */
38 public class ModelExporter implements IRunnableWithProgress {
39
40     ExportPlan exportModel;
41
42     public ModelExporter(ExportPlan exportModel) {
43         this.exportModel = exportModel;
44     }
45
46     @Override
47     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
48         SubMonitor progress = SubMonitor.convert(monitor, 50);
49         try {
50             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
51         } catch (IOException e) {
52             throw new InvocationTargetException(e);
53         } catch (DatabaseException e) {
54             throw new InvocationTargetException(e);
55         } catch (BindingException e) {
56             throw new InvocationTargetException(e);
57         } finally {
58             monitor.done();
59         }
60     }
61
62     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
63         try {
64             doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.includeDependencies);
65         } catch (DatabaseException e) {
66             e.printStackTrace();
67             Logger.defaultLogError(e);
68             mon.setCanceled(true);
69             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
70         } finally {
71             mon.setWorkRemaining(0);
72         }
73     }
74
75     public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info, boolean includeDependencies) throws DatabaseException, IOException {
76         Simantics.sync(new ReadRequest() {
77             @Override
78             public void run(ReadGraph graph) throws DatabaseException {
79                 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, info.library.getResource(), true, false);
80                 if (includeDependencies) {
81                     ModelDependenciesBean bean = ModelDependenciesBean.create(graph, info.library.getResource());
82                     conf.baseExtensions.put(ModelDependenciesBean.EXTENSION_KEY, new Variant(ModelDependenciesBean.BINDING, bean));
83                 }
84                 ModelingUtils.exportModel(graph, conf, location.getAbsolutePath(), "", 1);
85             }
86         });
87     }
88
89 }