Skip to content

Commit

Permalink
Add moving templates below collapsed last template
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Nov 12, 2023
1 parent eabb3e5 commit 48749d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class TemplateJTree(
if (scheme is Template) {
val index = templates.indexOf(fromNode)

if (moveDown && index == templates.lastIndex - 1) Pair(descendants.last(), Position.BELOW)
if (moveDown && index == templates.lastIndex - 1) Pair(templates.last(), Position.BELOW)
else Pair(templates.getOrNull(index + if (!moveDown) -1 else 2), Position.ABOVE)
} else {
val index = descendants.indexOf(fromNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class TemplateJTreeModel(
fromNode.state is Template ->
when (position) {
Position.INTO -> false
Position.BELOW -> toNode == descendants.last() && fromNode != templates.last()
Position.BELOW -> toNode == templates.last() && toNode != fromNode
Position.ABOVE ->
toNode.state is Template && toNode != templates.run { getOrNull(indexOf(fromNode) + 1) }
}
Expand Down Expand Up @@ -243,22 +243,22 @@ class TemplateJTreeModel(
}

/**
* Invokes [canMoveRow] after converting [fromIndex] and [toIndex] using [viewIndexToModelIndex].
* Returns `true` if and only if the node at [fromIndex] can be moved [Position.INTO] the node at [toIndex].
*
* @see canMoveRow
* @see RefinedDropSupport
*/
override fun canDrop(fromIndex: Int, toIndex: Int, position: Position) =
canMoveRow(viewIndexToModelIndex(fromIndex), viewIndexToModelIndex(toIndex), position)
override fun isDropInto(component: JComponent?, fromIndex: Int, toIndex: Int) =
canDrop(fromIndex, toIndex, Position.INTO)

/**
* Returns `true` if and only if the node at [fromIndex] can be moved [Position.INTO] the node at [toIndex].
* Invokes [canMoveRow] after converting [fromIndex] and [toIndex] using [viewIndexToModelIndex].
*
* @see canMoveRow
* @see RefinedDropSupport
*/
override fun isDropInto(component: JComponent?, fromIndex: Int, toIndex: Int) =
canDrop(fromIndex, toIndex, Position.INTO)
override fun canDrop(fromIndex: Int, toIndex: Int, position: Position) =
canMoveRow(viewIndexToModelIndex(fromIndex), viewIndexToModelIndex(toIndex), position)

/**
* Invokes [moveRow] after converting [fromIndex] and [toIndex] using [viewIndexToModelIndex].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ object TemplateJTreeModelTest : FunSpec({
"cannot move template into scheme" to row(3, 1, Position.INTO, false),
"cannot move template into template" to row(0, 5, Position.INTO, false),
"cannot move template below template" to row(5, 0, Position.BELOW, false),
"cannot move template below non-last scheme" to row(0, 4, Position.BELOW, false),
"cannot move template below scheme" to row(0, 4, Position.BELOW, false),
"cannot move template above next template" to row(0, 3, Position.ABOVE, false),
"cannot move template above scheme" to row(3, 2, Position.ABOVE, false),
"can move middle template above first template" to row(3, 0, Position.ABOVE, true),
"can move middle template above last template" to row(0, 5, Position.ABOVE, true),
"can move middle template below last scheme" to row(3, 6, Position.BELOW, true),
"can move middle template below last template" to row(3, 5, Position.BELOW, true),
"cannot move second-to-last template above last template" to row(3, 5, Position.ABOVE, false),
"cannot move last template below last scheme" to row(5, 6, Position.BELOW, false),
"cannot move scheme into scheme" to row(4, 2, Position.INTO, false),
Expand Down Expand Up @@ -328,15 +328,7 @@ object TemplateJTreeModelTest : FunSpec({
list.templates.names() shouldContainExactly listOf("Template1", "Template0", "Template2")
}

test("moves a template below the last scheme (if the last template is non-empty)") {
model.insertNode(StateNode(list.templates[2]), StateNode(DummyScheme("Scheme3"))) // row 6

model.moveRow(3, 6, Position.BELOW)

list.templates.names() shouldContainExactly listOf("Template0", "Template2", "Template1")
}

test("moves a template below the last template (if the last template is empty)") {
test("moves a template below the last template") {
model.moveRow(3, 5, Position.BELOW)

list.templates.names() shouldContainExactly listOf("Template0", "Template2", "Template1")
Expand Down Expand Up @@ -385,16 +377,6 @@ object TemplateJTreeModelTest : FunSpec({
}
}

context("canDrop") {
test("translates indices according to `viewIndexToModelIndex`") {
model.canDrop(2, 3, Position.BELOW) shouldBe false

model.viewIndexToModelIndex = { it - 1 }

model.canDrop(2, 3, Position.BELOW) shouldBe true
}
}

context("isDropInto") {
test("returns `false` if the node can be dropped into the other") {
model.isDropInto(null, 1, 3) shouldBe true
Expand All @@ -405,6 +387,16 @@ object TemplateJTreeModelTest : FunSpec({
}
}

context("canDrop") {
test("translates indices according to `viewIndexToModelIndex`") {
model.canDrop(2, 3, Position.BELOW) shouldBe false

model.viewIndexToModelIndex = { it - 1 }

model.canDrop(2, 3, Position.BELOW) shouldBe true
}
}

context("drop") {
test("translates indices according to `viewIndexToModelIndex`") {
val assertOriginal = { list.templates[0].schemes.names() shouldContainExactly listOf("Scheme0", "Scheme1") }
Expand Down

0 comments on commit 48749d8

Please sign in to comment.