Skip to content

Commit

Permalink
edvin#801 Add support to remove row from the GridPane
Browse files Browse the repository at this point in the history
As well as remove all rows.
  • Loading branch information
zshamrock committed Sep 15, 2018
1 parent 5caf327 commit 0203f6d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/tornadofx/Layouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ 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 {
if (properties.containsKey(GridPaneRowIdKey)) {
properties[GridPaneRowIdKey] = properties[GridPaneRowIdKey] as Int - 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)
} else if (childRowIndex > rowIndex) {
GridPane.setRowIndex(child, childRowIndex - 1)
}
}
children.removeAll(nodesToDelete)
return rowIndex
}

fun GridPane.removeAllRows() {
children.clear()
properties.remove(GridPaneRowIdKey)
}

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

fun GridPane.constraintsForRow(rowIndex: Int) = constraintsFor(rowConstraints, rowIndex)
Expand Down

0 comments on commit 0203f6d

Please sign in to comment.