]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/MergeFlagsAction.java
Allow simulation stop handling in Simantics/Sequences
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / MergeFlagsAction.java
1 package org.simantics.modeling.ui.actions;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.ArrayList;
7
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.core.runtime.SubMonitor;
10 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
11 import org.eclipse.jface.operation.IRunnableWithProgress;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.ui.PlatformUI;
14 import org.simantics.Simantics;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.CancelTransactionException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.adapter.ActionFactory;
21 import org.simantics.modeling.flags.MergeFlags;
22 import org.simantics.utils.ui.ExceptionUtils;
23
24 /**
25  * @author Hannu Niemistö
26  * @author Tuukka Lehtonen
27  */
28 public class MergeFlagsAction implements ActionFactory {
29     
30     @Override
31     public Runnable create(Object target) {
32         if (!(target instanceof Resource))
33             return null;
34
35         Resource composite = (Resource) target;
36         final THashSet<Resource> composites = new THashSet<Resource>();
37         composites.add(composite);
38
39         return new Runnable() {
40             @Override
41             public void run() {
42                 try {
43                     IRunnableWithProgress runnable = new IRunnableWithProgress() {
44                         @Override
45                         public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
46                             final SubMonitor submonitor = SubMonitor.convert(monitor, Messages.MergeFlagsAction_MonitorMergeFlags, 1000);
47                             try {
48                                 Simantics.getSession().sync(new WriteRequest() {
49                                     @Override
50                                     public void perform(WriteGraph graph) throws DatabaseException {
51                                         graph.markUndoPoint();
52                                         
53                                         SubMonitor expand = submonitor.newChild(10);
54                                         expand.subTask(Messages.MergeFlagsAction_ExpandCompositeSet);
55                                         MergeFlags.expandCompositeSet(graph, composites);
56                                         if (monitor.isCanceled())
57                                             throw new CancelTransactionException();
58                                         expand.done();
59
60                                         SubMonitor collect = submonitor.newChild(490);
61                                         collect.subTask(Messages.MergeFlagsAction_CollectFlag);
62                                         collect.setWorkRemaining(composites.size());
63                                         ArrayList<ArrayList<Resource>> groups = new ArrayList<ArrayList<Resource>>(); 
64                                         for(Resource composite : composites) {
65                                             MergeFlags.collectFlagGroupsInComposite(graph, composite, groups);
66                                             if (monitor.isCanceled())
67                                                 throw new CancelTransactionException();
68                                             collect.worked(1);
69                                         }
70                                         collect.done();
71
72                                         SubMonitor merge = submonitor.newChild(500);
73                                         merge.subTask(Messages.MergeFlagsAction_MonitorMergeCollectedFlags);
74                                         merge.setWorkRemaining(composites.size());
75                                         for(ArrayList<Resource> group : groups) {
76                                             MergeFlags.merge(graph, group);
77                                             if (monitor.isCanceled())
78                                                 throw new CancelTransactionException();
79                                             merge.worked(1);
80                                         }
81                                         merge.done();
82                                     }
83                                 });
84                             } catch (CancelTransactionException e) {
85                             } catch (DatabaseException e) {
86                                 throw new InvocationTargetException(e);
87                             } finally {
88                                 monitor.done();
89                             }
90                         }
91                     };
92
93                     Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
94                     new ProgressMonitorDialog(shell).run(true, true, runnable);
95                 } catch (InvocationTargetException e) {
96                     ExceptionUtils.logAndShowError(e.getTargetException());
97                 } catch (InterruptedException e) {
98                 }
99             }
100         };
101     }
102
103 }