]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ExportModelActionFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.adapters;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.widgets.Display;\r
18 import org.eclipse.swt.widgets.FileDialog;\r
19 import org.eclipse.swt.widgets.Shell;\r
20 import org.simantics.Simantics;\r
21 import org.simantics.databoard.Bindings;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.Session;\r
25 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
26 import org.simantics.db.common.request.UniqueRead;\r
27 import org.simantics.db.common.utils.Logger;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.layer0.adapter.ActionFactory;\r
30 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
31 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
32 import org.simantics.graph.db.TransferableGraphSource;\r
33 import org.simantics.graph.db.TransferableGraphs;\r
34 import org.simantics.layer0.Layer0;\r
35 \r
36 public class ExportModelActionFactory implements ActionFactory {\r
37 \r
38     String format;\r
39     String extension;\r
40     \r
41     public ExportModelActionFactory(String extension, String format) {\r
42         this.extension = extension;\r
43         this.format = format;\r
44     }\r
45 \r
46     @Override\r
47     public Runnable create(Object target_) {\r
48         final Resource target = (Resource)target_;\r
49         return new Runnable() {\r
50             @Override\r
51             public void run() {\r
52 \r
53                 Shell shell = Display.getDefault().getActiveShell();\r
54  \r
55                 Session session = Simantics.getSession();\r
56 \r
57                 try {\r
58 \r
59                         final Layer0 L0 = Layer0.getInstance(session);\r
60 \r
61                         final String name = session.syncRequest(new PossibleRelatedValue<String>(target, L0.HasName, Bindings.STRING));\r
62 \r
63                         FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
64                         dialog.setFilterExtensions(new String[] { "*." + extension });\r
65                         dialog.setFileName(name + "." + extension);\r
66                         String newFileName = dialog.open();\r
67                         if(newFileName == null) return;\r
68 \r
69                         if(!newFileName.endsWith(extension))\r
70                                 newFileName += "." + extension;\r
71 \r
72                         try (TransferableGraphSource s = session.sync(new ModelTransferableGraphSourceRequest(createConf(session, target)))) {\r
73                             TransferableGraphs.writeTransferableGraph(session, format, 1, s, new File(newFileName));\r
74                         }\r
75                 \r
76                         } catch (Exception e) {\r
77                                 Logger.defaultLogError(e);\r
78                         }\r
79 \r
80             }\r
81         };\r
82     }\r
83 \r
84     private TransferableGraphConfiguration2 createConf(Session session, final Resource model) throws DatabaseException {\r
85         return session.sync(new UniqueRead<TransferableGraphConfiguration2>() {\r
86             @Override\r
87             public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {\r
88                 return new TransferableGraphConfiguration2(graph, model, true, false);\r
89             }\r
90         });\r
91     }\r
92 \r
93 }\r