]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/MergeFlagsHandler.java
Allow simulation stop handling in Simantics/Sequences
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / MergeFlagsHandler.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.ui.PlatformUI;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.diagram.synchronization.runtime.DiagramSelectionUpdater;
12 import org.simantics.g2d.canvas.ICanvasContext;
13 import org.simantics.modeling.flags.MergeFlags;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author Hannu Niemistö
19  * @author Tuukka Lehtonen
20  */
21 public class MergeFlagsHandler extends FlagOperationHandler {
22
23     private static final Logger LOGGER = LoggerFactory.getLogger(MergeFlagsHandler.class);
24     protected void perform(IProgressMonitor monitor, WriteGraph graph, List<Resource> flags, ICanvasContext canvasContext) throws DatabaseException {
25         monitor.beginTask(Messages.MergeFlagsHandler_MonitorMergeSelectedFlags, IProgressMonitor.UNKNOWN);
26         performMerge(graph, flags, canvasContext);
27     }
28
29     public static void performMerge(WriteGraph graph, List<Resource> flags, ICanvasContext canvasContext) throws DatabaseException {
30         final String text = MergeFlags.validateForMerge(graph, flags);
31         if(text != null) {
32             LOGGER.error(text);
33             PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
34                 @Override
35                 public void run() {
36                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
37                     .getActiveEditor().getEditorSite().getActionBars().getStatusLineManager()
38                     .setErrorMessage(text);
39                 }
40             });
41         }
42         else {
43             String result = MergeFlags.merge(graph, flags);
44             if(result != null && !result.isEmpty()) {
45                 LOGGER.error(result); // Should not happen because of prevalidation
46             } else {
47                 if (canvasContext != null) {
48                     // Make sure the diagram selection is set to the
49                     // merged flag after the merge.
50                     new DiagramSelectionUpdater( canvasContext )
51                     .setNewSelection(0, Collections.singleton( flags.get(0) ))
52                     .setOneshot(true)
53                     .track();
54                 }
55             }
56         }
57     }
58
59 }