1 package org.simantics.modeling.ui.actions;
3 import gnu.trove.set.hash.THashSet;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.ArrayList;
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;
25 * @author Hannu Niemistö
26 * @author Tuukka Lehtonen
28 public class MergeFlagsAction implements ActionFactory {
31 public Runnable create(Object target) {
32 if (!(target instanceof Resource))
35 Resource composite = (Resource) target;
36 final THashSet<Resource> composites = new THashSet<Resource>();
37 composites.add(composite);
39 return new Runnable() {
43 IRunnableWithProgress runnable = new IRunnableWithProgress() {
45 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
46 final SubMonitor submonitor = SubMonitor.convert(monitor, "Merge Flags", 1000);
48 Simantics.getSession().sync(new WriteRequest() {
50 public void perform(WriteGraph graph) throws DatabaseException {
51 graph.markUndoPoint();
53 SubMonitor expand = submonitor.newChild(10);
54 expand.subTask("Expand Composite Set");
55 MergeFlags.expandCompositeSet(graph, composites);
56 if (monitor.isCanceled())
57 throw new CancelTransactionException();
60 SubMonitor collect = submonitor.newChild(490);
61 collect.subTask("Collect flags");
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();
72 SubMonitor merge = submonitor.newChild(500);
73 merge.subTask("Merge collected flags");
74 merge.setWorkRemaining(composites.size());
75 for(ArrayList<Resource> group : groups) {
76 MergeFlags.merge(graph, group);
77 if (monitor.isCanceled())
78 throw new CancelTransactionException();
84 } catch (CancelTransactionException e) {
85 } catch (DatabaseException e) {
86 throw new InvocationTargetException(e);
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) {