]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Refactor DeleteHandler to be independent from UI
authorJussi Koskela <jussi.koskela@semantum.fi>
Mon, 5 Nov 2018 09:06:12 +0000 (11:06 +0200)
committerJussi Koskela <jussi.koskela@semantum.fi>
Mon, 5 Nov 2018 09:06:12 +0000 (11:06 +0200)
Change-Id: I0000000000000000000000000000000000000000

bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java

index 5fa94ebc7a2787d1c9b3a51db9fe508ef04106dc..81e6103463d214d28decad76e6cd380117087e58 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Deque;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.function.Consumer;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -35,11 +36,9 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.adapter.Remover;
 import org.simantics.db.layer0.exception.CannotRemoveException;
 import org.simantics.db.layer0.util.RemoverUtil;
-import org.simantics.diagram.adapter.ElementFactoryUtil;
 import org.simantics.diagram.content.ConnectionUtil;
 import org.simantics.diagram.content.EdgeResource;
 import org.simantics.diagram.internal.Activator;
-import org.simantics.diagram.synchronization.ISynchronizationContext;
 import org.simantics.diagram.synchronization.graph.RemoveBranchpoint;
 import org.simantics.diagram.synchronization.graph.RemoveElement;
 import org.simantics.diagram.ui.DiagramModelHints;
@@ -119,7 +118,16 @@ public class DeleteHandler extends AbstractDiagramParticipant {
             if (ss.isEmpty())
                 return true;
 
-            if (delete(d, ss)) {
+            if (delete(d, ss,
+                    ex -> {
+                        if (ex instanceof CannotRemoveException) {
+                            ShowMessage.showInformation("Delete Selection Was Denied", ex.getLocalizedMessage());
+                        }
+                    },
+                    error -> {
+                        error(error);
+                    }
+                    )) {
                 sel.clear(0);
             }
 
@@ -128,8 +136,8 @@ public class DeleteHandler extends AbstractDiagramParticipant {
         return false;
     }
 
-    public boolean delete(final IDiagram d, Collection<IElement> ss) {
-        TimeLogger.resetTimeAndLog(getClass(), "delete");
+    public static boolean delete(final IDiagram d, Collection<IElement> ss, Consumer<Exception> exceptionListener, Consumer<String> errorListener) {
+        TimeLogger.resetTimeAndLog(DeleteHandler.class, "delete");
         
         Topology topology = d.getDiagramClass().getAtMostOneItemOfClass(Topology.class);
         RelationshipHandler erh = d.getDiagramClass().getAtMostOneItemOfClass(RelationshipHandler.class);
@@ -220,7 +228,9 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                 // Try to work with cases where the model is somewhat corrupt.
                 if (begin != null && end != null) {
                     if (PickFilter.FILTER_BRANCH_POINT.accept(begin.node) && PickFilter.FILTER_BRANCH_POINT.accept(end.node)) {
-                        error("Deletion of branch point connecting edges is not allowed. Must be connected to a node terminal.");
+                        if (errorListener != null) {
+                            errorListener.accept("Deletion of branch point connecting edges is not allowed. Must be connected to a node terminal.");
+                        }
                         return false;
                     }
                 }
@@ -266,9 +276,6 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                     System.out.println("\t\t" + e);
         }
 
-        final IDiagram diagram = this.diagram;
-        final ISynchronizationContext syncContext = ElementFactoryUtil.getContextChecked(diagram); 
-
         new DatabaseJob("Delete selection") {
             @Override
             protected IStatus run(IProgressMonitor monitor) {
@@ -276,12 +283,15 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                     delete(monitor);
                     return Status.OK_STATUS;
                 } catch (CannotRemoveException e) {
-                       ShowMessage.showInformation("Delete Selection Was Denied", e.getLocalizedMessage());
+                    if (exceptionListener != null) exceptionListener.accept(e);
                     return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
                 } catch (DatabaseException e) {
+                    if (exceptionListener != null) exceptionListener.accept(e);
                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unexpected error in delete.", e);
                 } finally {
-                    error(null);
+                    if (errorListener != null) {
+                        errorListener.accept(null);
+                    }
                     monitor.done();
                 }
             }