1 package org.simantics.proconf.g3d.shapeeditor.actions;
\r
4 import java.io.FileOutputStream;
\r
5 import java.io.IOException;
\r
6 import java.util.Collection;
\r
8 import org.eclipse.jface.action.Action;
\r
9 import org.eclipse.swt.SWT;
\r
10 import org.eclipse.swt.widgets.FileDialog;
\r
11 import org.simantics.db.Graph;
\r
12 import org.simantics.db.GraphRequestAdapter;
\r
13 import org.simantics.db.GraphRequestStatus;
\r
14 import org.simantics.db.Resource;
\r
15 import org.simantics.db.Statement;
\r
16 import org.simantics.layer0.utils.direct.GraphUtils;
\r
17 import org.simantics.layer0.utils.extent.Extent;
\r
18 import org.simantics.layer0.utils.extent.ExtentUtils;
\r
19 import org.simantics.layer0.utils.extent.IExtentAdvisor;
\r
20 import org.simantics.layer0.utils.serialization.ConnectionPointMap;
\r
21 import org.simantics.layer0.utils.serialization.TransferableGraph;
\r
22 import org.simantics.layer0.utils.serialization.TransferableGraphUtils;
\r
23 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;
\r
24 import org.simantics.utils.ui.ErrorLogger;
\r
27 public class ExportAction extends Action{
\r
28 private ShapeEditorBase parent;
\r
30 public ExportAction(ShapeEditorBase parent) {
\r
31 this.parent = parent;
\r
32 this.setText("Export");
\r
33 this.setId("g3d shape export");
\r
40 } catch (IOException e) {
\r
41 ErrorLogger.defaultLogError(e);
\r
45 private void doExport() throws IOException {
\r
46 FileDialog dialog = new FileDialog(parent.getRenderingComposite().getShell(),SWT.SAVE);
\r
47 String filename = dialog.open();
\r
48 if (filename == null)
\r
51 final File file = new File(filename);
\r
52 final FileOutputStream fos = new FileOutputStream(file);
\r
54 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
56 public GraphRequestStatus perform(Graph g) throws Exception {
\r
57 Resource modelResource = parent.getModelResource();
\r
58 System.out.println("Exporting " + modelResource);
\r
59 Collection<Statement> model = ExtentUtils.determineExtent(g, new ExtentAdvisor(), modelResource);
\r
61 for (Statement s : model)
\r
62 System.out.println(GraphUtils.getReadableName(g, s.getSubject())+", "+GraphUtils.getReadableName(g, s.getPredicate())+", "+GraphUtils.getReadableName(g, s.getObject()));
\r
64 ConnectionPointMap purposeProvider = new ConnectionPointMap();
\r
65 purposeProvider.put(modelResource, TransferableGraphUtils.CP_OBJECT);
\r
67 TransferableGraph dbIndependentSubgraph = TransferableGraphUtils.extractTransferableGraph(g, model, purposeProvider, null);
\r
69 byte[] data = TransferableGraphUtils.serialize(dbIndependentSubgraph);
\r
73 System.out.println("Exporting done.");
\r
74 return GraphRequestStatus.transactionComplete();
\r
78 public void requestCompleted(GraphRequestStatus status) {
\r
81 } catch (IOException e) {
\r
82 ErrorLogger.defaultLogError(e);
\r
89 public class ExtentAdvisor implements IExtentAdvisor {
\r
91 public ExtentAdvice getAdvice(Graph g, Extent currentState, Resource extent) {
\r
92 System.out.println("Extent advice : " + currentState + " : " + extent);
\r
94 //if (extent.equals(ShapeEditorResources.equationResource.Expression)) return ExtentAdvice.Include;
\r
95 //return ExtentAdvice.Exclude;
\r
96 return ExtentAdvice.Include;
\r