Remove unused import in DeleteHandler
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / DeleteHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.handler;\r
13 \r
14 import java.util.ArrayDeque;\r
15 import java.util.ArrayList;\r
16 import java.util.Collection;\r
17 import java.util.Deque;\r
18 import java.util.HashMap;\r
19 import java.util.HashSet;\r
20 import java.util.Set;\r
21 \r
22 import org.eclipse.core.runtime.IProgressMonitor;\r
23 import org.eclipse.core.runtime.IStatus;\r
24 import org.eclipse.core.runtime.Status;\r
25 import org.eclipse.jface.action.IStatusLineManager;\r
26 import org.eclipse.swt.widgets.Display;\r
27 import org.simantics.DatabaseJob;\r
28 import org.simantics.Simantics;\r
29 import org.simantics.db.ReadGraph;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.WriteGraph;\r
32 import org.simantics.db.common.request.WriteRequest;\r
33 import org.simantics.db.exception.DatabaseException;\r
34 import org.simantics.db.layer0.adapter.Remover;\r
35 import org.simantics.db.layer0.exception.CannotRemoveException;\r
36 import org.simantics.db.layer0.util.RemoverUtil;\r
37 import org.simantics.diagram.adapter.ElementFactoryUtil;\r
38 import org.simantics.diagram.content.ConnectionUtil;\r
39 import org.simantics.diagram.content.EdgeResource;\r
40 import org.simantics.diagram.internal.Activator;\r
41 import org.simantics.diagram.synchronization.ISynchronizationContext;\r
42 import org.simantics.diagram.synchronization.graph.RemoveBranchpoint;\r
43 import org.simantics.diagram.synchronization.graph.RemoveElement;\r
44 import org.simantics.diagram.ui.DiagramModelHints;\r
45 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
46 import org.simantics.g2d.connection.ConnectionEntity;\r
47 import org.simantics.g2d.connection.handler.ConnectionHandler;\r
48 import org.simantics.g2d.diagram.IDiagram;\r
49 import org.simantics.g2d.diagram.handler.PickRequest.PickFilter;\r
50 import org.simantics.g2d.diagram.handler.Relationship;\r
51 import org.simantics.g2d.diagram.handler.RelationshipHandler;\r
52 import org.simantics.g2d.diagram.handler.RelationshipHandler.Relation;\r
53 import org.simantics.g2d.diagram.handler.Topology;\r
54 import org.simantics.g2d.diagram.handler.Topology.Connection;\r
55 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
56 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
57 import org.simantics.g2d.diagram.participant.Selection;\r
58 import org.simantics.g2d.element.ElementClass;\r
59 import org.simantics.g2d.element.ElementHints;\r
60 import org.simantics.g2d.element.ElementUtils;\r
61 import org.simantics.g2d.element.IElement;\r
62 import org.simantics.g2d.element.handler.BendsHandler;\r
63 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
64 import org.simantics.g2d.element.handler.TerminalTopology;\r
65 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
66 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
67 import org.simantics.scenegraph.g2d.events.command.Commands;\r
68 import org.simantics.utils.logging.TimeLogger;\r
69 import org.simantics.utils.strings.EString;\r
70 import org.simantics.utils.threads.SWTThread;\r
71 import org.simantics.utils.threads.ThreadUtils;\r
72 import org.simantics.utils.ui.dialogs.ShowMessage;\r
73 \r
74 /**\r
75  * DeleteHandler is a canvas handler for Commands.DELETE commands for an\r
76  * IDiagram.\r
77  * \r
78  * <p>\r
79  * The handler attempts to delete the current selection for pointer 0, meaning\r
80  * {@link Selection#SELECTION0}.\r
81  * </p>\r
82  * \r
83  * <p>\r
84  * The handler logic goes as follows:\r
85  * </p>\r
86  * <ol>\r
87  * <li>Separate nodes and edges form the the removed selection</li>\r
88  * <li>Find all edges attached to the removed nodes and remove them too</li>\r
89  * <li>Delete connections that contain less than 2 terminal connections</li>\r
90  * </ol>\r
91  * \r
92  * @see Selection for the current diagram selection source\r
93  * \r
94  * @author Tuukka Lehtonen\r
95  * \r
96  * TODO: start using WorkbenchStatusLine participant\r
97  */\r
98 public class DeleteHandler extends AbstractDiagramParticipant {\r
99 \r
100     public static final boolean DEBUG_DELETE = false;\r
101 \r
102     @Dependency Selection sel;\r
103 \r
104     private final IStatusLineManager statusLine;\r
105 \r
106     public DeleteHandler(IStatusLineManager statusLine) {\r
107         this.statusLine = statusLine;\r
108     }\r
109 \r
110     @EventHandler(priority = 0)\r
111     public boolean handleCommand(CommandEvent e) {\r
112         if (Commands.DELETE.equals( e.command )) {\r
113             IDiagram d = diagram;\r
114             if (d == null)\r
115                 return true;\r
116 \r
117             Set<IElement> ss = sel.getSelection(0);\r
118             if (ss.isEmpty())\r
119                 return true;\r
120 \r
121             if (delete(d, ss)) {\r
122                 sel.clear(0);\r
123             }\r
124 \r
125             return true;\r
126         }\r
127         return false;\r
128     }\r
129 \r
130     public boolean delete(final IDiagram d, Collection<IElement> ss) {\r
131         TimeLogger.resetTimeAndLog(getClass(), "delete");\r
132         \r
133         Topology topology = d.getDiagramClass().getAtMostOneItemOfClass(Topology.class);\r
134         RelationshipHandler erh = d.getDiagramClass().getAtMostOneItemOfClass(RelationshipHandler.class);\r
135 \r
136         if (DEBUG_DELETE) {\r
137             System.out.println("diagram: " + d);\r
138             for (IElement e : d.getSnapshot()) {\r
139                 ElementClass ec = e.getElementClass();\r
140                 System.out.println("\t-element " + e);\r
141                 System.out.println("\t  -class " + e.getElementClass());\r
142                 if (ec.containsClass(ConnectionHandler.class)) {\r
143                     ConnectionEntity ce = e.getHint(ElementHints.KEY_CONNECTION_ENTITY);\r
144                     for (IElement child : ce.getBranchPoints(null)) {\r
145                         System.out.println("\t\t-branch " + child);\r
146                         System.out.println("\t\t  -class " + child.getElementClass());\r
147                     }\r
148                     for (IElement child : ce.getSegments(null)) {\r
149                         System.out.println("\t\t-segment " + child);\r
150                         System.out.println("\t\t  -class " + child.getElementClass());\r
151                     }\r
152                 }\r
153             }\r
154             System.out.println("delete requested for elements:");\r
155             for (IElement e : ss)\r
156                 System.out.println("\t-element " + e);\r
157         }\r
158 \r
159         // Analyze removals:\r
160         //  - separate elements and connections\r
161         //  - find all connections attached to the elements and remove them too\r
162         Deque<IElement> elementsToProcess = new ArrayDeque<IElement>(ss);\r
163         Set<IElement> processedElements = new HashSet<IElement>();\r
164         Set<IElement> relationshipsProcessedForElement = new HashSet<IElement>();\r
165 \r
166         final Collection<IElement> elements = new ArrayList<IElement>();\r
167         final Set<IElement> edges = new HashSet<IElement>();\r
168         Collection<Connection> connections = new ArrayList<Connection>();\r
169         Collection<Terminal> terminals = new ArrayList<Terminal>();\r
170         Collection<Relation> relations = new ArrayList<Relation>();\r
171         while (!elementsToProcess.isEmpty()) {\r
172             IElement el = elementsToProcess.pollFirst();\r
173 \r
174             if (relationshipsProcessedForElement.add(el)) {\r
175                 // Check for relationships to other elements and mark child\r
176                 // elements to be removed before the parent element.\r
177                 relations.clear();\r
178                 erh.getRelations(d, el, relations);\r
179                 if (!relations.isEmpty()) {\r
180                     boolean restart = false;\r
181                     for (Relation r : relations) {\r
182                         //System.out.println("FOUND RELATION: " + r);\r
183                         if (r.getRelationship() == Relationship.PARENT_OF) {\r
184                             if ((r.getObject() instanceof IElement)) {\r
185                                 IElement ee = (IElement) r.getObject();\r
186                                 if (d.containsElement(ee)) {\r
187                                     //System.out.println("DIAGRAM CONTAINS OBJECT: " + r.getObject());\r
188 \r
189                                     // Mark the object also to be processed for removal.\r
190                                     elementsToProcess.addFirst(ee);\r
191                                     restart = true;\r
192                                 }\r
193                             }\r
194                         }\r
195                     }\r
196                     if (restart) {\r
197                         // Only process this element after we're sure that\r
198                         // all its children have been processed.\r
199                         elementsToProcess.addLast(el);\r
200                         continue;\r
201                     }\r
202                 }\r
203             }\r
204 \r
205             if (!processedElements.add(el))\r
206                 continue;\r
207 \r
208             TerminalTopology tt = el.getElementClass().getAtMostOneItemOfClass(TerminalTopology.class);\r
209             BendsHandler bh = el.getElementClass().getAtMostOneItemOfClass(BendsHandler.class);\r
210 \r
211             if (bh != null) {\r
212                 // Verify that the edge is NOT between two branch points.\r
213                 // If it is, do not allow deletion because it is the only case\r
214                 // which can break a connection tree into a connection forest.\r
215                 // We do not want that to happen.\r
216                 Connection begin = topology.getConnection(el, EdgeEnd.Begin);\r
217                 Connection end = topology.getConnection(el, EdgeEnd.End);\r
218 \r
219                 // Try to work with cases where the model is somewhat corrupt.\r
220                 if (begin != null && end != null) {\r
221                     if (PickFilter.FILTER_BRANCH_POINT.accept(begin.node) && PickFilter.FILTER_BRANCH_POINT.accept(end.node)) {\r
222                         error("Deletion of branch point connecting edges is not allowed. Must be connected to a node terminal.");\r
223                         return false;\r
224                     }\r
225                 }\r
226 \r
227                 if (DEBUG_DELETE)\r
228                     System.out.println("ADDED EDGE FOR REMOVAL: " + el);\r
229                 edges.add(el);\r
230             } else {\r
231                 if (DEBUG_DELETE)\r
232                     System.out.println("ADDED ELEMENT FOR REMOVAL: " + el);\r
233                 elements.add(el);\r
234 \r
235                 if (tt != null) {\r
236                     terminals.clear();\r
237                     tt.getTerminals(el, terminals);\r
238                     connections.clear();\r
239                     for (Terminal terminal : terminals)\r
240                         topology.getConnections(el, terminal, connections);\r
241                     for (Connection c : connections) {\r
242                         if (c.edge != null) {\r
243                             if (c.edge.getElementClass().containsClass(BendsHandler.class))\r
244                                 edges.add(c.edge);\r
245                             if (DEBUG_DELETE)\r
246                                 System.out.println("TERMINAL CONNECTION WILL BE DISCONNECTED: " + c);\r
247                         }\r
248                     }\r
249                 }\r
250             }\r
251         }\r
252 \r
253         if (elements.isEmpty() && edges.isEmpty())\r
254             return false;\r
255 \r
256         if (DEBUG_DELETE) {\r
257             System.out.println("gathered elements to delete:");\r
258             System.out.println("\telements:");\r
259             if (!elements.isEmpty())\r
260                 for (IElement e : elements)\r
261                     System.out.println("\t\t" + e);\r
262             System.out.println("\tedges:");\r
263             if (!edges.isEmpty())\r
264                 for (IElement e : edges)\r
265                     System.out.println("\t\t" + e);\r
266         }\r
267 \r
268         final IDiagram diagram = this.diagram;\r
269         final ISynchronizationContext syncContext = ElementFactoryUtil.getContextChecked(diagram); \r
270 \r
271         new DatabaseJob("Delete selection") {\r
272             @Override\r
273             protected IStatus run(IProgressMonitor monitor) {\r
274                 try {\r
275                     delete(monitor);\r
276                     return Status.OK_STATUS;\r
277                 } catch (CannotRemoveException e) {\r
278                         ShowMessage.showInformation("Delete Selection Was Denied", e.getLocalizedMessage());\r
279                     return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);\r
280                 } catch (DatabaseException e) {\r
281                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unexpected error in delete.", e);\r
282                 } finally {\r
283                     error(null);\r
284                     monitor.done();\r
285                 }\r
286             }\r
287 \r
288             private void delete(IProgressMonitor monitor) throws DatabaseException {\r
289                 Simantics.getSession().syncRequest(new WriteRequest() {\r
290                     Set<Resource> connectionsToRemove = new HashSet<Resource>();\r
291                     Set<Resource> touchedConnections = new HashSet<Resource>();\r
292 \r
293                     @Override\r
294                     public void perform(WriteGraph graph) throws DatabaseException {\r
295                         validateRemoval(graph);\r
296                         graph.markUndoPoint();\r
297 \r
298                         ConnectionUtil cu = new ConnectionUtil(graph);\r
299 \r
300                         // Remove edges\r
301                         for (IElement edge : edges) {\r
302                             ConnectionEntity ce = edge.getHint(ElementHints.KEY_CONNECTION_ENTITY);\r
303                             touchConnection( ce.getConnection() );\r
304 \r
305                             if (DEBUG_DELETE)\r
306                                 System.out.println("REMOVING EDGE: " + edge);\r
307                             Object obj = ElementUtils.getObject(edge);\r
308                             if (obj instanceof EdgeResource) {\r
309                                 cu.remove((EdgeResource) obj);\r
310                             }\r
311                         }\r
312 \r
313                         // Remove elements\r
314                         for (IElement element : elements) {\r
315                             ConnectionHandler ch = element.getElementClass().getAtMostOneItemOfClass(ConnectionHandler.class);\r
316                             if (ch != null) {\r
317                                 if (DEBUG_DELETE)\r
318                                     System.out.println("MARKING CONNECTION TO BE REMOVED: " + element);\r
319                                 connectionsToRemove.add( (Resource) ElementUtils.getObject(element) );\r
320                             } else {\r
321                                 ConnectionEntity ce = element.getHint(ElementHints.KEY_CONNECTION_ENTITY);\r
322                                 if(ce != null) {\r
323                                     if (DEBUG_DELETE)\r
324                                         System.out.println("REMOVING BRANCH POINT: " + element);\r
325                                     new RemoveBranchpoint(element).perform(graph);\r
326                                     touchConnection( ce.getConnection() );\r
327                                 } else {\r
328                                     if (DEBUG_DELETE)\r
329                                         System.out.println("REMOVING ELEMENT: " + element);\r
330 \r
331                                     Object obj = ElementUtils.getObject(element);\r
332                                     if (obj instanceof Resource) {\r
333                                         // Get terminal connections for element\r
334                                         Collection<Resource> connectors = cu.getTerminalConnectors((Resource) obj, null);\r
335                                         for (Resource connector : connectors) {\r
336                                             Resource connection = ConnectionUtil.tryGetConnection(graph, connector);\r
337                                             if (connection != null)\r
338                                                 touchConnection( connection );\r
339                                             cu.disconnectFromAllRouteNodes(connector);\r
340                                         }\r
341 \r
342                                         new RemoveElement((Resource)d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE), (Resource)element.getHint(ElementHints.KEY_OBJECT)).perform(graph);\r
343                                     }\r
344                                 }\r
345                             }\r
346                         }\r
347 \r
348                         // Check all touched connections to see if they are empty.\r
349                         for (Resource connection : touchedConnections) {\r
350                             int removedConnectors = cu.removeUnusedConnectors(connection);\r
351                             if (DEBUG_DELETE)\r
352                                 System.out.println("PRUNED " + removedConnectors + " CONNECTORS FROM TOUCHED CONNECTION " + connection);\r
353                             while (true) {\r
354                                 int removedInteriorRouteNodes = cu.removeExtraInteriorRouteNodes(connection);\r
355                                 if (DEBUG_DELETE)\r
356                                     System.out.println("PRUNED " + removedInteriorRouteNodes + " INTERIOR ROUTE NODES FROM TOUCHED CONNECTION " + connection);\r
357                                 if (removedInteriorRouteNodes == 0)\r
358                                     break;\r
359                             }\r
360                             int connectors = cu.getConnectedConnectors(connection, null).size();\r
361                             if (DEBUG_DELETE)\r
362                                 System.out.println("\t" + connectors + " CONNECTORS LEFT");\r
363                             if (connectors < 2) {\r
364                                 connectionsToRemove.add(connection);\r
365                             }\r
366                         }\r
367 \r
368                         // Remove selected/left-over empty connections\r
369                         for (Resource connection : connectionsToRemove) {\r
370                             if (DEBUG_DELETE)\r
371                                 System.out.println("REMOVING CONNECTION: " + connection);\r
372                             RemoveElement.removeElement(graph, (Resource)d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE), connection);\r
373                         }\r
374                     }\r
375 \r
376                     private void validateRemoval(ReadGraph graph) throws DatabaseException, CannotRemoveException {\r
377                         ArrayList<String> problems = new ArrayList<String>(elements.size());\r
378                         for (IElement element : elements) {\r
379                             Object obj = ElementUtils.getObject(element);\r
380                             if (obj instanceof Resource) {\r
381                                 Remover remover = RemoverUtil.getPossibleRemover(graph, (Resource) obj);\r
382                                 if (remover != null) {\r
383                                     String problem = remover.canRemove(graph, new HashMap<Object, Object>(4));\r
384                                     if (problem != null) {\r
385                                         problems.add(problem);\r
386                                     }\r
387                                 }\r
388                             }\r
389                         }\r
390                         if (!problems.isEmpty()) {\r
391                             throw new CannotRemoveException(EString.implode(problems));\r
392                         }\r
393                     }\r
394 \r
395                     void touchConnection(Object c) {\r
396                         if (DEBUG_DELETE)\r
397                             System.out.println("TOUCHED CONNECTION: " + c);\r
398                         if (c instanceof IElement) {\r
399                             Object obj = ElementUtils.getObject((IElement) c);\r
400                             if (obj instanceof Resource)\r
401                                 touchedConnections.add((Resource) obj);\r
402                         } else if (c instanceof Resource) {\r
403                             touchedConnections.add((Resource) c);\r
404                         }\r
405                     }\r
406                 });\r
407             }\r
408         }.schedule();\r
409 \r
410         return true;\r
411     }\r
412 \r
413     void message(final String message) {\r
414         if (statusLine == null)\r
415             return;\r
416         swtExec(new Runnable() {\r
417             @Override\r
418             public void run() {\r
419                 statusLine.setMessage(message);\r
420                 statusLine.setErrorMessage(null);\r
421             }\r
422         });\r
423     }\r
424 \r
425     void error(final String message) {\r
426         if (statusLine == null)\r
427             return;\r
428         swtExec(new Runnable() {\r
429             @Override\r
430             public void run() {\r
431                 statusLine.setErrorMessage(message);\r
432             }\r
433         });\r
434     }\r
435 \r
436     void swtExec(Runnable r) {\r
437         ThreadUtils.asyncExec(SWTThread.getThreadAccess(Display.getDefault()), r);\r
438     }\r
439 \r
440 }\r