]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
84573763920e5563768915299f6902bdd34b55af
[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                 DirectoryDialog dd = new DirectoryDialog(shell);\r
48                 dd.setFilterPath(Platform.getLocation().toOSString());\r
49                 dd.setText("Export files to...");\r
50                 dd.setMessage("Select a directory");\r
51                 final String dir = dd.open();\r
52                 if (dir == null) {\r
53                         return null;\r
54                 }\r
55 \r
56                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
57 \r
58                         @Override\r
59                         public void run(ReadGraph graph) throws DatabaseException {\r
60                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
61                                 for(Resource r : resources) {\r
62                                         try {\r
63                                                 String name = NameUtils.getSafeName(graph, r);\r
64                                                 FileOutputStream fos = new FileOutputStream(dir + "\\" + name);\r
65                                                 byte[] fileBArray = graph.getPossibleRelatedValue(r, sr.HasExternalFile, Bindings.BYTE_ARRAY);\r
66                                                 fos.write(fileBArray);\r
67                                                 fos.close();\r
68                                         } catch (IOException e) {\r
69                                                 e.printStackTrace();\r
70                                         }\r
71                                 }\r
72                                 \r
73                         }\r
74                 });\r
75 \r
76                 return null;\r
77         }\r
78 \r
79 }\r