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