1 package org.simantics.proconf.g3d.shapeeditor.actions;
\r
3 import java.io.BufferedInputStream;
\r
5 import java.io.FileInputStream;
\r
6 import java.io.IOException;
\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.ErrorLogger;
\r
22 public class ImportAction extends Action {
\r
24 private ShapeEditorBase parent;
\r
26 public ImportAction(ShapeEditorBase parent) {
\r
27 this.parent = parent;
\r
29 setId("g3d shape import");
\r
36 } catch (IOException e) {
\r
37 ErrorLogger.defaultLogError(e);
\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
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
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
59 final byte fdata[] = data;
\r
60 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\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
70 for (Statement s : g.getStatements(modelResource)) {
\r
72 g.removeStatement(s);
\r
73 g.addStatement(currentModelResource, s.getPredicate(), s.getObject());
\r
76 return GraphRequestStatus.transactionComplete();
\r