Skip to content

Commit a8dc72d

Browse files
committed
scala: fixed movement of multiple selected rows of column's names in a configuration dialog
1 parent d42b777 commit a8dc72d

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

RubyScript/src/org/knime/ext/jruby/RubyScriptNodeDialog.scala

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,39 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
121121
}
122122
}
123123
})
124-
val upButton = new JButton("Up")
125-
upButton.addActionListener(new ActionListener() {
126124

127-
def actionPerformed(e: ActionEvent) {
128-
val selectedRows = table.getSelectedRows
129-
logger.debug("selectedRows = " + selectedRows)
130-
if (selectedRows.length > 0) {
131-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
132-
.moveRowsUp(selectedRows)
133-
}
134-
}
135-
})
136-
val downButton = new JButton("Down")
137-
downButton.addActionListener(new ActionListener() {
125+
table = new JTable()
126+
table.putClientProperty("terminateEditOnFocusLost", true)
127+
table.setAutoscrolls(true)
128+
val model = new ScriptNodeOutputColumnsTableModel()
129+
model.addColumn("Column name")
130+
model.addColumn("Column type")
131+
model.addRow("script output " + columnCounter, "String")
132+
columnCounter += 1
133+
table.setModel(model)
138134

139-
def actionPerformed(e: ActionEvent) {
140-
val selectedRows = table.getSelectedRows
141-
logger.debug("selectedRows = " + selectedRows)
142-
if (selectedRows.length > 0) {
143-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
144-
.moveRowsDown(selectedRows)
135+
def createButtonForRowsMoving(title: String, func: (Array[Int]) => (Int, Int)): JButton = {
136+
val result = new JButton(title)
137+
result.addActionListener(new ActionListener() {
138+
139+
def actionPerformed(e: ActionEvent) {
140+
val selectedRows = table.getSelectedRows
141+
logger.debug("selectedRows = " + selectedRows)
142+
if (selectedRows.length > 0) {
143+
val position = func(selectedRows)
144+
table.setRowSelectionInterval(position._1, position._2)
145+
}
145146
}
146-
}
147-
})
147+
})
148+
result
149+
}
150+
151+
val upButton = createButtonForRowsMoving(
152+
"Up",
153+
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel].moveRowsUp)
154+
val downButton = createButtonForRowsMoving(
155+
"Down",
156+
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel].moveRowsDown)
148157

149158
Array(addButton, removeButton, Box.createHorizontalStrut(40),
150159
upButton, downButton).foreach(outputButtonPanel.add)
@@ -154,15 +163,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
154163
// outputButtonPanel.add(Box.createHorizontalStrut(40))
155164
// outputButtonPanel.add(upButton)
156165
// outputButtonPanel.add(downButton)
157-
table = new JTable()
158-
table.putClientProperty("terminateEditOnFocusLost", true)
159-
table.setAutoscrolls(true)
160-
val model = new ScriptNodeOutputColumnsTableModel()
161-
model.addColumn("Column name")
162-
model.addColumn("Column type")
163-
model.addRow("script output " + columnCounter, "String")
164-
columnCounter += 1
165-
table.setModel(model)
166+
166167
outputMainPanel.add(table.getTableHeader, BorderLayout.PAGE_START)
167168
outputMainPanel.add(table, BorderLayout.CENTER)
168169
outputPanel.add(newtableCBPanel)

RubyScript/src/org/knime/ext/jruby/ScriptNodeOutputColumnsTableModel.scala

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,27 @@ class ScriptNodeOutputColumnsTableModel extends AbstractTableModel {
8282
s(j) = v
8383
}
8484

85-
def moveRowsUp(rows: Array[Int]) {
86-
for (j <- 0 until rows.length if rows(j) != 0)
87-
swap(data, rows(j), rows(j) - 1)
88-
fireTableDataChanged()
85+
def moveRowsUp(rows: Array[Int]):(Int,Int) = {
86+
val limit = 0
87+
var range = Array(rows.head, rows.last)
88+
if (rows.head > limit) {
89+
rows.foreach(i => swap(data, i, i - 1))
90+
fireTableDataChanged()
91+
92+
range = range.map(i => if (i - 1 < limit) limit else i - 1)
93+
}
94+
(range(0), range(1))
8995
}
9096

91-
def moveRowsDown(rows: Array[Int]) {
92-
for (j <- rows.length - 1 to 0 if rows(j) != data.size - 1)
93-
swap(data, rows(j), rows(j) + 1)
94-
fireTableDataChanged()
97+
def moveRowsDown(rows: Array[Int]): (Int, Int) = {
98+
val limit = data.size - 1;
99+
var range = Array(rows.head, rows.last)
100+
if (rows.last < limit) {
101+
rows.view.reverse.foreach(i => swap(data, i + 1, i))
102+
103+
fireTableDataChanged()
104+
range = range.map(i => if (i + 1 > limit) limit else i + 1)
105+
}
106+
(range(0), range(1))
95107
}
96108
}

0 commit comments

Comments
 (0)