Skip to content

Commit

Permalink
Merge pull request #802 from zshamrock/master
Browse files Browse the repository at this point in the history
#801 Add support to remove row from the GridPane
  • Loading branch information
Edvin Syse committed Sep 18, 2018
2 parents 4e785e1 + 5118b48 commit b656f9a
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/tornadofx/Layouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,47 @@ fun GridPane.row(title: String? = null, op: Pane.() -> Unit = {}) {
addRow(properties[GridPaneRowIdKey] as Int, *fake.children.toTypedArray())
}

/**
* Removes the corresponding row to which this [node] belongs to.
*
* It does the opposite of the [GridPane.row] cleaning all internal state properly.
*
* @return the row index of the removed row.
*/
fun GridPane.removeRow(node: Node): Int {
val rowIdKey = properties[GridPaneRowIdKey] as Int?
if (rowIdKey != null) {
when (rowIdKey) {
0 -> properties.remove(GridPaneRowIdKey)
else -> properties[GridPaneRowIdKey] = rowIdKey - 1
}
}
val rowIndex = GridPane.getRowIndex(node) ?: 0
val nodesToDelete = mutableListOf<Node>()
children.forEach { child ->
val childRowIndex = GridPane.getRowIndex(child) ?: 0
if (childRowIndex == rowIndex) {
nodesToDelete.add(child)
// Remove row index property from the node
GridPane.setRowIndex(child, null)
GridPane.setColumnIndex(child, null)
} else if (childRowIndex > rowIndex) {
GridPane.setRowIndex(child, childRowIndex - 1)
}
}
children.removeAll(nodesToDelete)
return rowIndex
}

fun GridPane.removeAllRows() {
children.forEach {
GridPane.setRowIndex(it, null)
GridPane.setColumnIndex(it, null)
}
children.clear()
properties.remove(GridPaneRowIdKey)
}

fun GridPane.constraintsForColumn(columnIndex: Int) = constraintsFor(columnConstraints, columnIndex)

fun GridPane.constraintsForRow(rowIndex: Int) = constraintsFor(rowConstraints, rowIndex)
Expand Down
169 changes: 169 additions & 0 deletions src/test/kotlin/tornadofx/tests/LayoutsTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package tornadofx.tests

import javafx.scene.Node
import javafx.scene.Scene
import javafx.scene.control.Label
import javafx.scene.control.ScrollPane
import javafx.scene.layout.GridPane
import javafx.scene.layout.Pane
import javafx.scene.layout.VBox
import javafx.scene.text.Text
import javafx.stage.Stage
import org.junit.Test
import org.testfx.api.FxToolkit
import tornadofx.*
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull

/**
*
* @author Anindya Chatterjee
*/
class RegionTest {
val GridPaneRowIdKey = "TornadoFX.GridPaneRowId"
val primaryStage: Stage = FxToolkit.registerPrimaryStage()

lateinit var vbox: VBox
Expand Down Expand Up @@ -55,4 +62,166 @@ class RegionTest {
assertEquals(root.height, vbox.height)
}
}

@Test
fun testEmptyGridPane() {
FxToolkit.setupFixture {
val root = GridPane().apply {
}

assertFalse(root.properties.containsKey(GridPaneRowIdKey))
assertEquals(root.children, emptyList<Node>().observable())
}
}

@Test
fun testGridPaneAddRows() {
lateinit var label: Label
lateinit var text1: Text
lateinit var text2: Text
lateinit var text3: Text
lateinit var vbox: VBox
FxToolkit.setupFixture {
val root = GridPane().apply {
row {
label = label()
}
row {
text1 = text()
text2 = text()
text3 = text()
}
row {
vbox = vbox()
}

}

assertEquals(root.properties[GridPaneRowIdKey], 2)
assertEquals(GridPane.getRowIndex(label), 0)
assertEquals(GridPane.getColumnIndex(label), 0)
assertEquals(GridPane.getRowIndex(text1), 1)
assertEquals(GridPane.getColumnIndex(text1), 0)
assertEquals(GridPane.getRowIndex(text2), 1)
assertEquals(GridPane.getColumnIndex(text2), 1)
assertEquals(GridPane.getRowIndex(text3), 1)
assertEquals(GridPane.getColumnIndex(text3), 2)
assertEquals(GridPane.getRowIndex(vbox), 2)
assertEquals(GridPane.getColumnIndex(vbox), 0)
assertEquals(root.children, listOf(label, text1, text2, text3, vbox).observable())
}
}

@Test
fun testGridPaneRemoveNoRow() {
val label = Label()
FxToolkit.setupFixture {
val root = GridPane().apply {
}

root.removeRow(label)

assertFalse(root.properties.containsKey(GridPaneRowIdKey))
assertEquals(root.children, emptyList<Node>().observable())
}
}

@Test
fun testGridPaneRemoveSingleRow() {
lateinit var label: Label
FxToolkit.setupFixture {
val root = GridPane().apply {
row {
label = label()
}
}

root.removeRow(label)

assertFalse(root.properties.containsKey(GridPaneRowIdKey))
assertNull(GridPane.getRowIndex(label))
assertNull(GridPane.getColumnIndex(label))
assertEquals(root.children, emptyList<Node>().observable())
}
}

@Test
fun testGridPaneRemoveMiddleRow() {
lateinit var label: Label
lateinit var text1: Text
lateinit var text2: Text
lateinit var text3: Text
lateinit var vbox: VBox
FxToolkit.setupFixture {
val root = GridPane().apply {
row {
label = label()
}
row {
text1 = text()
text2 = text()
text3 = text()
}
row {
vbox = vbox()
}

}

root.removeRow(text1)

assertEquals(root.properties[GridPaneRowIdKey], 1)
assertEquals(GridPane.getRowIndex(label), 0)
assertEquals(GridPane.getColumnIndex(label), 0)
assertNull(GridPane.getRowIndex(text1))
assertNull(GridPane.getColumnIndex(text1))
assertNull(GridPane.getRowIndex(text2))
assertNull(GridPane.getColumnIndex(text2))
assertNull(GridPane.getRowIndex(text3))
assertNull(GridPane.getColumnIndex(text3))
assertEquals(GridPane.getRowIndex(vbox), 1)
assertEquals(GridPane.getColumnIndex(vbox), 0)
assertEquals(root.children, listOf<Node>(label, vbox).observable())
}
}

@Test
fun testGridPaneRemoveAllRows() {
lateinit var label: Label
lateinit var text1: Text
lateinit var text2: Text
lateinit var text3: Text
lateinit var vbox: VBox
FxToolkit.setupFixture {
val root = GridPane().apply {
row {
label = label()
}
row {
text1 = text()
text2 = text()
text3 = text()
}
row {
vbox = vbox()
}

}

root.removeAllRows()

assertFalse(root.properties.containsKey(GridPaneRowIdKey))
assertNull(GridPane.getRowIndex(label))
assertNull(GridPane.getColumnIndex(label))
assertNull(GridPane.getRowIndex(text1))
assertNull(GridPane.getColumnIndex(text1))
assertNull(GridPane.getRowIndex(text2))
assertNull(GridPane.getColumnIndex(text2))
assertNull(GridPane.getRowIndex(text3))
assertNull(GridPane.getColumnIndex(text3))
assertNull(GridPane.getRowIndex(vbox))
assertNull(GridPane.getColumnIndex(vbox))
assertEquals(root.children, emptyList<Node>().observable())
}
}
}

0 comments on commit b656f9a

Please sign in to comment.