X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fcommon%2FAbstractVTKNodeMap.java;h=a42c367252fc0744a754cc22fbd9f9045f283275;hb=4266ad3085e9f475ede502ff5c54b3eebb909866;hp=0866dde55e730b6acaa82fd71d9de56abe983e4a;hpb=4a656971025eea4b563933179d6120d0e87e7549;p=simantics%2F3d.git diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java index 0866dde5..a42c3672 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.g3d.vtk.common; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; @@ -23,6 +24,8 @@ import java.util.Map; import java.util.Set; import java.util.Stack; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.progress.IProgressService; import org.simantics.db.ReadGraph; import org.simantics.db.Session; import org.simantics.db.UndoContext; @@ -40,17 +43,20 @@ import org.simantics.g3d.scenegraph.base.ParentNode; import org.simantics.objmap.exceptions.MappingException; import org.simantics.objmap.graph.IMapping; import org.simantics.objmap.graph.IMappingListener; -import org.simantics.utils.datastructures.Callback; import org.simantics.utils.datastructures.MapList; import org.simantics.utils.datastructures.MapSet; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.ui.ExceptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import vtk.vtkProp; public abstract class AbstractVTKNodeMap implements VTKNodeMap, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{ - private static final boolean DEBUG = false; + private static final boolean DEBUG = true; + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVTKNodeMap.class); protected Session session; protected IMapping mapping; @@ -109,7 +115,7 @@ public abstract class AbstractVTKNodeMap implements VT @SuppressWarnings("unchecked") @Override public Collection getRenderObjects(INode node) { - return nodeToActor.getValues((E)node); + return nodeToActor.getValues((E) node); } protected void map(E node, Collection props) { @@ -121,13 +127,20 @@ public abstract class AbstractVTKNodeMap implements VT protected void removeMap(E node) { Collection coll = nodeToActor.getValuesUnsafe(node); - for (vtkProp p : coll) { - actorToNode.remove(p); + if (coll.size() > 0) { + view.lock(); + for (vtkProp p : coll) { + actorToNode.remove(p); + if (p.GetVTKId() != 0) { + view.getRenderer().RemoveActor(p); + p.Delete(); + } + } + view.unlock(); } nodeToActor.remove(node); } - @SuppressWarnings("unchecked") @Override public ParentNode getRootNode() { return (ParentNode)rootNode; @@ -188,12 +201,11 @@ public abstract class AbstractVTKNodeMap implements VT } - @SuppressWarnings("unchecked") @Override public void updateRenderObjectsFor(E node) { List toDelete = new ArrayList(); view.lock(); - for (vtkProp prop : nodeToActor.getValues((E)node)) { + for (vtkProp prop : nodeToActor.getValues(node)) { if (prop.GetVTKId() != 0) { view.getRenderer().RemoveActor(prop); //prop.Delete(); @@ -202,12 +214,12 @@ public abstract class AbstractVTKNodeMap implements VT actorToNode.remove(prop); } view.unlock(); - nodeToActor.remove((E)node); - Collection coll = getActors((E)node); + nodeToActor.remove(node); + Collection coll = getActors(node); if (coll != null) { for (vtkProp prop : coll) { - nodeToActor.add((E)node,prop); - actorToNode.put(prop, (E)node); + nodeToActor.add(node,prop); + actorToNode.put(prop, node); toDelete.remove(prop); } } @@ -253,7 +265,6 @@ public abstract class AbstractVTKNodeMap implements VT repaint(); } - @SuppressWarnings("unchecked") private void receiveUpdate(E node, String id, boolean db) { if (DEBUG) System.out.println("receiveUpdate " + debugString(node) + " " + id + " " + db); synchronized (syncMutex) { @@ -283,27 +294,29 @@ public abstract class AbstractVTKNodeMap implements VT } protected void doCommit() { - session.asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - if (DEBUG) System.out.println("Commit " + commitMessage); - if (commitMessage != null) { - Layer0Utils.addCommentMetadata(graph, commitMessage); - graph.markUndoPoint(); - commitMessage = null; + IProgressService service = PlatformUI.getWorkbench().getProgressService(); + try { + service.busyCursorWhile(monitor -> { + try { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if (DEBUG) System.out.println("Commit " + commitMessage); + if (commitMessage != null) { + Layer0Utils.addCommentMetadata(graph, commitMessage); + graph.markUndoPoint(); + commitMessage = null; + } + commit(graph); + } + }); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError("Cannot commit editor changes", e); } - commit(graph); - } - - }, new Callback() { - - @Override - public void run(DatabaseException parameter) { - if (parameter != null) - ExceptionUtils.logAndShowError("Cannot commit editor changes", parameter); - } - }); + }); + } catch (InvocationTargetException | InterruptedException e) { + LOGGER.error("Unexpected exception", e); + } } protected void commit(WriteGraph graph) throws DatabaseException { @@ -326,7 +339,6 @@ public abstract class AbstractVTKNodeMap implements VT if (DEBUG)System.out.println("domainModified"); session.asyncRequest(new ReadRequest() { - @SuppressWarnings("unchecked") @Override public void run(ReadGraph graph) throws DatabaseException { update(graph); @@ -338,25 +350,24 @@ public abstract class AbstractVTKNodeMap implements VT protected void reset(ReadGraph graph) throws MappingException { if (DEBUG) System.out.println("Reset"); - synchronized (syncMutex) { - graphUpdates = true; - mapping.getRangeModified().clear(); - for (DBObject o : mapping.getDomain()) - mapping.domainModified(o); - mapping.updateRange(graph); - graphModified.clear(); - graphUpdates = false; - } + graphUpdates = true; + mapping.getRangeModified().clear(); + for (DBObject o : mapping.getDomain()) + mapping.domainModified(o); + mapping.updateRange(graph); + graphModified.clear(); + graphUpdates = false; } private boolean useFullSyncWithUndo = false; protected void update(ReadGraph graph) throws DatabaseException { - if (DEBUG) System.out.println("Graph update start"); - if (runUndo && useFullSyncWithUndo) { - reset(graph); - } else { - synchronized (syncMutex) { + synchronized (syncMutex) { + if (DEBUG) System.out.println("Graph update start"); + + if (runUndo && useFullSyncWithUndo) { + reset(graph); + } else { graphUpdates = true; for (DBObject domainObject : mapping.getDomainModified()) { @SuppressWarnings("unchecked") @@ -369,12 +380,13 @@ public abstract class AbstractVTKNodeMap implements VT syncDeletes(); clearDeletes(); graphUpdates = false; - } + } + + if (mapping.isRangeModified() && !runUndo && !runRedo) + commit((String)null); + + if (DEBUG) System.out.println("Graph update done"); } - - if (mapping.isRangeModified() && !runUndo && !runRedo) - commit((String)null); - if (DEBUG) System.out.println("Graph update done"); } @Override @@ -416,6 +428,7 @@ public abstract class AbstractVTKNodeMap implements VT * This code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added). * */ + @SuppressWarnings("unused") protected void syncDeletes() { deleteUC.clear(); for (Pair n : removed) { @@ -437,6 +450,7 @@ public abstract class AbstractVTKNodeMap implements VT /** * Clears deletable objects from mapping cache. */ + @SuppressWarnings("unused") protected void clearDeletes() { if (DEBUG && delete.size() > 0) System.out.println("Delete"); for (E n : delete) { @@ -479,10 +493,10 @@ public abstract class AbstractVTKNodeMap implements VT E n = stack.pop(); boolean conflict = filterChange(removed, n); if (conflict) { - System.out.println("Prevent removing " + n); + if (DEBUG) System.out.println("Prevent removing " + n); //filterChange(added, n) if (filterChange(added, n)) - System.out.println("Prevent adding " + n); + if (DEBUG) System.out.println("Prevent adding " + n); } if (n instanceof ParentNode) { ParentNode pn = (ParentNode)n; @@ -491,7 +505,10 @@ public abstract class AbstractVTKNodeMap implements VT } } } - + // Do not process updates for removed nodes. + for (Pair r : removed) { + updated.removeValues(r.first); + } rem.addAll(removed); add.addAll(added); for (E e : updated.getKeys()) { @@ -506,16 +523,16 @@ public abstract class AbstractVTKNodeMap implements VT } - for (Pair n : rem) { stopListening(n.first); removeActor(n.first); + n.first.remove(); } - for (Pair n : add) { - addActor(n.first); - listen(n.first); - } + for (Pair n : add) { + addActor(n.first); + listen(n.first); + } for (E e : mod.getKeys()) { Set ids = mod.getValues(e); @@ -549,26 +566,11 @@ public abstract class AbstractVTKNodeMap implements VT } } -// synchronized (syncMutex) { -// rem.addAll(removed); -// add.addAll(added); -// //mod.addAll(updated); -// for (E e : updated.getKeys()) { -// for (String s : updated.getValues(e)) -// mod.add(e, s); -// } -// -// removed.clear(); -// added.clear(); -// updated.clear(); -// } - for (E e : mod.getKeys()) { Set ids = mod.getValues(e); updateActor(e,ids); } - - + for (Pair n : rem) { for (NodeListener l : nodeListeners) l.nodeRemoved(null, n.first, n.second); @@ -640,7 +642,7 @@ public abstract class AbstractVTKNodeMap implements VT //FIXME : 1. sometimes removed structural models cause ObjMap to add their children again. // removing the listener here prevents corruption of visual model, but better fix is needed. - // 2. detach causes nodeRemoved event, which then causes other critical events to be missed. Took out th + // 2. detach causes nodeRemoved event, which then causes other critical events to be missed. Took out call: //stopListening(child); }