]> gerrit.simantics Code Review - simantics/3d.git/blob
28bdc023df373ccc44fa41b7f2bce073c1468e26
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.shapeeditor.actions;\r
12 \r
13 import java.io.BufferedInputStream;\r
14 import java.io.File;\r
15 import java.io.FileInputStream;\r
16 import java.io.IOException;\r
17 \r
18 import org.eclipse.jface.action.Action;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.widgets.FileDialog;\r
21 import org.simantics.db.Graph;\r
22 import org.simantics.db.GraphRequestAdapter;\r
23 import org.simantics.db.GraphRequestStatus;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.Statement;\r
26 import org.simantics.layer0.utils.serialization.ConnectionPointList;\r
27 import org.simantics.layer0.utils.serialization.TransferableGraph;\r
28 import org.simantics.layer0.utils.serialization.TransferableGraphUtils;\r
29 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;\r
30 import org.simantics.utils.ui.ErrorLogger;\r
31 \r
32 public class ImportAction extends Action {\r
33         \r
34         private ShapeEditorBase parent;\r
35         \r
36         public ImportAction(ShapeEditorBase parent) {\r
37                 this.parent = parent;\r
38                 setText("Import");\r
39                 setId("g3d shape import");\r
40         }\r
41         \r
42         @Override\r
43         public void run() {\r
44                 try {\r
45                         doImport();\r
46                 } catch (IOException e) {\r
47                         ErrorLogger.defaultLogError(e);\r
48                 }\r
49         }\r
50         \r
51         private void doImport() throws IOException {\r
52                 FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.OPEN);\r
53                 String filename = dialog.open();\r
54                 if (filename == null)\r
55                         return;\r
56                 File file = new File(filename);\r
57                 FileInputStream fis = new FileInputStream(file);\r
58                 BufferedInputStream bis = new BufferedInputStream(fis);\r
59                 byte[] data = new byte[0];\r
60                 byte[] buf = new byte[256];\r
61                 int res = 0;\r
62                 while ((res = bis.read(buf)) != -1) {\r
63                         byte[] newData = new byte[data.length + res];\r
64                         System.arraycopy(data, 0, newData, 0, data.length);\r
65                         System.arraycopy(buf, 0, newData, data.length, res);\r
66                         data = newData;\r
67                 }\r
68                 bis.close();\r
69                 final byte fdata[] = data;\r
70                 parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
71                         @Override\r
72                         public GraphRequestStatus perform(Graph g) throws Exception {\r
73                                 TransferableGraph sg = TransferableGraphUtils.deserialize(fdata);\r
74                                 ConnectionPointList purposes = TransferableGraphUtils.integrateTransferableGraph(g, sg, null);\r
75                                 Resource modelResource = purposes.getSingleByDescription(TransferableGraphUtils.CP_OBJECT).resource;\r
76                     Resource currentModelResource = parent.getModelResource();\r
77                     for (Statement s : g.getStatements(modelResource)) {\r
78                         g.removeStatements(currentModelResource, s.getPredicate());\r
79                     }\r
80                                 for (Statement s : g.getStatements(modelResource)) {\r
81                                         \r
82                                         g.removeStatement(s);\r
83                                         g.addStatement(currentModelResource, s.getPredicate(), s.getObject());\r
84                                 }\r
85                     \r
86                                 return GraphRequestStatus.transactionComplete();\r
87                         }\r
88                 });\r
89         }\r
90 \r
91 }\r