]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
727bb8be423ec09769fcb7371276e470759b1d98
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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;\r
13 \r
14 import java.io.FileOutputStream;\r
15 import java.io.IOException;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.core.runtime.Platform;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.swt.widgets.DirectoryDialog;\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.request.ReadRequest;\r
29 import org.simantics.db.common.utils.NameUtils;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.sysdyn.SysdynResource;\r
32 import org.simantics.ui.SimanticsUI;\r
33 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
34 \r
35 public class ExportExternalFunctionFilesHandler extends AbstractHandler {\r
36 \r
37         @Override\r
38         public Object execute(ExecutionEvent event) throws ExecutionException {\r
39 \r
40                 \r
41                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
42                 ISelection sel = HandlerUtil.getCurrentSelection(event);\r
43                 final Resource[] resources = ResourceAdaptionUtils.toResources(sel);\r
44                 if (resources.length < 1)\r
45                         return null;\r
46 \r
47                 return exportFiles(shell, resources);\r
48         }\r
49         \r
50         public static Object exportFiles(Shell shell, final Resource[] resources) {\r
51                 DirectoryDialog dd = new DirectoryDialog(shell);\r
52                 dd.setFilterPath(Platform.getLocation().toOSString());\r
53                 dd.setText("Export files to...");\r
54                 dd.setMessage("Select a directory");\r
55                 final String dir = dd.open();\r
56                 if (dir == null) {\r
57                         return null;\r
58                 }\r
59 \r
60                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
61 \r
62                         @Override\r
63                         public void run(ReadGraph graph) throws DatabaseException {\r
64                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
65                                 for(Resource r : resources) {\r
66                                         try {\r
67                                                 String name = NameUtils.getSafeName(graph, r);\r
68                                                 FileOutputStream fos = new FileOutputStream(dir + "\\" + name);\r
69                                                 byte[] fileBArray = graph.getPossibleRelatedValue(r, sr.HasExternalFile, Bindings.BYTE_ARRAY);\r
70                                                 fos.write(fileBArray);\r
71                                                 fos.close();\r
72                                         } catch (IOException e) {\r
73                                                 e.printStackTrace();\r
74                                         }\r
75                                 }\r
76                                 \r
77                         }\r
78                 });\r
79                 return null;\r
80         }\r
81 \r
82 }\r