Search in sources :

Example 1 with NodeRelativePath

use of org.freeplane.features.map.NodeRelativePath in project freeplane by freeplane.

the class MMapController method getPathToNearestTargetClone.

private NodeRelativePath getPathToNearestTargetClone(final NodeModel source, final NodeModel target) {
    if (source == target)
        return new NodeRelativePath(source, target);
    final Clones targetClones = target.subtreeClones();
    final int pathNumber = targetClones.size();
    if (pathNumber == 1)
        return new NodeRelativePath(source, target);
    Collection<NodeRelativePath> paths = new ArrayList<>(pathNumber);
    for (NodeModel targetClone : targetClones) paths.add(new NodeRelativePath(source, targetClone));
    final NodeRelativePath shortestPath = Collections.min(paths, new Comparator<NodeRelativePath>() {

        @Override
        public int compare(NodeRelativePath o1, NodeRelativePath o2) {
            return o1.getPathLength() - o2.getPathLength();
        }
    });
    return shortestPath;
}
Also used : NodeModel(org.freeplane.features.map.NodeModel) NodeRelativePath(org.freeplane.features.map.NodeRelativePath) ArrayList(java.util.ArrayList) Clones(org.freeplane.features.map.Clones) Point(java.awt.Point)

Example 2 with NodeRelativePath

use of org.freeplane.features.map.NodeRelativePath in project freeplane by freeplane.

the class MMapController method moveNodeAndItsClones.

public void moveNodeAndItsClones(NodeModel child, final NodeModel newParent, int newIndex, final boolean isLeft, final boolean changeSide) {
    if (child.subtreeContainsCloneOf(newParent)) {
        UITools.errorMessage("not allowed");
        return;
    }
    final NodeModel oldParent = child.getParentNode();
    if (newParent != oldParent && newParent.subtreeClones().contains(oldParent)) {
        moveNodeAndItsClones(child, oldParent, newIndex, newParent.isLeft(), false);
        return;
    }
    final NodeModel childNode = child;
    final int oldIndex = oldParent.getIndex(childNode);
    final int childCount = newParent.getChildCount();
    newIndex = newIndex >= childCount ? oldParent == newParent ? childCount - 1 : childCount : newIndex;
    if (oldParent != newParent || oldIndex != newIndex || changeSide != false) {
        final NodeRelativePath nodeRelativePath = getPathToNearestTargetClone(oldParent, newParent);
        final Set<NodeModel> oldParentClones = new HashSet<NodeModel>(oldParent.subtreeClones().toCollection());
        final Set<NodeModel> newParentClones = new HashSet<NodeModel>(newParent.subtreeClones().toCollection());
        final NodeModel commonAncestor = nodeRelativePath.commonAncestor();
        for (NodeModel commonAncestorClone : commonAncestor.subtreeClones()) {
            NodeModel oldParentClone = nodeRelativePath.pathBegin(commonAncestorClone);
            NodeModel newParentClone = nodeRelativePath.pathEnd(commonAncestorClone);
            final boolean isLeftForClone = newParentClone == newParent ? isLeft : newParentClone.isLeft();
            moveSingleNode(oldParentClone.getChildAt(oldIndex), newParentClone, newIndex, isLeftForClone, changeSide);
            oldParentClones.remove(oldParentClone);
            newParentClones.remove(newParentClone);
        }
        for (NodeModel newParentClone : newParentClones) insertSingleNewNode(child.cloneTree(), newParentClone, newIndex, newParentClone.isLeft());
        for (NodeModel oldParentClone : oldParentClones) deleteSingleNode(oldParentClone, oldIndex);
    }
}
Also used : NodeModel(org.freeplane.features.map.NodeModel) NodeRelativePath(org.freeplane.features.map.NodeRelativePath) Point(java.awt.Point) HashSet(java.util.HashSet)

Example 3 with NodeRelativePath

use of org.freeplane.features.map.NodeRelativePath in project freeplane by freeplane.

the class MMapController method moveNode.

public void moveNode(final NodeModel child, final NodeModel newParent, final int newIndex, final boolean isLeft, final boolean changeSide) {
    if (child.subtreeContainsCloneOf(newParent)) {
        UITools.errorMessage("not allowed");
        return;
    }
    final NodeModel oldParent = child.getParentNode();
    final int oldIndex = oldParent.getChildPosition(child);
    if (oldParent != newParent || oldIndex != newIndex || changeSide != false) {
        final Set<NodeModel> oldParentClones = new HashSet<NodeModel>(oldParent.clones().toCollection());
        final Set<NodeModel> newParentClones = new HashSet<NodeModel>(newParent.clones().toCollection());
        final NodeRelativePath nodeRelativePath = new NodeRelativePath(oldParent, newParent);
        final NodeModel commonAncestor = nodeRelativePath.commonAncestor();
        for (NodeModel commonAncestorClone : commonAncestor.clones()) {
            NodeModel oldParentClone = nodeRelativePath.pathBegin(commonAncestorClone);
            NodeModel newParentClone = nodeRelativePath.pathEnd(commonAncestorClone);
            moveSingleNode(oldParentClone.getChildAt(oldIndex), newParentClone, newIndex, isLeft, changeSide);
            oldParentClones.remove(oldParentClone);
            newParentClones.remove(newParentClone);
        }
        for (NodeModel newParentClone : newParentClones) insertSingleNewNode(child.cloneTree(), newParentClone, newIndex, newParentClone.isLeft());
        for (NodeModel oldParentClone : oldParentClones) deleteSingleNode(oldParentClone, oldIndex);
    }
}
Also used : NodeModel(org.freeplane.features.map.NodeModel) NodeRelativePath(org.freeplane.features.map.NodeRelativePath) Point(java.awt.Point) HashSet(java.util.HashSet)

Example 4 with NodeRelativePath

use of org.freeplane.features.map.NodeRelativePath in project freeplane by freeplane.

the class ConnectorModel method cloneForSource.

public NodeLinkModel cloneForSource(NodeModel sourceClone) {
    final NodeModel source = getSource();
    if (sourceClone == source)
        return this;
    final NodeModel target = getTarget();
    if (target != null && target.getParentNode() != null && source.getParentNode() != null) {
        final NodeRelativePath nodeRelativePath = new NodeRelativePath(source, target);
        final NodeModel commonAncestor = nodeRelativePath.commonAncestor();
        final NodeModel ancestorClone = nodeRelativePath.ancestorForBegin(sourceClone);
        if (commonAncestor.isSubtreeCloneOf(ancestorClone)) {
            final NodeRelativePath pathAncestorToSource = new NodeRelativePath(commonAncestor, source);
            final NodeRelativePath clonePath = new NodeRelativePath(ancestorClone, sourceClone);
            if (pathAncestorToSource.equalPathsTo(clonePath)) {
                final NodeModel targetClone = nodeRelativePath.pathEnd(ancestorClone);
                String targetID = targetClone.createID();
                return cloneForSource(sourceClone, targetID);
            }
        }
    }
    return null;
}
Also used : NodeModel(org.freeplane.features.map.NodeModel) NodeRelativePath(org.freeplane.features.map.NodeRelativePath)

Example 5 with NodeRelativePath

use of org.freeplane.features.map.NodeRelativePath in project freeplane by freeplane.

the class NewSummaryAction method addNewSummaryNodeStartEditing.

private boolean addNewSummaryNodeStartEditing() {
    final ModeController modeController = Controller.getCurrentModeController();
    final IMapSelection selection = modeController.getController().getSelection();
    final List<NodeModel> sortedSelection = selection.getSortedSelection(false);
    final NodeModel firstNode = sortedSelection.get(0);
    final NodeModel lastNode = sortedSelection.get(sortedSelection.size() - 1);
    final boolean isLeft = firstNode.isLeft();
    // different sides
    if (isLeft != lastNode.isLeft()) {
        return false;
    }
    final NodeModel parentNode = firstNode.getParentNode();
    if (parentNode == null)
        return false;
    final NodeModel lastParent = lastNode.getParentNode();
    if (lastParent == null)
        return false;
    if (parentNode.equals(lastParent))
        return addNewSummaryNodeStartEditing(firstNode, lastNode);
    else {
        final NodeRelativePath nodeRelativePath = new NodeRelativePath(firstNode, lastNode);
        NodeModel commonAncestor = nodeRelativePath.commonAncestor();
        if (commonAncestor == firstNode || commonAncestor == lastNode)
            return false;
        final NodeModel newFirstNode = nodeRelativePath.beginPathElement(1);
        final NodeModel newLastNode = nodeRelativePath.endPathElement(1);
        return addNewSummaryNodeStartEditing(newFirstNode, newLastNode);
    }
}
Also used : NodeModel(org.freeplane.features.map.NodeModel) IMapSelection(org.freeplane.features.map.IMapSelection) NodeRelativePath(org.freeplane.features.map.NodeRelativePath) ModeController(org.freeplane.features.mode.ModeController)

Aggregations

NodeModel (org.freeplane.features.map.NodeModel)6 NodeRelativePath (org.freeplane.features.map.NodeRelativePath)6 Point (java.awt.Point)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 Clones (org.freeplane.features.map.Clones)1 IMapSelection (org.freeplane.features.map.IMapSelection)1 ModeController (org.freeplane.features.mode.ModeController)1