]> gerrit.simantics Code Review - simantics/3d.git/blob
0760d9bd2d0f534cf82cef94efc3065a1d288c8f
[simantics/3d.git] /
1 package org.simantics.proconf.g3d.shapeeditor.actions;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileOutputStream;\r
5 import java.io.IOException;\r
6 import java.util.Collection;\r
7 \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
25 \r
26 \r
27 public class ExportAction extends Action{\r
28         private ShapeEditorBase parent;\r
29         \r
30         public ExportAction(ShapeEditorBase parent) {\r
31                 this.parent = parent;\r
32                 this.setText("Export");\r
33                 this.setId("g3d shape export");\r
34         }\r
35         \r
36         @Override\r
37         public void run() {\r
38                 try {\r
39                         doExport();\r
40                 } catch (IOException e) {\r
41                         ErrorLogger.defaultLogError(e);\r
42                 }\r
43         }\r
44         \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
49                         return;\r
50                 \r
51                 final File file = new File(filename);\r
52                 final FileOutputStream fos = new FileOutputStream(file);\r
53                 \r
54                 parent.getSession().asyncRead(new GraphRequestAdapter() {\r
55                         @Override\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
60                                 \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
63                                 \r
64                                 ConnectionPointMap purposeProvider = new ConnectionPointMap();\r
65                                 purposeProvider.put(modelResource, TransferableGraphUtils.CP_OBJECT);\r
66                                 \r
67                                 TransferableGraph dbIndependentSubgraph = TransferableGraphUtils.extractTransferableGraph(g, model, purposeProvider, null); \r
68                                 \r
69                                 byte[] data = TransferableGraphUtils.serialize(dbIndependentSubgraph);\r
70                                 \r
71                                 fos.write(data);\r
72                                 \r
73                                 System.out.println("Exporting done.");\r
74                                 return GraphRequestStatus.transactionComplete();\r
75                         }\r
76                         \r
77                         @Override\r
78                         public void requestCompleted(GraphRequestStatus status) {\r
79                                 try {\r
80                                         fos.close();\r
81                                 } catch (IOException e) {\r
82                                         ErrorLogger.defaultLogError(e);\r
83                                 }\r
84                                 \r
85                         }\r
86                 });\r
87         }\r
88         \r
89         public class ExtentAdvisor implements IExtentAdvisor {\r
90                 @Override\r
91                 public ExtentAdvice getAdvice(Graph g, Extent currentState, Resource extent) {\r
92                         System.out.println("Extent advice : " + currentState + " : " + extent);\r
93                         \r
94                         //if (extent.equals(ShapeEditorResources.equationResource.Expression)) return ExtentAdvice.Include;\r
95                         //return ExtentAdvice.Exclude;\r
96                         return ExtentAdvice.Include;\r
97                 }\r
98         }\r
99 \r
100 }\r