]> gerrit.simantics Code Review - simantics/3d.git/blob
ba4cfe56cb30c7e4ca7aedbb0fb792ec75570c3b
[simantics/3d.git] /
1 package org.simantics.proconf.g3d.shapeeditor.actions;\r
2 \r
3 import java.io.BufferedInputStream;\r
4 import java.io.File;\r
5 import java.io.FileInputStream;\r
6 import java.io.IOException;\r
7 \r
8 import org.eclipse.jface.action.Action;\r
9 import org.eclipse.swt.SWT;\r
10 import org.eclipse.swt.widgets.FileDialog;\r
11 import org.simantics.db.Graph;\r
12 import org.simantics.db.GraphRequestAdapter;\r
13 import org.simantics.db.GraphRequestStatus;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Statement;\r
16 import org.simantics.layer0.utils.serialization.ConnectionPointList;\r
17 import org.simantics.layer0.utils.serialization.TransferableGraph;\r
18 import org.simantics.layer0.utils.serialization.TransferableGraphUtils;\r
19 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;\r
20 import org.simantics.utils.ui.ErrorLogger;\r
21 \r
22 public class ImportAction extends Action {\r
23         \r
24         private ShapeEditorBase parent;\r
25         \r
26         public ImportAction(ShapeEditorBase parent) {\r
27                 this.parent = parent;\r
28                 setText("Import");\r
29                 setId("g3d shape import");\r
30         }\r
31         \r
32         @Override\r
33         public void run() {\r
34                 try {\r
35                         doImport();\r
36                 } catch (IOException e) {\r
37                         ErrorLogger.defaultLogError(e);\r
38                 }\r
39         }\r
40         \r
41         private void doImport() throws IOException {\r
42                 FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.OPEN);\r
43                 String filename = dialog.open();\r
44                 if (filename == null)\r
45                         return;\r
46                 File file = new File(filename);\r
47                 FileInputStream fis = new FileInputStream(file);\r
48                 BufferedInputStream bis = new BufferedInputStream(fis);\r
49                 byte[] data = new byte[0];\r
50                 byte[] buf = new byte[256];\r
51                 int res = 0;\r
52                 while ((res = bis.read(buf)) != -1) {\r
53                         byte[] newData = new byte[data.length + res];\r
54                         System.arraycopy(data, 0, newData, 0, data.length);\r
55                         System.arraycopy(buf, 0, newData, data.length, res);\r
56                         data = newData;\r
57                 }\r
58                 bis.close();\r
59                 final byte fdata[] = data;\r
60                 parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
61                         @Override\r
62                         public GraphRequestStatus perform(Graph g) throws Exception {\r
63                                 TransferableGraph sg = TransferableGraphUtils.deserialize(fdata);\r
64                                 ConnectionPointList purposes = TransferableGraphUtils.integrateTransferableGraph(g, sg, null);\r
65                                 Resource modelResource = purposes.getSingleByDescription(TransferableGraphUtils.CP_OBJECT).resource;\r
66                     Resource currentModelResource = parent.getModelResource();\r
67                     for (Statement s : g.getStatements(modelResource)) {\r
68                         g.removeStatements(currentModelResource, s.getPredicate());\r
69                     }\r
70                                 for (Statement s : g.getStatements(modelResource)) {\r
71                                         \r
72                                         g.removeStatement(s);\r
73                                         g.addStatement(currentModelResource, s.getPredicate(), s.getObject());\r
74                                 }\r
75                     \r
76                                 return GraphRequestStatus.transactionComplete();\r
77                         }\r
78                 });\r
79         }\r
80 \r
81 }\r