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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.shapeeditor.actions;
\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
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
32 public class ImportAction extends Action {
\r
34 private ShapeEditorBase parent;
\r
36 public ImportAction(ShapeEditorBase parent) {
\r
37 this.parent = parent;
\r
39 setId("g3d shape import");
\r
46 } catch (IOException e) {
\r
47 ErrorLogger.defaultLogError(e);
\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
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
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
69 final byte fdata[] = data;
\r
70 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\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
80 for (Statement s : g.getStatements(modelResource)) {
\r
82 g.removeStatement(s);
\r
83 g.addStatement(currentModelResource, s.getPredicate(), s.getObject());
\r
86 return GraphRequestStatus.transactionComplete();
\r