]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e82df8580c84ba5b6e6a9bd7ef8be9ab2c737bc7
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.sysdyn.ui.handlers.exports;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.runtime.Platform;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.widgets.FileDialog;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 import org.eclipse.ui.handlers.HandlerUtil;\r
25 import org.simantics.databoard.Bindings;\r
26 import org.simantics.db.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
29 import org.simantics.db.common.request.ReadRequest;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
32 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
33 import org.simantics.db.request.Read;\r
34 import org.simantics.graph.db.TransferableGraphSource;\r
35 import org.simantics.graph.db.TransferableGraphs;\r
36 import org.simantics.layer0.Layer0;\r
37 import org.simantics.sysdyn.ui.Activator;\r
38 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;\r
39 import org.simantics.ui.SimanticsUI;\r
40 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
41 \r
42 /**\r
43  * Exports a selected model\r
44  * \r
45  * @author Teemu Lempinen\r
46  * @author Tuomas Miettinen\r
47  *\r
48  */\r
49 public class ExportModelHandler extends AbstractHandler {\r
50 \r
51         @Override\r
52         public Object execute(ExecutionEvent event) throws ExecutionException {\r
53         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
54         final Resource model = ResourceAdaptionUtils.toSingleResource(sel);\r
55         if(model == null) return null;\r
56         \r
57         // FIXME: Model browser doesn't change its selection even if the selected object is removed,\r
58         // so you can try to export a removed model \r
59                 String name = null;\r
60                 try {\r
61                         name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
62 \r
63                                 @Override\r
64                                 public String perform(ReadGraph graph) throws DatabaseException {\r
65                                         if (!graph.hasStatement(model, Layer0.getInstance(graph).PartOf))\r
66                                                 return null;\r
67                                         Layer0 l0 = Layer0.getInstance(graph);\r
68                                         String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, Bindings.STRING ));\r
69                                         return name;\r
70                                         \r
71                                 }\r
72                                 \r
73                         });\r
74                 } catch (DatabaseException e1) {\r
75                         e1.printStackTrace();\r
76                 }\r
77                 // Do not export if the resource has no name\r
78                 if(name == null) return null;\r
79                 \r
80         // Find a location (and name) for the exported library using FileDialog\r
81                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
82                 FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
83                 fd.setText("Export Model");\r
84                 fd.setFileName(name);\r
85                 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODELTPATH);\r
86                 if(path.isEmpty() || !(new File(path).exists()))\r
87                         path = Platform.getLocation().toOSString();\r
88                 fd.setFilterPath(path);\r
89                 String[] filterExt = {"*.tg"};\r
90                 fd.setFilterExtensions(filterExt);\r
91                 fd.setOverwrite(true);\r
92                 final String selected = fd.open();\r
93                 if(selected == null) return null;\r
94 \r
95                 // Save location to preference store\r
96                 Activator.getDefault().getPreferenceStore().setValue(ImportUtilsUI.IMPORTMODELTPATH, (new File(selected)).getParent());\r
97 \r
98                 // Asynchronously create the file using transferable graph\r
99                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
100 \r
101                     @Override\r
102                     public void run(ReadGraph graph) throws DatabaseException {\r
103                         TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model);\r
104                         TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
105                         try {\r
106                             TransferableGraphs.writeTransferableGraph(graph, "sysdynModel", 1, s,new File(selected));\r
107                         } catch (Exception e) {\r
108                             e.printStackTrace();\r
109                         }\r
110                         }\r
111                 });\r
112 \r
113                 return null;\r
114         }\r
115 \r
116 }\r