Skip to content

Commit 3391e18

Browse files
committed
scala: NodeDialog: JButtons replaced by scala Buttons
1 parent c6f0c8d commit 3391e18

File tree

1 file changed

+77
-88
lines changed

1 file changed

+77
-88
lines changed

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

Lines changed: 77 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,38 @@ package org.knime.ext.jruby
22

33
import java.awt.BorderLayout
44
import java.awt.Color
5-
import java.awt.Component
6-
import java.awt.Dimension
7-
import java.awt.Point
85
import java.awt.event._
9-
import java.io.BufferedReader
10-
import java.io.File
11-
import java.io.FileReader
12-
import java.io.IOException
13-
import java.util.Iterator
14-
import java.util.Map
6+
157
import javax.swing.Box
168
import javax.swing.BoxLayout
179
import javax.swing.DefaultCellEditor
18-
import javax.swing.JButton
19-
import javax.swing.JCheckBox
20-
import javax.swing.JComboBox
2110
import javax.swing.JFileChooser
22-
import javax.swing.JLabel
11+
import javax.swing.JComboBox
2312
import javax.swing.JPanel
24-
import javax.swing.JSplitPane
2513
import javax.swing.JTable
2614
import javax.swing.table.TableColumn
2715
import javax.swing.table.TableCellEditor
2816
import javax.swing.text.BadLocationException
17+
2918
import org.knime.core.data.DataColumnSpec
3019
import org.knime.core.data.DataTableSpec
3120
import org.knime.core.node._
3221
import org.knime.core.node.workflow.FlowVariable
22+
3323
import javax.swing.JScrollPane
34-
import javax.swing.JTextArea
3524
import java.awt.Font
25+
3626
import org.fife.ui.rtextarea._
3727
import org.fife.ui.rsyntaxtextarea._
28+
3829
import RubyScriptNodeDialog._
3930
//remove if not needed
31+
4032
import scala.collection.JavaConversions._
4133
import scala.collection.convert.WrapAsScala.enumerationAsScalaIterator
34+
4235
import scala.swing._
36+
import scala.swing.event._
4337

4438
/**
4539
* <code>NodeDialog</code> for the "JRuby Script" Node.
@@ -82,10 +76,6 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
8276

8377
createScriptTab()
8478

85-
implicit private def toActionListener(f: (ActionEvent) => Unit) = new ActionListener {
86-
def actionPerformed(e: ActionEvent) { f(e) }
87-
}
88-
8979
implicit private def toTableModel(table: JTable):ScriptNodeOutputColumnsTableModel = {
9080
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
9181
}
@@ -101,29 +91,32 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
10191
val newtableCBPanel = new JPanel()
10292
doAppendInputColumns = new CheckBox("Append columns to input table spec")
10393
newtableCBPanel.add(doAppendInputColumns.peer, BorderLayout.WEST)
104-
val addButton = new JButton("Add Output Column")
105-
addButton.addActionListener((_: ActionEvent) => {
106-
var name: String = null
107-
val model:ScriptNodeOutputColumnsTableModel = table
108-
val columns = model.getDataTableColumnNames
109-
do {
110-
name = "script output " + columnCounter
111-
columnCounter += 1
112-
} while (columns.indexWhere(_ == name) >= 0);
113-
model.addRow(name, "String")
114-
})
115-
val removeButton = new JButton("Remove Output Column")
116-
removeButton.addActionListener((_: ActionEvent) => {
117-
val selectedRows = table.getSelectedRows
118-
logger.debug("selectedRows = " + selectedRows)
119-
if (selectedRows.length == 0) {
120-
return
121-
}
122-
for (i <- selectedRows.length - 1 to 0) {
123-
logger.debug(" removal " + i + ": removing row " + selectedRows(i))
124-
table.removeRow(selectedRows(i))
125-
}
126-
})
94+
val addButton = new Button("Add Output Column") {
95+
reactions += {
96+
case ButtonClicked(b) =>
97+
var name: String = null
98+
val model: ScriptNodeOutputColumnsTableModel = table
99+
val columns = model.getDataTableColumnNames
100+
do {
101+
name = "script output " + columnCounter
102+
columnCounter += 1
103+
} while (columns.indexWhere(_ == name) >= 0);
104+
model.addRow(name, "String")
105+
}
106+
}
107+
val removeButton = new Button("Remove Output Column") {
108+
reactions += {
109+
case ButtonClicked(b) =>
110+
val selectedRows = table.getSelectedRows
111+
logger.debug("selectedRows = " + selectedRows)
112+
if (selectedRows.length > 0) {
113+
for (i <- selectedRows.length - 1 to 0) {
114+
logger.debug(" removal " + i + ": removing row " + selectedRows(i))
115+
table.removeRow(selectedRows(i))
116+
}
117+
}
118+
}
119+
}
127120

128121
table = new JTable()
129122
table.putClientProperty("terminateEditOnFocusLost", true)
@@ -135,17 +128,18 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
135128
columnCounter += 1
136129
table.setModel(model)
137130

138-
def createButtonForRowsMoving(title: String, func: (Array[Int]) => (Int, Int)): JButton = {
139-
val result = new JButton(title)
140-
result.addActionListener((_: ActionEvent) => {
141-
val selectedRows = table.getSelectedRows
142-
logger.debug("selectedRows = " + selectedRows)
143-
if (selectedRows.length > 0) {
144-
val position = func(selectedRows)
145-
table.setRowSelectionInterval(position._1, position._2)
146-
}
147-
})
148-
result
131+
def createButtonForRowsMoving(title: String, func: (Array[Int]) => (Int, Int)): Button = {
132+
new Button(title) {
133+
reactions += {
134+
case ButtonClicked(b) =>
135+
val selectedRows = table.getSelectedRows
136+
logger.debug("selectedRows = " + selectedRows)
137+
if (selectedRows.length > 0) {
138+
val position = func(selectedRows)
139+
table.setRowSelectionInterval(position._1, position._2)
140+
}
141+
}
142+
}
149143
}
150144

151145
val upButton = createButtonForRowsMoving(
@@ -155,27 +149,23 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
155149
"Down",
156150
table.moveRowsDown)
157151

158-
Array(addButton, removeButton, Box.createHorizontalStrut(40),
159-
upButton, downButton).foreach(outputButtonPanel.add)
160-
161-
// outputButtonPanel.add(addButton)
162-
// outputButtonPanel.add(removeButton)
163-
// outputButtonPanel.add(Box.createHorizontalStrut(40))
164-
// outputButtonPanel.add(upButton)
165-
// outputButtonPanel.add(downButton)
152+
outputButtonPanel.add(addButton.peer)
153+
outputButtonPanel.add(removeButton.peer)
154+
outputButtonPanel.add(Box.createHorizontalStrut(40))
155+
outputButtonPanel.add(upButton.peer)
156+
outputButtonPanel.add(downButton.peer)
166157

167158
outputMainPanel.add(table.getTableHeader, BorderLayout.PAGE_START)
168159
outputMainPanel.add(table, BorderLayout.CENTER)
169160
outputPanel.add(newtableCBPanel)
170161
outputPanel.add(outputButtonPanel)
171162
outputPanel.add(outputMainPanel)
172163
val typeColumn = table.getColumnModel.getColumn(1)
173-
val typeSelector = new JComboBox[String]()
174-
typeSelector.addItem("String")
175-
typeSelector.addItem("Integer")
176-
typeSelector.addItem("Double")
177-
typeSelector.setEditable(true)
178-
typeColumn.setCellEditor(new DefaultCellEditor(typeSelector))
164+
val typeSelector: ComboBox[String] =
165+
new ComboBox[String](Seq("String", "Integer","Double"))
166+
typeSelector.makeEditable
167+
typeColumn.setCellEditor(new DefaultCellEditor(
168+
typeSelector.peer.asInstanceOf[JComboBox[String]]))
179169
addTab("Script Output", outputPanel)
180170
}
181171

@@ -198,25 +188,24 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
198188
errorMessage.editable = false
199189
scriptPanel = new BorderPanel()
200190
val scriptButtonPanel = new BorderPanel()
201-
val scriptButton = new JButton("Load Script from File")
202-
scriptButton.addActionListener((e: ActionEvent) => {
203-
val returnVal = fileChooser.showOpenDialog(
204-
e.getSource.asInstanceOf[Component])
205-
if (returnVal != JFileChooser.APPROVE_OPTION) {
206-
return
207-
}
208-
val file = fileChooser.getSelectedFile
209-
if (!file.exists()) {
210-
return
211-
}
191+
val scriptButton = new Button("Load Script from File") {
192+
reactions += {
193+
case ButtonClicked(b) =>
194+
val returnVal = fileChooser.showOpenDialog(b.peer)
195+
if (returnVal == JFileChooser.APPROVE_OPTION) {
196+
val file = fileChooser.getSelectedFile
197+
if (file.exists()) {
198+
val file_content = scala.io.Source.fromFile(file, "utf-8").mkString
199+
scriptTextArea.setText(file_content)
200+
clearErrorHighlight()
201+
}
202+
}
203+
}
204+
}
205+
scriptButtonPanel.peer.add(scriptButton.peer, BorderLayout.EAST)
206+
scriptButtonPanel.peer.add(new Label("Ruby Script").peer, BorderLayout.CENTER)
212207

213-
val file_content = scala.io.Source.fromFile(file, "utf-8").mkString
214-
scriptTextArea.setText(file_content)
215-
clearErrorHighlight()
216-
})
217-
scriptButtonPanel.peer.add(scriptButton)
218208
val scriptMainPanel = new BorderPanel()
219-
scriptMainPanel.peer.add(new Label("Script: ").peer, BorderLayout.NORTH)
220209
var splitPane = new SplitPane(Orientation.Horizontal,
221210
swing.Component.wrap(spScript), spErrorMessage)
222211
scriptMainPanel.peer.add(splitPane.peer, BorderLayout.CENTER)
@@ -263,7 +252,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
263252
table.addMouseListener(new MouseAdapter(){
264253
private var m_index: Int = _
265254

266-
override def mouseClicked(event: MouseEvent) {
255+
override def mouseClicked(event: java.awt.event.MouseEvent) {
267256
if (event.getClickCount == 2) {
268257
val table = event.getSource.asInstanceOf[JTable]
269258
val p = event.getPoint
@@ -290,7 +279,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
290279
}.init(index))
291280
val scrollPane = new JScrollPane(table)
292281
table.setFillsViewportHeight(true)
293-
panel.peer.add(new JLabel(label), BorderLayout.NORTH)
282+
panel.peer.add(new Label(label).peer, BorderLayout.NORTH)
294283
panel.peer.add(scrollPane, BorderLayout.CENTER)
295284
columnTables(index) = table
296285
panel
@@ -325,7 +314,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
325314
table.setModel(model)
326315
table.addMouseListener(new MouseAdapter() {
327316

328-
override def mouseClicked(event: MouseEvent) {
317+
override def mouseClicked(event: java.awt.event.MouseEvent) {
329318
if (event.getClickCount == 2) {
330319
val table = event.getSource.asInstanceOf[JTable]
331320
val p = event.getPoint
@@ -344,7 +333,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
344333
)
345334
val scrollPane = new JScrollPane(table)
346335
table.setFillsViewportHeight(true)
347-
flowVariablesPanel.peer.add(new JLabel(label), BorderLayout.NORTH)
336+
flowVariablesPanel.peer.add(new Label(label).peer, BorderLayout.NORTH)
348337
flowVariablesPanel.peer.add(scrollPane, BorderLayout.CENTER)
349338
flowVariablesPanel
350339
}

0 commit comments

Comments
 (0)