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.File;
\r
14 import java.io.FileOutputStream;
\r
15 import java.io.IOException;
\r
16 import java.util.Collection;
\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.direct.GraphUtils;
\r
27 import org.simantics.layer0.utils.extent.Extent;
\r
28 import org.simantics.layer0.utils.extent.ExtentUtils;
\r
29 import org.simantics.layer0.utils.extent.IExtentAdvisor;
\r
30 import org.simantics.layer0.utils.serialization.ConnectionPointMap;
\r
31 import org.simantics.layer0.utils.serialization.TransferableGraph;
\r
32 import org.simantics.layer0.utils.serialization.TransferableGraphUtils;
\r
33 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;
\r
34 import org.simantics.utils.ui.ErrorLogger;
\r
37 public class ExportAction extends Action{
\r
38 private ShapeEditorBase parent;
\r
40 public ExportAction(ShapeEditorBase parent) {
\r
41 this.parent = parent;
\r
42 this.setText("Export");
\r
43 this.setId("g3d shape export");
\r
50 } catch (IOException e) {
\r
51 ErrorLogger.defaultLogError(e);
\r
55 private void doExport() throws IOException {
\r
56 FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.SAVE);
\r
57 String filename = dialog.open();
\r
58 if (filename == null)
\r
61 final File file = new File(filename);
\r
62 final FileOutputStream fos = new FileOutputStream(file);
\r
64 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
66 public GraphRequestStatus perform(Graph g) throws Exception {
\r
67 Resource modelResource = parent.getModelResource();
\r
68 System.out.println("Exporting " + modelResource);
\r
69 Collection<Statement> model = ExtentUtils.determineExtent(g, new ExtentAdvisor(), modelResource);
\r
71 for (Statement s : model)
\r
72 System.out.println(GraphUtils.getReadableName(g, s.getSubject())+", "+GraphUtils.getReadableName(g, s.getPredicate())+", "+GraphUtils.getReadableName(g, s.getObject()));
\r
74 ConnectionPointMap purposeProvider = new ConnectionPointMap();
\r
75 purposeProvider.put(modelResource, TransferableGraphUtils.CP_OBJECT);
\r
77 TransferableGraph dbIndependentSubgraph = TransferableGraphUtils.extractTransferableGraph(g, model, purposeProvider, null);
\r
79 byte[] data = TransferableGraphUtils.serialize(dbIndependentSubgraph);
\r
83 System.out.println("Exporting done.");
\r
84 return GraphRequestStatus.transactionComplete();
\r
88 public void requestCompleted(GraphRequestStatus status) {
\r
91 } catch (IOException e) {
\r
92 ErrorLogger.defaultLogError(e);
\r
99 public class ExtentAdvisor implements IExtentAdvisor {
\r
101 public ExtentAdvice getAdvice(Graph g, Extent currentState, Resource extent) {
\r
102 System.out.println("Extent advice : " + currentState + " : " + extent);
\r
104 //if (extent.equals(ShapeEditorResources.equationResource.Expression)) return ExtentAdvice.Include;
\r
105 //return ExtentAdvice.Exclude;
\r
106 return ExtentAdvice.Include;
\r