X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.proconf.g3d.shapeeditor%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fshapeeditor%2Factions%2FImportAction.java;fp=org.simantics.proconf.g3d.shapeeditor%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fshapeeditor%2Factions%2FImportAction.java;h=7a4d063ab5b1f8a148489122b084fb0282a57c5c;hb=477a3eae417fe71addfcf8f87dab41f87151a384;hp=0000000000000000000000000000000000000000;hpb=d0db32bb067d2a7c323054295f6356bdb42078c9;p=simantics%2F3d.git diff --git a/org.simantics.proconf.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ImportAction.java b/org.simantics.proconf.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ImportAction.java new file mode 100644 index 00000000..7a4d063a --- /dev/null +++ b/org.simantics.proconf.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ImportAction.java @@ -0,0 +1,81 @@ +package org.simantics.proconf.g3d.shapeeditor.actions; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.eclipse.jface.action.Action; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.simantics.db.Graph; +import org.simantics.db.GraphRequestAdapter; +import org.simantics.db.GraphRequestStatus; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.layer0.utils.serialization.ConnectionPointList; +import org.simantics.layer0.utils.serialization.TransferableGraph; +import org.simantics.layer0.utils.serialization.TransferableGraphUtils; +import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase; +import org.simantics.utils.ErrorLogger; + +public class ImportAction extends Action { + + private ShapeEditorBase parent; + + public ImportAction(ShapeEditorBase parent) { + this.parent = parent; + setText("Import"); + setId("g3d shape import"); + } + + @Override + public void run() { + try { + doImport(); + } catch (IOException e) { + ErrorLogger.defaultLogError(e); + } + } + + private void doImport() throws IOException { + FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.OPEN); + String filename = dialog.open(); + if (filename == null) + return; + File file = new File(filename); + FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + byte[] data = new byte[0]; + byte[] buf = new byte[256]; + int res = 0; + while ((res = bis.read(buf)) != -1) { + byte[] newData = new byte[data.length + res]; + System.arraycopy(data, 0, newData, 0, data.length); + System.arraycopy(buf, 0, newData, data.length, res); + data = newData; + } + bis.close(); + final byte fdata[] = data; + parent.getSession().asyncWrite(new GraphRequestAdapter() { + @Override + public GraphRequestStatus perform(Graph g) throws Exception { + TransferableGraph sg = TransferableGraphUtils.deserialize(fdata); + ConnectionPointList purposes = TransferableGraphUtils.integrateTransferableGraph(g, sg, null); + Resource modelResource = purposes.getSingleByDescription(TransferableGraphUtils.CP_OBJECT).resource; + Resource currentModelResource = parent.getModelResource(); + for (Statement s : g.getStatements(modelResource)) { + g.removeStatements(currentModelResource, s.getPredicate()); + } + for (Statement s : g.getStatements(modelResource)) { + + g.removeStatement(s); + g.addStatement(currentModelResource, s.getPredicate(), s.getObject()); + } + + return GraphRequestStatus.transactionComplete(); + } + }); + } + +}