X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.shapeeditor%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fshapeeditor%2Factions%2FExportAction.java;fp=org.simantics.g3d.shapeeditor%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fshapeeditor%2Factions%2FExportAction.java;h=152bcf965a3bbb3894a4a2c9f08d058ab69f1840;hb=8b42fcb0b43efb8caaca85dd722ae608b9a3a730;hp=0000000000000000000000000000000000000000;hpb=55702d303318f9d3dc0b5e20010cfc2c4ae0b88e;p=simantics%2F3d.git diff --git a/org.simantics.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ExportAction.java b/org.simantics.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ExportAction.java new file mode 100644 index 00000000..152bcf96 --- /dev/null +++ b/org.simantics.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/actions/ExportAction.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * 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; + } + } + +}