/******************************************************************************* * 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.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Collection; 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.direct.GraphUtils; import org.simantics.layer0.utils.extent.Extent; import org.simantics.layer0.utils.extent.ExtentUtils; import org.simantics.layer0.utils.extent.IExtentAdvisor; import org.simantics.layer0.utils.serialization.ConnectionPointMap; 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 ExportAction extends Action{ private ShapeEditorBase parent; public ExportAction(ShapeEditorBase parent) { this.parent = parent; this.setText("Export"); this.setId("g3d shape export"); } @Override public void run() { try { doExport(); } catch (IOException e) { ErrorLogger.defaultLogError(e); } } private void doExport() throws IOException { FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.SAVE); String filename = dialog.open(); if (filename == null) return; final File file = new File(filename); final FileOutputStream fos = new FileOutputStream(file); parent.getSession().asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Resource modelResource = parent.getModelResource(); System.out.println("Exporting " + modelResource); Collection model = ExtentUtils.determineExtent(g, new ExtentAdvisor(), modelResource); for (Statement s : model) System.out.println(GraphUtils.getReadableName(g, s.getSubject())+", "+GraphUtils.getReadableName(g, s.getPredicate())+", "+GraphUtils.getReadableName(g, s.getObject())); ConnectionPointMap purposeProvider = new ConnectionPointMap(); purposeProvider.put(modelResource, TransferableGraphUtils.CP_OBJECT); TransferableGraph dbIndependentSubgraph = TransferableGraphUtils.extractTransferableGraph(g, model, purposeProvider, null); byte[] data = TransferableGraphUtils.serialize(dbIndependentSubgraph); fos.write(data); System.out.println("Exporting done."); return GraphRequestStatus.transactionComplete(); } @Override public void requestCompleted(GraphRequestStatus status) { try { fos.close(); } catch (IOException e) { ErrorLogger.defaultLogError(e); } } }); } public class ExtentAdvisor implements IExtentAdvisor { @Override public ExtentAdvice getAdvice(Graph g, Extent currentState, Resource extent) { System.out.println("Extent advice : " + currentState + " : " + extent); //if (extent.equals(ShapeEditorResources.equationResource.Expression)) return ExtentAdvice.Include; //return ExtentAdvice.Exclude; return ExtentAdvice.Include; } } }