From: Jussi Koskela Date: Mon, 5 Nov 2018 09:06:12 +0000 (+0200) Subject: Refactor DeleteHandler to be independent from UI X-Git-Tag: v1.43.0~136^2~272 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=2bc244dcb1f23667bb26da82da7d484d227467c4 Refactor DeleteHandler to be independent from UI Change-Id: I0000000000000000000000000000000000000000 (cherry picked from commit cff3197e30752cf687fa8a7c037160a798b3b999) --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java index 5fa94ebc7..81e610346 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java @@ -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 ss) { - TimeLogger.resetTimeAndLog(getClass(), "delete"); + public static boolean delete(final IDiagram d, Collection ss, Consumer exceptionListener, Consumer 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(); } }