import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.service.UndoRedoSupport;
import org.simantics.g3d.ontology.G3D;
import org.simantics.g3d.scenegraph.RenderListener;
import org.simantics.g3d.scenegraph.base.INode;
import org.simantics.g3d.scenegraph.base.NodeListener;
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 vtk.vtkProp;
-public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener{
+public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{
private static final boolean DEBUG = false;
protected ParentNode<E> rootNode;
+ protected UndoRedoSupport undoRedoSupport;
+ protected int undoOpCount = 0;
+ protected boolean runUndo = false;
public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, VtkView view, ParentNode<E> rootNode) {
this.session = session;
this.mapping = mapping;
view.addListener(this);
mapping.addMappingListener(this);
rootNode.addListener(this);
+
+ undoRedoSupport = session.getService(UndoRedoSupport.class);
+ undoRedoSupport.subscribe(this);
+ try {
+ undoOpCount = undoRedoSupport.getUndoContext(session).getAll().size();
+ } catch(DatabaseException e) {
+ e.printStackTrace();
+ }
}
+
protected abstract void addActor(E node);
protected abstract void removeActor(E node);
protected abstract void updateActor(E node,Set<String> ids);
private boolean rangeModified = false;
+ @Override
+ public void onChanged() {
+ try {
+ int count = undoRedoSupport.getUndoContext(session).getAll().size();
+ if (count < undoOpCount) {
+ runUndo = true;
+ } else {
+ runUndo = false;
+ }
+ undoOpCount = count;
+ if (DEBUG) System.out.println("Undo " + runUndo);
+ } catch (DatabaseException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+ }
+
@SuppressWarnings("unchecked")
@Override
public void updateRenderObjectsFor(INode node) {
@SuppressWarnings("unchecked")
private void receiveAdd(E node, String id, boolean db) {
- if (DEBUG) System.out.println("receiveAdd " + node + " " + id + " " + db);
+ if (DEBUG) System.out.println("receiveAdd " + debugString(node) + " " + id + " " + db);
synchronized (syncMutex) {
for (Pair<E, String> n : added) {
if (n.first.equals(node))
@SuppressWarnings("unchecked")
private void receiveRemove(E node, String id, boolean db) {
- if (DEBUG) System.out.println("receiveRemove " + node + " " + id + " " + db);
+ if (DEBUG) System.out.println("receiveRemove " + debugString(node) + " " + id + " " + db);
synchronized (syncMutex) {
for (Pair<E, String> n : removed) {
if (n.first.equals(node))
@SuppressWarnings("unchecked")
private void receiveUpdate(E node, String id, boolean db) {
- if (DEBUG) System.out.println("receiveUpdate " + node + " " + id + " " + db);
+ if (DEBUG) System.out.println("receiveUpdate " + debugString(node) + " " + id + " " + db);
synchronized (syncMutex) {
// for (Pair<E, String> n : updated) {
// if (n.first.equals(node))
@Override
public void perform(WriteGraph graph) throws DatabaseException {
- commit(graph);
+ if (DEBUG) System.out.println("Commit " + commitMessage);
if (commitMessage != null) {
Layer0Utils.addCommentMetadata(graph, commitMessage);
+ graph.markUndoPoint();
commitMessage = null;
}
- graph.markUndoPoint();
+ commit(graph);
}
}, new Callback<DatabaseException>() {
graphUpdates = true;
mapping.updateDomain(graph);
graphUpdates = false;
+ clearDeletes();
+ if (DEBUG) System.out.println("Commit done");
}
}
+
+
@Override
public void domainModified() {
if (graphUpdates)
}
+ 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;
+ }
+ }
+
+ private boolean useFullSyncWithUndo = false;
+
protected void update(ReadGraph graph) throws DatabaseException {
- synchronized (syncMutex) {
- graphUpdates = true;
- for (Object domainObject : mapping.getDomainModified()) {
- E rangeObject = mapping.get(domainObject);
- if (rangeObject != null)
- graphModified.add(rangeObject);
- }
- mapping.updateRange(graph);
- graphModified.clear();
- graphUpdates = false;
- }
+ if (DEBUG) System.out.println("Graph update start");
+ if (runUndo && useFullSyncWithUndo) {
+ reset(graph);
+ } else {
+ synchronized (syncMutex) {
+ graphUpdates = true;
+ for (Object domainObject : mapping.getDomainModified()) {
+ E rangeObject = mapping.get(domainObject);
+ if (rangeObject != null)
+ graphModified.add(rangeObject);
+ }
+ mapping.updateRange(graph);
+ graphModified.clear();
+ syncDeletes();
+ clearDeletes();
+ graphUpdates = false;
+ }
+ }
- if (mapping.isRangeModified())
- commit("Graph sync");
+ //if (mapping.isRangeModified() && !runUndo) // FIXME : redo?
+ if (mapping.isRangeModified())
+ commit((String)null);
+ if (DEBUG) System.out.println("Graph update done");
}
@Override
}
}
- List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>();
- List<Pair<E, String>> add = new ArrayList<Pair<E,String>>();
- MapSet<E, String> mod = new MapSet.Hash<E, String>();
- Set<E> propagation = new HashSet<E>();
- Stack<E> stack = new Stack<E>();
-
+ // Reusable containers for data synchronisation
+ List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>(); // Removed objects
+ List<Pair<E, String>> add = new ArrayList<Pair<E,String>>(); // Added objects
+ MapSet<E, String> mod = new MapSet.Hash<E, String>(); // Modified objects
+ Set<E> propagation = new HashSet<E>(); // Objects with propagated changes
+ Stack<E> stack = new Stack<E>(); // Stack for handling propagation
+ Set<E> delete = Collections.synchronizedSet(new HashSet<E>()); // Objects to be completely deleted
+ Set<E> deleteUC = new HashSet<E>();
@Override
public synchronized void preRender() {
updateCycle();
}
+
+ /**
+ * 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 code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added).
+ *
+ */
+ protected void syncDeletes() {
+ deleteUC.clear();
+ for (Pair<E, String> n : removed) {
+ deleteUC.add(n.first);
+ }
+ for (Pair<E, String> n : added) {
+ deleteUC.remove(n.first);
+ }
+ if (DEBUG && deleteUC.size() > 0) {
+ System.out.println("Delete sync");
+ for (E n : delete) {
+ System.out.println(debugString(n));
+ }
+ }
+ delete.addAll(deleteUC);
+ deleteUC.clear();
+ }
+
+ /**
+ * Clears deletable objects from mapping cache.
+ */
+ protected void clearDeletes() {
+ if (DEBUG && delete.size() > 0) System.out.println("Delete");
+ for (E n : delete) {
+ if (DEBUG) System.out.println(debugString(n));
+ mapping.getRange().remove(n);
+ }
+ delete.clear();
+ }
+
+ protected String debugString(E n) {
+ return n + "@" + Integer.toHexString(n.hashCode());
+ }
+
@SuppressWarnings("unchecked")
protected void updateCycle() {
rem.clear();
mod.clear();
propagation.clear();
+
synchronized (syncMutex) {
rem.addAll(removed);
add.addAll(added);
mod.add(e, s);
}
}
-
+ syncDeletes();
removed.clear();
added.clear();
updated.clear();
for (Pair<E, String> n : rem) {
stopListening(n.first);
removeActor(n.first);
-
}
for (Pair<E, String> n : add) {
for (String s : mod.getValues(e))
l.propertyChanged(e, s);
}
+
synchronized (syncMutex) {
- if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
+ if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
rangeModified = false;
}
}
@Override
public void delete() {
+ if (undoRedoSupport != null)
+ undoRedoSupport.cancel(this);
+
changeTracking = false;
view.removeListener(this);
mapping.removeMappingListener(this);
public void setNext(PipelineComponent comp) {
if (next == comp)
return;
+ if (comp == null)
+ this.next._removeRef(this);
this.next = comp;
+ this.syncnext = false;
syncNext();
firePropertyChanged(Plant3D.URIs.HasNext);
if (comp != null)
public void setPrevious(PipelineComponent comp) {
if (previous == comp)
return;
+ if (comp == null)
+ this.previous._removeRef(this);
this.previous = comp;
-
+ this.syncprev = false;
+ syncPrevious();
firePropertyChanged(Plant3D.URIs.HasPrevious);
if (comp != null)
comp.sync();
public void setBranch0(PipelineComponent comp) {
if (branch0 == comp)
return;
+ if (comp == null)
+ this.branch0._removeRef(this);
this.branch0 = comp;
+ this.syncbr0 = false;
syncBranch0();
firePropertyChanged(Plant3D.URIs.HasBranch0);
if (comp != null)
// System.out.println(this + " next " + comp);
}
+ @GetPropertyValue(name="Previous",tabId="Debug",value=Plant3D.URIs.HasPrevious)
+ public String getPreviousDebug() {
+ if (previous == null)
+ return null;
+ return previous.getName();
+ }
+
+ @GetPropertyValue(name="Next",tabId="Debug",value=Plant3D.URIs.HasNext)
+ public String getNextDebug() {
+ if (next == null)
+ return null;
+ return next.getName();
+ }
+
+ @GetPropertyValue(name="Branch0",tabId="Debug",value=Plant3D.URIs.HasBranch0)
+ public String getBR0Debug() {
+ if (branch0 == null)
+ return null;
+ return branch0.getName();
+ }
+
private PipeControlPoint getBranchPoint() {
PipeControlPoint branchPoint;
if (getControlPoint().getSubPoint().size() > 0) {
return true;
}
+ // When link to a component is removed, also link to the other direction must be removed at the same time, or
+ // Control point structure is left into illegal state.
+ private void _removeRef(PipelineComponent comp) {
+ if (next == comp) {
+ next = null;
+ syncnext = false;
+ syncNext();
+ } else if (previous == comp) {
+ previous = null;
+ syncprev = false;
+ syncPrevious();
+ } else if (branch0 == comp) {
+ branch0 = null;
+ syncbr0 = false;
+ syncBranch0();
+ }
+ }
+
+ boolean syncnext = false;
+ private void syncNext() {
+ if (syncnext)
+ return;
+ syncnext = _syncNext();
+ }
- private boolean syncNext() {
-
- if (getControlPoint() != null) {
+ private boolean _syncNext() {
+ PipeControlPoint pcp = getControlPoint();
+ if (pcp != null) {
+
if (next != null ) {
- if (next.getControlPoint() != null && next.getPipeRun() != null) {
+ if (next.getControlPoint() != null) {
// TODO, relying that the other direction is connected.
boolean nxt = next.getPrevious() == this;
boolean br0 = next.getBranch0() == this;
if (nxt){
- return _connectNext(getControlPoint(), next.getControlPoint());
+ return _connectNext(pcp, next.getControlPoint());
} else if (br0) {
- return _connectNext(getControlPoint(), next.getBranchPoint());
+ return _connectNext(pcp, next.getBranchPoint());
+ } else {
+ return false;
}
} else {
return false;
}
- } else if (getControlPoint().getPrevious() != null) {
- getControlPoint().setNext(null);
+ } else if (pcp.getNext() != null) {
+ pcp.setNext(null);
+ return true;
}
} else {
return false;
return true;
}
- private boolean syncPrevious() {
-
- if (getControlPoint() != null) {
+ boolean syncprev = false;
+ private void syncPrevious() {
+ if (syncprev)
+ return;
+ syncprev = _syncPrevious();
+ }
+
+ private boolean _syncPrevious() {
+ PipeControlPoint pcp = getControlPoint();
+ if (pcp != null) {
if (previous != null ) {
- if (previous.getControlPoint() != null && previous.getPipeRun() != null) {
+ if (previous.getControlPoint() != null) {
// TODO, relying that the other direction is connected.
boolean prev = previous.getNext() == this;
boolean br0 = previous.getBranch0() == this;
if (prev){
- return _connectPrev(getControlPoint(), previous.getControlPoint());
+ return _connectPrev(pcp, previous.getControlPoint());
} else if (br0) {
- return _connectPrev(getControlPoint(), previous.getBranchPoint());
+ return _connectPrev(pcp, previous.getBranchPoint());
+ } else {
+ return false;
}
} else {
return false;
}
- } else if (getControlPoint().getPrevious() != null) {
- getControlPoint().setPrevious(null);
+ } else if (pcp.getPrevious() != null) {
+ pcp.setPrevious(null);
+ return true;
}
} else {
return false;
return true;
}
- private boolean syncBranch0() {
+ boolean syncbr0 = false;
+ private void syncBranch0() {
+ if (syncbr0)
+ return;
+ syncbr0 = _syncBranch0();
+ }
+
+ private boolean _syncBranch0() {
if (getControlPoint() != null) {
if (getControlPoint().isDualInline()) {
branch0 = null;
return false;
}
if (branch0 != null) {
- if (branch0.getControlPoint() != null && branch0.getPipeRun() != null) {
+ if (branch0.getControlPoint() != null) {
PipeControlPoint branchPoint = getBranchPoint();
PipeControlPoint pcp = branch0.getControlPoint();
// TODO, relying that the other direction is connected.
_connectNext(branchPoint, pcp);
} else if (prev){
_connectPrev(branchPoint, pcp);
+ } else {
+ return false;
}
+
} else {
return false;
}
} else if (getControlPoint().getSubPoint().size() > 0) { // TODO : this may cause problems? (Removes branch point, before branch has been set?)
getControlPoint().getSubPoint().get(0).remove();
getControlPoint().children.clear();
+ return true;
}
} else {
return false;
public class PipeControlPoint extends G3DNode implements IP3DNode {
+
+ private static boolean DEBUG = false;
public enum Type{INLINE,TURN,END};
public enum Direction{NEXT,PREVIOUS};
public void setNext(PipeControlPoint next) {
if (isEnd() && previous != null && next != null)
throw new RuntimeException("End control points are allowed to have only one connection");
-// if (next != null && getPipeRun() == null)
-// throw new RuntimeException("Cannot connect control point befor piperun has been set");
+ if (this.next == next)
+ return;
+ if (DEBUG) System.out.println(this + " next " + next);
this.next = next;
if (component != null) {
if (parent == null || sub)
public void setPrevious(PipeControlPoint previous) {
if (isEnd() && next != null && previous != null)
throw new RuntimeException("End control points are allowed to have only one connection");
-// if (previous != null && getPipeRun() == null)
-// throw new RuntimeException("Cannot connect control point befor piperun has been set");
+ if (this.previous == previous)
+ return;
+ if (DEBUG) System.out.println(this + " previous " + previous);
this.previous = previous;
if (component != null) {
if (parent == null || sub)
public PipeControlPoint getParentPoint() {
return parent;
}
-
-
-
-
-
-
private double length;
private Double turnAngle;
private Vector3d turnAxis;
}
public void setTurnAxis(Vector3d turnAxis) {
- this.turnAxis = turnAxis;
+ if (this.turnAxis != null && MathTools.equals(turnAxis, this.turnAxis))
+ return;
+ this.turnAxis = turnAxis;
firePropertyChanged("turnAxis");
}
dir.sub(pcp.getWorldPosition(),previous.getWorldPosition());
if (dir.lengthSquared() > MathTools.NEAR_ZERO)
dir.normalize();
+ else
+ return null;
Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
Quat4d q2 = MathTools.getQuat(aa);
dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
if (dir.lengthSquared() > MathTools.NEAR_ZERO)
dir.normalize();
+ else
+ return null;
Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
Quat4d q2 = MathTools.getQuat(aa);
if (component == null)
return;
PipelineComponent next = component.getNext();
- PipelineComponent prev = component.getNext();
+ PipelineComponent prev = component.getPrevious();
+ PipelineComponent br0 = component.getBranch0();
+ component.setNext(null);
+ component.setPrevious(null);
+ component.setBranch0(null);
if (next != null) {
if (next.getNext() == component)
next.setNext(null);
else if (next.getPrevious() == component)
next.setPrevious(null);
+ else if (next.getBranch0() == component)
+ next.setBranch0(null);
}
if (prev != null) {
if (prev.getNext() == component)
prev.setNext(null);
else if (prev.getPrevious() == component)
prev.setPrevious(null);
+ else if (prev.getBranch0() == component)
+ prev.setBranch0(null);
+ }
+ if (br0 != null) {
+ if (br0.getNext() == component)
+ prev.setNext(null);
+ else if (br0.getPrevious() == component)
+ prev.setPrevious(null);
+ else if (br0.getBranch0() == component)
+ br0.setBranch0(null);
}
PipelineComponent comp = component;
component = null;