/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ 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.ui.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(); } }); } }