Provide toolbar to move filename templates

This commit is contained in:
Matthias Gugel 2020-12-13 16:54:16 +01:00
parent ad1f6de879
commit 875580f27f
2 changed files with 45 additions and 1 deletions

View File

@ -39,6 +39,16 @@ class FilenameTemplateSettingsComponent(val filenameTemplates: ArrayList<Filenam
filenameTemplateTableModel.removeRows(table.selectedRows) filenameTemplateTableModel.removeRows(table.selectedRows)
} }
}) })
actionGroup.add(object : AnAction("Up", "Move selected filename templates up", AllIcons.Actions.MoveUp) {
override fun actionPerformed(e: AnActionEvent) {
filenameTemplateTableModel.moveRowsUp(table.selectedRows)
}
})
actionGroup.add(object : AnAction("Down", "Move selected filename templates down", AllIcons.Actions.MoveDown) {
override fun actionPerformed(e: AnActionEvent) {
filenameTemplateTableModel.moveRowsDown(table.selectedRows)
}
})
val toolbar = ActionManager.getInstance().createActionToolbar("MyToolbar", actionGroup, false) val toolbar = ActionManager.getInstance().createActionToolbar("MyToolbar", actionGroup, false)
panel.add(toolbar.component) panel.add(toolbar.component)
return panel return panel

View File

@ -41,10 +41,44 @@ class FilenameTemplateTableModel(private val filenameTemplates: ArrayList<Filena
} }
fun removeRows(selectedRows: IntArray) { fun removeRows(selectedRows: IntArray) {
Arrays.sort(selectedRows) selectedRows.sort()
for (i in selectedRows.indices.reversed()) { for (i in selectedRows.indices.reversed()) {
filenameTemplates.removeAt(selectedRows[i]) filenameTemplates.removeAt(selectedRows[i])
} }
fireTableDataChanged() fireTableDataChanged()
} }
fun moveRowsUp(selectedRows: IntArray) {
selectedRows.sort()
var minRow = 0
for (currentRow in selectedRows) {
val targetRow = currentRow - 1
minRow = if (minRow <= targetRow) {
val filenameTemplate = filenameTemplates[currentRow]
filenameTemplates.removeAt(currentRow)
filenameTemplates.add(targetRow, filenameTemplate)
currentRow
} else {
currentRow + 1
}
}
fireTableDataChanged()
}
fun moveRowsDown(selectedRows: IntArray) {
selectedRows.sortDescending()
var maxRow = filenameTemplates.size - 1
for (currentRow in selectedRows) {
val targetRow = currentRow + 1
maxRow = if (targetRow <= maxRow) {
val filenameTemplate = filenameTemplates[currentRow]
filenameTemplates.removeAt(currentRow)
filenameTemplates.add(targetRow, filenameTemplate)
currentRow
} else {
currentRow - 1
}
}
fireTableDataChanged()
}
} }