/* * 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(); }