]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java
Added new field TypeId to dependency index for exact type searching
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ExportModelActionFactory.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.adapters;
13
14 import java.io.File;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.FileDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.simantics.Simantics;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.common.utils.Logger;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.adapter.ActionFactory;
30 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
31 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
32 import org.simantics.graph.db.TransferableGraphSource;
33 import org.simantics.graph.db.TransferableGraphs;
34 import org.simantics.layer0.Layer0;
35
36 public class ExportModelActionFactory implements ActionFactory {
37
38     String format;
39     String extension;
40     
41     public ExportModelActionFactory(String extension, String format) {
42         this.extension = extension;
43         this.format = format;
44     }
45
46     @Override
47     public Runnable create(Object target_) {
48         final Resource target = (Resource)target_;
49         return new Runnable() {
50             @Override
51             public void run() {
52
53                 Shell shell = Display.getDefault().getActiveShell();
54  
55                 Session session = Simantics.getSession();
56
57                 try {
58
59                         final Layer0 L0 = Layer0.getInstance(session);
60
61                         final String name = session.syncRequest(new PossibleRelatedValue<String>(target, L0.HasName, Bindings.STRING));
62
63                         FileDialog dialog = new FileDialog(shell, SWT.SAVE);
64                         dialog.setFilterExtensions(new String[] { "*." + extension });
65                         dialog.setFileName(name + "." + extension);
66                         String newFileName = dialog.open();
67                         if(newFileName == null) return;
68
69                         if(!newFileName.endsWith(extension))
70                                 newFileName += "." + extension;
71
72                         try (TransferableGraphSource s = session.sync(new ModelTransferableGraphSourceRequest(createConf(session, target)))) {
73                             TransferableGraphs.writeTransferableGraph(session, format, 1, s, new File(newFileName));
74                         }
75                 
76                         } catch (Exception e) {
77                                 Logger.defaultLogError(e);
78                         }
79
80             }
81         };
82     }
83
84     private TransferableGraphConfiguration2 createConf(Session session, final Resource model) throws DatabaseException {
85         return session.sync(new UniqueRead<TransferableGraphConfiguration2>() {
86             @Override
87             public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
88                 return new TransferableGraphConfiguration2(graph, model, true, false);
89             }
90         });
91     }
92
93 }