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