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=0df8367fd9964982a0734818817892d67a01a370;hb=f36bb7c63520639baddd1e8a37c054c27ad1e4a5;hp=b762200786217a0434515a068a8d0f89cbe222e6;hpb=43b9a071783377f64924bb0c2f1930fb49316f6f;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 b7622007..0df8367f 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,14 +24,17 @@ 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; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.db.procedure.SyncProcedure; import org.simantics.db.service.UndoRedoSupport; import org.simantics.g3d.ontology.G3D; import org.simantics.g3d.scenegraph.RenderListener; @@ -40,24 +44,27 @@ 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{ +public abstract class AbstractVTKNodeMap implements VTKNodeMap, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{ private static final boolean DEBUG = true; + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVTKNodeMap.class); protected Session session; - protected IMapping mapping; + protected IMapping mapping; protected VtkView view; - protected MapList nodeToActor = new MapList(); - protected Map actorToNode = new HashMap(); + private MapList nodeToActor = new MapList(); + private Map actorToNode = new HashMap(); protected ParentNode rootNode; @@ -66,7 +73,7 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< protected int redoOpCount = 0; protected boolean runUndo = false; protected boolean runRedo = false; - public AbstractVTKNodeMap(Session session, IMapping mapping, VtkView view, ParentNode rootNode) { + public AbstractVTKNodeMap(Session session, IMapping mapping, VtkView view, ParentNode rootNode) { this.session = session; this.mapping = mapping; this.view = view; @@ -85,9 +92,7 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< e.printStackTrace(); } } - - - + protected abstract void addActor(E node); protected abstract void removeActor(E node); protected abstract void updateActor(E node,Set ids); @@ -111,10 +116,32 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< @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) { + for (vtkProp p : props) { + nodeToActor.add(node, p); + actorToNode.put(p, node); + } + } + + protected void removeMap(E node) { + Collection coll = nodeToActor.getValuesUnsafe(node); + 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; @@ -143,6 +170,10 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< private boolean rangeModified = false; + public boolean isRangeModified() { + return rangeModified; + } + @Override public void onChanged() { try { @@ -171,12 +202,11 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } - @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(); @@ -185,12 +215,12 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< 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); } } @@ -210,11 +240,12 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } if (changeTracking) { mapping.rangeModified((E)node.getParent()); + mapping.rangeModified((E)node); } added.add(new Pair(node, id)); rangeModified = true; } - view.refresh(); + repaint(); } @SuppressWarnings("unchecked") @@ -225,15 +256,16 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< if (n.first.equals(node)) return; } - if (changeTracking && !db) + if (changeTracking && !db) { + mapping.rangeModified((E)node); mapping.rangeModified((E)node.getParent()); + } removed.add(new Pair(node, id)); rangeModified = true; } repaint(); } - @SuppressWarnings("unchecked") private void receiveUpdate(E node, String id, boolean db) { if (DEBUG) System.out.println("receiveUpdate " + debugString(node) + " " + id + " " + db); synchronized (syncMutex) { @@ -263,29 +295,34 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } 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); - } - }); + }); + postCommit(); + } catch (InvocationTargetException | InterruptedException e) { + LOGGER.error("Unexpected exception", e); + } } - + + protected void postCommit() {} + protected void commit(WriteGraph graph) throws DatabaseException { synchronized(syncMutex) { if (DEBUG) System.out.println("Commit"); @@ -304,54 +341,72 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< if (graphUpdates) return; if (DEBUG)System.out.println("domainModified"); - session.asyncRequest(new ReadRequest() { - - @SuppressWarnings("unchecked") + session.asyncRequest(new UniqueRead() { + @Override + public Object perform(ReadGraph graph) throws DatabaseException { + return new Object(); + } + }, new SyncProcedure() { @Override - public void run(ReadGraph graph) throws DatabaseException { + public void execute(ReadGraph graph, Object result) throws DatabaseException { + // Perform all updates to the model in a single query thread update(graph); + }; + + @Override + public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException { + LOGGER.error("Failed to update pipeline changes" + throwable); } }); - } protected void reset(ReadGraph graph) throws MappingException { if (DEBUG) System.out.println("Reset"); - synchronized (syncMutex) { - graphUpdates = true; - mapping.getRangeModified().clear(); - for (Object 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); + synchronized (syncMutex) { + reset(graph); + } } else { synchronized (syncMutex) { graphUpdates = true; - for (Object domainObject : mapping.getDomainModified()) { - E rangeObject = mapping.get(domainObject); + for (DBObject domainObject : mapping.getDomainModified()) { + @SuppressWarnings("unchecked") + E rangeObject = (E) mapping.get(domainObject); if (rangeObject != null) graphModified.add(rangeObject); } - mapping.updateRange(graph); + + } + + mapping.updateRange(graph); + + synchronized (syncMutex) { graphModified.clear(); syncDeletes(); - clearDeletes(); - graphUpdates = false; } + + clearDeletes(); + graphUpdates = false; } if (mapping.isRangeModified() && !runUndo && !runRedo) commit((String)null); + if (DEBUG) System.out.println("Graph update done"); } @@ -389,7 +444,7 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< /** * When objects are removed (either from Java or Graph), after remove processing the Java objects remain in mapping cache. - * This causes problems with Undo and Redo, whcih the end up re-using the removed objects from mapping cache. + * This causes problems with Undo and Redo, which cause re-using the removed objects from mapping cache. * * This code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added). * @@ -420,6 +475,7 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< for (E n : delete) { if (DEBUG) System.out.println(debugString(n)); mapping.getRange().remove(n); + stopListening(n); } delete.clear(); } @@ -456,7 +512,10 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< E n = stack.pop(); boolean conflict = filterChange(removed, n); if (conflict) { - filterChange(added, n); + if (DEBUG) System.out.println("Prevent removing " + n); + //filterChange(added, n) + if (filterChange(added, n)) + if (DEBUG) System.out.println("Prevent adding " + n); } if (n instanceof ParentNode) { ParentNode pn = (ParentNode)n; @@ -465,7 +524,10 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } } } - + // 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()) { @@ -480,16 +542,16 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } - 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); @@ -523,26 +585,11 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } } -// 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); @@ -602,7 +649,6 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< if (DEBUG) System.out.println("Node added " + child + " parent " + node); //receiveAdd((E)child, rel ,graphUpdates); receiveAdd((E)child, rel ,graphModified.contains(node)); - } @SuppressWarnings("unchecked") @@ -613,9 +659,10 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< //receiveRemove((E)child, rel, graphUpdates); receiveRemove((E)child, rel, graphModified.contains(node)); - //FIXME : 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. - stopListening(child); + //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 call: + //stopListening(child); } @Override @@ -657,7 +704,7 @@ public abstract class AbstractVTKNodeMap implements VTKNodeMap< } - public IMapping getMapping() { + public IMapping getMapping() { return mapping; }