]> gerrit.simantics Code Review - simantics/3d.git/blob
152bcf965a3bbb3894a4a2c9f08d058ab69f1840
[simantics/3d.git] /
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.shapeeditor.actions;\r
12 \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
17 \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
35 \r
36 \r
37 public class ExportAction extends Action{\r
38         private ShapeEditorBase parent;\r
39         \r
40         public ExportAction(ShapeEditorBase parent) {\r
41                 this.parent = parent;\r
42                 this.setText("Export");\r
43                 this.setId("g3d shape export");\r
44         }\r
45         \r
46         @Override\r
47         public void run() {\r
48                 try {\r
49                         doExport();\r
50                 } catch (IOException e) {\r
51                         ErrorLogger.defaultLogError(e);\r
52                 }\r
53         }\r
54         \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
59                         return;\r
60                 \r
61                 final File file = new File(filename);\r
62                 final FileOutputStream fos = new FileOutputStream(file);\r
63                 \r
64                 parent.getSession().asyncRead(new GraphRequestAdapter() {\r
65                         @Override\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
70                                 \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
73                                 \r
74                                 ConnectionPointMap purposeProvider = new ConnectionPointMap();\r
75                                 purposeProvider.put(modelResource, TransferableGraphUtils.CP_OBJECT);\r
76                                 \r
77                                 TransferableGraph dbIndependentSubgraph = TransferableGraphUtils.extractTransferableGraph(g, model, purposeProvider, null); \r
78                                 \r
79                                 byte[] data = TransferableGraphUtils.serialize(dbIndependentSubgraph);\r
80                                 \r
81                                 fos.write(data);\r
82                                 \r
83                                 System.out.println("Exporting done.");\r
84                                 return GraphRequestStatus.transactionComplete();\r
85                         }\r
86                         \r
87                         @Override\r
88                         public void requestCompleted(GraphRequestStatus status) {\r
89                                 try {\r
90                                         fos.close();\r
91                                 } catch (IOException e) {\r
92                                         ErrorLogger.defaultLogError(e);\r
93                                 }\r
94                                 \r
95                         }\r
96                 });\r
97         }\r
98         \r
99         public class ExtentAdvisor implements IExtentAdvisor {\r
100                 @Override\r
101                 public ExtentAdvice getAdvice(Graph g, Extent currentState, Resource extent) {\r
102                         System.out.println("Extent advice : " + currentState + " : " + extent);\r
103                         \r
104                         //if (extent.equals(ShapeEditorResources.equationResource.Expression)) return ExtentAdvice.Include;\r
105                         //return ExtentAdvice.Exclude;\r
106                         return ExtentAdvice.Include;\r
107                 }\r
108         }\r
109 \r
110 }\r