ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Adding up and down methods to MutableTreeModel
This commit is contained in:
parent
60ae3f1e9d
commit
60720a7a3b
2 changed files with 141 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
|
|
@ -38,6 +39,37 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
|
|||
}
|
||||
}
|
||||
|
||||
public int[] down(Node<T> node) {
|
||||
ListIterator<Node<T>> listIterator = children.listIterator();
|
||||
while (listIterator.hasNext()) {
|
||||
Node<T> current = listIterator.next();
|
||||
if (current == node && listIterator.hasNext()) {
|
||||
int nextIndex = listIterator.nextIndex();
|
||||
listIterator.remove();
|
||||
listIterator.next();
|
||||
listIterator.add(node);
|
||||
return new int[] { nextIndex - 1, nextIndex };
|
||||
}
|
||||
}
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
public int[] up(Node<T> node) {
|
||||
ListIterator<Node<T>> listIterator = children.listIterator(children
|
||||
.size());
|
||||
while (listIterator.hasPrevious()) {
|
||||
Node<T> current = listIterator.previous();
|
||||
if (current == node && listIterator.hasPrevious()) {
|
||||
listIterator.remove();
|
||||
int previousIndex = listIterator.previousIndex();
|
||||
listIterator.previous();
|
||||
listIterator.add(current);
|
||||
return new int[] { previousIndex, previousIndex + 1 };
|
||||
}
|
||||
}
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
private void until(LinkedList<Integer> result, Node<T> parent) {
|
||||
if (parent.equals(this)) {
|
||||
return;
|
||||
|
|
@ -243,4 +275,24 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
|
|||
add(parent, insertionPosition, toAdd);
|
||||
}
|
||||
|
||||
public void down(T node) {
|
||||
T parent = getParent(node);
|
||||
Node<T> parentNode = find(parent);
|
||||
int[] changed = parentNode.down(find(node));
|
||||
if (changed.length != 0) {
|
||||
fireEvent(parent, changed[0], changed[1],
|
||||
TreeDataEvent.CONTENTS_CHANGED);
|
||||
}
|
||||
}
|
||||
|
||||
public void up(T node) {
|
||||
T parent = getParent(node);
|
||||
Node<T> parentNode = find(parent);
|
||||
int[] changed = parentNode.up(find(node));
|
||||
if (changed.length != 0) {
|
||||
fireEvent(parent, changed[0], changed[1],
|
||||
TreeDataEvent.CONTENTS_CHANGED);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -334,6 +334,92 @@ public class MutableTreeModelTest {
|
|||
assertThat(model.getChild(model.getRoot(), 0), equalTo(substitution));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNodeCanBeMovedDown() {
|
||||
final MutableTreeModel<Prueba> model = MutableTreeModel
|
||||
.create(Prueba.class);
|
||||
Prueba prueba1 = new Prueba();
|
||||
model.addToRoot(prueba1);
|
||||
Prueba prueba2 = new Prueba();
|
||||
model.addToRoot(prueba2);
|
||||
Prueba prueba3 = new Prueba();
|
||||
model.addToRoot(prueba3);
|
||||
model.down(prueba1);
|
||||
assertThat(model.getChild(model.getRoot(), 0), equalTo(prueba2));
|
||||
assertThat(model.getChild(model.getRoot(), 1), equalTo(prueba1));
|
||||
assertThat(model.getChild(model.getRoot(), 2), equalTo(prueba3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNodeCanBeMovedUp() {
|
||||
final MutableTreeModel<Prueba> model = MutableTreeModel
|
||||
.create(Prueba.class);
|
||||
Prueba prueba1 = new Prueba();
|
||||
model.addToRoot(prueba1);
|
||||
Prueba prueba2 = new Prueba();
|
||||
model.addToRoot(prueba2);
|
||||
Prueba prueba3 = new Prueba();
|
||||
model.addToRoot(prueba3);
|
||||
model.up(prueba2);
|
||||
assertThat(model.getChild(model.getRoot(), 0), equalTo(prueba2));
|
||||
assertThat(model.getChild(model.getRoot(), 1), equalTo(prueba1));
|
||||
assertThat(model.getChild(model.getRoot(), 2), equalTo(prueba3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void IfItIsAtTheTopUpDoesNothing() {
|
||||
final MutableTreeModel<Prueba> model = MutableTreeModel
|
||||
.create(Prueba.class);
|
||||
Prueba prueba1 = new Prueba();
|
||||
model.addToRoot(prueba1);
|
||||
Prueba prueba2 = new Prueba();
|
||||
model.addToRoot(prueba2);
|
||||
Prueba prueba3 = new Prueba();
|
||||
model.addToRoot(prueba3);
|
||||
model.up(prueba1);
|
||||
assertThat(model.getChild(model.getRoot(), 0), equalTo(prueba1));
|
||||
assertThat(model.getChild(model.getRoot(), 1), equalTo(prueba2));
|
||||
assertThat(model.getChild(model.getRoot(), 2), equalTo(prueba3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void movingUpAndDownSendsEvents() {
|
||||
final MutableTreeModel<Prueba> model = MutableTreeModel
|
||||
.create(Prueba.class);
|
||||
Prueba prueba1 = new Prueba();
|
||||
model.addToRoot(prueba1);
|
||||
Prueba prueba2 = new Prueba();
|
||||
model.addToRoot(prueba2);
|
||||
Prueba prueba3 = new Prueba();
|
||||
model.addToRoot(prueba3);
|
||||
final ArrayList<TreeDataEvent> eventsFired = new ArrayList<TreeDataEvent>();
|
||||
model.addTreeDataListener(new TreeDataListener() {
|
||||
|
||||
@Override
|
||||
public void onChange(TreeDataEvent event) {
|
||||
eventsFired.add(event);
|
||||
}
|
||||
});
|
||||
model.up(prueba2);
|
||||
checkIsValid(getLast(eventsFired), TreeDataEvent.CONTENTS_CHANGED,
|
||||
model.getRoot(), 0, 1);
|
||||
model.down(prueba1);
|
||||
checkIsValid(getLast(eventsFired), TreeDataEvent.CONTENTS_CHANGED,
|
||||
model.getRoot(), 1, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifItIsAtTheBottomDownDoesNothing() {
|
||||
final MutableTreeModel<Prueba> model = MutableTreeModel
|
||||
.create(Prueba.class);
|
||||
Prueba prueba1 = new Prueba();
|
||||
model.addToRoot(prueba1);
|
||||
Prueba prueba2 = new Prueba();
|
||||
model.addToRoot(prueba2);
|
||||
model.down(prueba2);
|
||||
assertThat(model.getChild(model.getRoot(), 1), equalTo(prueba2));
|
||||
}
|
||||
|
||||
private void checkIsValid(TreeDataEvent event, int type,
|
||||
Prueba expectedParent, int expectedPosition) {
|
||||
checkIsValid(event, type, expectedParent, expectedPosition,
|
||||
|
|
@ -350,6 +436,9 @@ public class MutableTreeModelTest {
|
|||
}
|
||||
|
||||
private TreeDataEvent getLast(List<TreeDataEvent> list) {
|
||||
if (list.isEmpty()) {
|
||||
throw new RuntimeException("no events");
|
||||
}
|
||||
return list.get(list.size() - 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue