Skip to content

Commit 3e5ebd8

Browse files
committed
refactored code for up/down output columns moving. Fixed selection lost
1 parent a75ece0 commit 3e5ebd8

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

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

+36-35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Iterator;
2222
import java.util.Map;
2323

24+
import java.util.function.*;
25+
2426
import javax.swing.Box;
2527
import javax.swing.BoxLayout;
2628
import javax.swing.DefaultCellEditor;
@@ -144,41 +146,6 @@ public void actionPerformed(final ActionEvent e) {
144146
}
145147
});
146148

147-
JButton upButton = new JButton("Up");
148-
upButton.addActionListener(new ActionListener() {
149-
public void actionPerformed(final ActionEvent e) {
150-
int[] selectedRows = m_table.getSelectedRows();
151-
logger.debug("selectedRows = " + selectedRows);
152-
153-
if (selectedRows.length == 0) {
154-
return;
155-
}
156-
((ScriptNodeOutputColumnsTableModel) m_table.getModel())
157-
.moveRowsUp(selectedRows);
158-
}
159-
});
160-
161-
JButton downButton = new JButton("Down");
162-
downButton.addActionListener(new ActionListener() {
163-
public void actionPerformed(final ActionEvent e) {
164-
int[] selectedRows = m_table.getSelectedRows();
165-
logger.debug("selectedRows = " + selectedRows);
166-
167-
if (selectedRows.length == 0) {
168-
return;
169-
}
170-
171-
((ScriptNodeOutputColumnsTableModel) m_table.getModel())
172-
.moveRowsDown(selectedRows);
173-
}
174-
});
175-
176-
outputButtonPanel.add(addButton);
177-
outputButtonPanel.add(removeButton);
178-
outputButtonPanel.add(Box.createHorizontalStrut(40));
179-
outputButtonPanel.add(upButton);
180-
outputButtonPanel.add(downButton);
181-
182149
m_table = new JTable();
183150
m_table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
184151

@@ -190,6 +157,17 @@ public void actionPerformed(final ActionEvent e) {
190157
m_counter++;
191158
m_table.setModel(model);
192159

160+
JButton upButton = createButtonForRowsMoving("Up",
161+
selectedRows -> model.moveRowsUp(selectedRows));
162+
JButton downButton = createButtonForRowsMoving("Down",
163+
selectedRows -> model.moveRowsDown(selectedRows));
164+
165+
outputButtonPanel.add(addButton);
166+
outputButtonPanel.add(removeButton);
167+
outputButtonPanel.add(Box.createHorizontalStrut(40));
168+
outputButtonPanel.add(upButton);
169+
outputButtonPanel.add(downButton);
170+
193171
outputMainPanel.add(m_table.getTableHeader(), BorderLayout.PAGE_START);
194172
outputMainPanel.add(m_table, BorderLayout.CENTER);
195173
outputPanel.add(newtableCBPanel);
@@ -562,4 +540,27 @@ protected final void clearErrorHighlight() {
562540
m_scriptPanel.revalidate();
563541
m_scriptPanel.repaint();
564542
}
543+
544+
protected JButton createButtonForRowsMoving(String title,
545+
Function<int[], int[]> mover) {
546+
JButton button = new JButton(title);
547+
button.addActionListener(new ActionListener() {
548+
public void actionPerformed(final ActionEvent e) {
549+
int[] selectedRows = m_table.getSelectedRows();
550+
logger.debug("selectedRows = " + selectedRows);
551+
552+
if (selectedRows.length == 0) {
553+
return;
554+
}
555+
556+
int[] selection = mover.apply(selectedRows);
557+
558+
m_table.addRowSelectionInterval(
559+
selection[0],
560+
selection[selection.length - 1]);
561+
}
562+
});
563+
564+
return button;
565+
}
565566
}

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

+26-15
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,30 @@ public final void clearRows() {
8888
data = new ArrayList<ArrayList<Object>>();
8989
}
9090

91-
public final void moveRowsUp(int[] rows) {
92-
for (int j = 0; j < rows.length; j++) {
93-
if (rows[j] != 0)
94-
Collections.swap(data, rows[j], rows[j] - 1);
95-
}
96-
fireTableDataChanged();
97-
}
98-
99-
public final void moveRowsDown(int[] rows) {
100-
for (int j = rows.length - 1; j >= 0; j--) {
101-
if (rows[j] != data.size() - 1)
102-
Collections.swap(data, rows[j], rows[j] + 1);
103-
}
104-
fireTableDataChanged();
105-
}
91+
public final int[] moveRowsUp(int[] rows) {
92+
int[] newSelection = rows.clone();
93+
94+
if (rows[0] != 0) {
95+
for (int j = 0; j < rows.length; j++) {
96+
Collections.swap(data, rows[j], rows[j] - 1);
97+
newSelection[j] -= 1;
98+
}
99+
fireTableDataChanged();
100+
}
101+
102+
return newSelection;
103+
}
104+
105+
public final int[] moveRowsDown(int[] rows) {
106+
int[] newSelection = rows.clone();
107+
108+
if (rows[rows.length - 1] != data.size() - 1) {
109+
for (int j = rows.length - 1; j >= 0; j--) {
110+
Collections.swap(data, rows[j], rows[j] + 1);
111+
newSelection[j] += 1;
112+
}
113+
fireTableDataChanged();
114+
}
115+
return newSelection;
116+
}
106117
}

0 commit comments

Comments
 (0)