]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportManager.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / manager / ExportManager.java
1 package org.simantics.export.core.manager;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 import org.eclipse.core.runtime.IProgressMonitor;\r
7 import org.eclipse.core.runtime.SubMonitor;\r
8 import org.simantics.databoard.binding.mutable.Variant;\r
9 import org.simantics.export.core.ExportContext;\r
10 import org.simantics.export.core.error.ExportException;\r
11 \r
12 /**\r
13  * This class exports content.\r
14  *\r
15  * @author toni.kalajainen@semantum.fi\r
16  * @author tuukka.lehtonen@semantum.fi\r
17  */\r
18 public class ExportManager {\r
19 \r
20         public Variant options;\r
21         public ExportContext ctx;\r
22 \r
23         public ExportManager(Variant options, ExportContext ctx) {\r
24                 this.options = options;\r
25                 this.ctx = ctx;\r
26         }\r
27 \r
28         /**\r
29          * Execute export plan\r
30          * \r
31          * @param ctx\r
32          *            export context\r
33          * @param monitor\r
34          *            the progress monitor to use for reporting progress to the\r
35          *            user. It is the caller's responsibility to call done() on the\r
36          *            given monitor. Accepts <code>null</code>, indicating that no\r
37          *            progress should be reported and that the operation cannot be\r
38          *            cancelled.</pre>\r
39          * @param plan\r
40          *            export plan\r
41          * @throws ExportException\r
42          */\r
43         public void execute(ExportContext ctx, IProgressMonitor monitor, ExportPlan plan) throws ExportException\r
44         {\r
45                 // 10000 units for all actions and 10 units the cleanup of each\r
46                 int totalWork = plan.totalWork(ctx) * 10000 + plan.actions.size() * 100;\r
47                 monitor.beginTask( plan.label, totalWork );\r
48                 SubMonitor mon = SubMonitor.convert(monitor, plan.label, totalWork);\r
49 \r
50                 try {\r
51 \r
52                         try {\r
53                                 // 1. Export\r
54                                 for ( ExportAction action : plan.actions ) {\r
55                                         if ( monitor.isCanceled() ) return;\r
56                                         int work = action.work(ctx) * 10000;\r
57                                         mon.setTaskName( action.label(ctx) );\r
58                                         action.execute(ctx, mon.newChild(work), options);\r
59                                 }\r
60                         } finally {\r
61                                 // 2. Cleanup\r
62                                 for ( ExportAction action : plan.actions ) {\r
63                                         mon.setTaskName( "Cleanup: " + action.label(ctx) );\r
64                                         try {\r
65                                                 action.cleanup(ctx, mon.newChild(100), options);\r
66                                         } catch (ExportException ee) {\r
67                                                 ee.printStackTrace();\r
68                                         }\r
69                                 }\r
70                         }\r
71 \r
72                 } catch (ExportException ee) {\r
73                         monitor.setTaskName( ee.getClass().getName()+": "+ee.getClass().getName() );\r
74                         throw ee;\r
75                 }\r
76         }\r
77 \r
78         /**\r
79          * Validate the plan is executable.\r
80          * \r
81          * @param ctx\r
82          * @param optionsBinding\r
83          * @param options\r
84          * @return null or a label describing the expected problem\r
85          */\r
86         public List<String> validate(ExportContext ctx, ExportPlan plan)\r
87         {\r
88                 List<String> result = new ArrayList<String>(0);\r
89                 for ( ExportAction action : plan.actions ) {\r
90                         List<String> problems = action.validate(ctx, options);\r
91                         result.addAll( problems );\r
92                 }\r
93                 return result;\r
94         }\r
95 \r
96 \r
97 }\r