/* * This method takes two elements and exchanges their parents and previous * elements. It then makes sure spacing is correct by calling * focusPositionAndLines * * This method assumes index1 < index2 */ private synchronized void swapElements(Object newParam, int index1, int index2) { // the two elements to be swapped LinearElement firstElement = root.getVector().get(index1); LinearElement secondElement = root.getVector().get(index2); // the two elements after each of the elements to be swapped LinearElement secondElementNext; LinearElement firstElementNext; /* * There is a corner case where secondElement.previousChild is * firstElement */ // the first element's previous child before it is reassigned LinearElement tempPrevChild = firstElement.getPreviousChild(); if (index2 != index1 + 1) { // the elements are not adjacent // set previous elements for the two nodes firstElement.setPreviousChild(secondElement.getPreviousChild()); secondElement.setPreviousChild(tempPrevChild); // set previous elements for the nodes referencing the swapped nodes if (index1 + 1 < root.getVector().size()) { firstElementNext = root.getVector().get(index1 + 1); firstElementNext.setPreviousChild(secondElement); } if (index2 + 1 < root.getVector().size()) { secondElementNext = root.getVector().get(index2 + 1); secondElementNext.setPreviousChild(firstElement); } } else { // the elements are adjacent firstElement.setPreviousChild(secondElement); secondElement.setPreviousChild(tempPrevChild); // set previous elements for the nodes referencing the second node if (index2 + 1 < root.getVector().size()) { secondElementNext = root.getVector().get(index2 + 1); secondElementNext.setPreviousChild(firstElement); } } // swap the elements in root root.getVector().swap(index1, index2); root.focusPosition(); }
public synchronized void elementRemoved(Object source, int pos, int newSize) { LinearElement toBeRemoved = root.getVector().get(pos); shapes.getShapes().remove(toBeRemoved.getShape()); visualizer.removeLine(toBeRemoved.getVerticalLine()); visualizer.removeLine(toBeRemoved.getHorizontalLine()); LinearElement parent = root; LinearElement previousChild = toBeRemoved.getPreviousChild(); if (pos + 1 <= newSize) // If it is not the last element root.getVector().get(pos + 1).setPreviousChild(previousChild); parent.getVector().remove(toBeRemoved); root.focusPosition(); }
public synchronized void elementInserted( Object source, ElementType element, int pos, int newSize) { LinearElement parent = root; LinearElement previousChild = pos - 1 >= 0 ? root.getVector().get(pos - 1) : null; LinearElement newElement = visualizer.initElement(element, parent, previousChild); if (pos + 1 < newSize) // if there is a child after us // use pos not pos + 1 because the element has not been // added to the vector yet, so pos + 1 in the user's // vector is pos in the roots vector. root.getVector().get(pos).setPreviousChild(newElement); parent.getVector().insertElementAt(newElement, pos); root.focusPosition(); }