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;
import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.widgets.Display;
import org.simantics.DatabaseJob;
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;
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);
}
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);
// 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;
}
}
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) {
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();
}
}
private void delete(IProgressMonitor monitor) throws DatabaseException {
+ SubMonitor subMonitor = SubMonitor.convert(monitor, 4);
Simantics.getSession().syncRequest(new WriteRequest() {
Set<Resource> connectionsToRemove = new HashSet<Resource>();
Set<Resource> touchedConnections = new HashSet<Resource>();
graph.markUndoPoint();
ConnectionUtil cu = new ConnectionUtil(graph);
-
+ SubMonitor edgeElementMonitor = subMonitor.split(2); // Consume 50 % of the monitor
+ edgeElementMonitor.setWorkRemaining(edges.size() + elements.size()); // set work remaining to size of edges and elements
+ edgeElementMonitor.setTaskName("Removing edges");
// Remove edges
for (IElement edge : edges) {
ConnectionEntity ce = edge.getHint(ElementHints.KEY_CONNECTION_ENTITY);
if (obj instanceof EdgeResource) {
cu.remove((EdgeResource) obj);
}
+ edgeElementMonitor.worked(1);
}
-
+ edgeElementMonitor.setTaskName("Removing elements");
// Remove elements
for (IElement element : elements) {
ConnectionHandler ch = element.getElementClass().getAtMostOneItemOfClass(ConnectionHandler.class);
}
}
}
+ edgeElementMonitor.worked(1);
}
-
+ SubMonitor connectionsMonitor = subMonitor.split(1); // Consume 50 % of the monitor
+ connectionsMonitor.setWorkRemaining(touchedConnections.size()); // set work remaining to size of edges and elements
+ connectionsMonitor.setTaskName("Pruning connectors");
// Check all touched connections to see if they are empty.
for (Resource connection : touchedConnections) {
int removedConnectors = cu.removeUnusedConnectors(connection);
if (connectors < 2) {
connectionsToRemove.add(connection);
}
+ connectionsMonitor.worked(1);
}
-
+ SubMonitor connectionsMonitor2 = subMonitor.split(1); // Consume 50 % of the monitor
+ connectionsMonitor2.setWorkRemaining(connectionsToRemove.size()); // set work remaini
+ connectionsMonitor2.setTaskName("Removing leftover connections");
// Remove selected/left-over empty connections
for (Resource connection : connectionsToRemove) {
if (DEBUG_DELETE)
System.out.println("REMOVING CONNECTION: " + connection);
RemoveElement.removeElement(graph, (Resource)d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE), connection);
+ connectionsMonitor2.worked(1);
}
}