--- /dev/null
+package org.simantics.export.core.manager;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.export.core.ExportContext;\r
+import org.simantics.export.core.error.ExportException;\r
+\r
+/**\r
+ * This class exports content.\r
+ *\r
+ * @author toni.kalajainen@semantum.fi\r
+ * @author tuukka.lehtonen@semantum.fi\r
+ */\r
+public class ExportManager {\r
+\r
+ public Variant options;\r
+ public ExportContext ctx;\r
+\r
+ public ExportManager(Variant options, ExportContext ctx) {\r
+ this.options = options;\r
+ this.ctx = ctx;\r
+ }\r
+\r
+ /**\r
+ * Execute export plan\r
+ * \r
+ * @param ctx\r
+ * export context\r
+ * @param monitor\r
+ * the progress monitor to use for reporting progress to the\r
+ * user. It is the caller's responsibility to call done() on the\r
+ * given monitor. Accepts <code>null</code>, indicating that no\r
+ * progress should be reported and that the operation cannot be\r
+ * cancelled.</pre>\r
+ * @param plan\r
+ * export plan\r
+ * @throws ExportException\r
+ */\r
+ public void execute(ExportContext ctx, IProgressMonitor monitor, ExportPlan plan) throws ExportException\r
+ {\r
+ // 10000 units for all actions and 10 units the cleanup of each\r
+ int totalWork = plan.totalWork(ctx) * 10000 + plan.actions.size() * 100;\r
+ monitor.beginTask( plan.label, totalWork );\r
+ SubMonitor mon = SubMonitor.convert(monitor, plan.label, totalWork);\r
+\r
+ try {\r
+\r
+ try {\r
+ // 1. Export\r
+ for ( ExportAction action : plan.actions ) {\r
+ if ( monitor.isCanceled() ) return;\r
+ int work = action.work(ctx) * 10000;\r
+ mon.setTaskName( action.label(ctx) );\r
+ action.execute(ctx, mon.newChild(work), options);\r
+ }\r
+ } finally {\r
+ // 2. Cleanup\r
+ for ( ExportAction action : plan.actions ) {\r
+ mon.setTaskName( "Cleanup: " + action.label(ctx) );\r
+ try {\r
+ action.cleanup(ctx, mon.newChild(100), options);\r
+ } catch (ExportException ee) {\r
+ ee.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ } catch (ExportException ee) {\r
+ monitor.setTaskName( ee.getClass().getName()+": "+ee.getClass().getName() );\r
+ throw ee;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validate the plan is executable.\r
+ * \r
+ * @param ctx\r
+ * @param optionsBinding\r
+ * @param options\r
+ * @return null or a label describing the expected problem\r
+ */\r
+ public List<String> validate(ExportContext ctx, ExportPlan plan)\r
+ {\r
+ List<String> result = new ArrayList<String>(0);\r
+ for ( ExportAction action : plan.actions ) {\r
+ List<String> problems = action.validate(ctx, options);\r
+ result.addAll( problems );\r
+ }\r
+ return result;\r
+ }\r
+\r
+\r
+}\r