Skip to content

Commit 14b41a5

Browse files
committed
scala: scala style optimization. Added implicit convertions
1 parent 4d2d8de commit 14b41a5

File tree

1 file changed

+35
-57
lines changed

1 file changed

+35
-57
lines changed

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

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,20 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
7575

7676
private var columnTables: Array[JTable] = _
7777

78+
private val fileChooser = new JFileChooser()
79+
7880
createColumnSelectionTab()
7981

8082
createScriptTab()
8183

84+
implicit private def toActionListener(f: (ActionEvent) => Unit) = new ActionListener {
85+
def actionPerformed(e: ActionEvent) { f(e) }
86+
}
87+
88+
implicit private def toTableModel(table: JTable):ScriptNodeOutputColumnsTableModel = {
89+
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
90+
}
91+
8292
/**
8393
* Create column selection tab panel
8494
*/
@@ -91,35 +101,27 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
91101
doAppendInputColumns = new JCheckBox("Append columns to input table spec")
92102
newtableCBPanel.add(doAppendInputColumns, BorderLayout.WEST)
93103
val addButton = new JButton("Add Output Column")
94-
addButton.addActionListener(new ActionListener() {
95-
96-
def actionPerformed(e: ActionEvent) {
104+
addButton.addActionListener((_: ActionEvent) => {
97105
var name: String = null
98-
val model = table.getModel
99-
.asInstanceOf[ScriptNodeOutputColumnsTableModel]
106+
val model:ScriptNodeOutputColumnsTableModel = table
100107
val columns = model.getDataTableColumnNames
101108
do {
102109
name = "script output " + columnCounter
103110
columnCounter += 1
104111
} while (columns.indexWhere(_ == name) >= 0);
105112
model.addRow(name, "String")
106-
}
107113
})
108114
val removeButton = new JButton("Remove Output Column")
109-
removeButton.addActionListener(new ActionListener() {
110-
111-
def actionPerformed(e: ActionEvent) {
115+
removeButton.addActionListener((_: ActionEvent) => {
112116
val selectedRows = table.getSelectedRows
113117
logger.debug("selectedRows = " + selectedRows)
114118
if (selectedRows.length == 0) {
115119
return
116120
}
117121
for (i <- selectedRows.length - 1 to 0) {
118122
logger.debug(" removal " + i + ": removing row " + selectedRows(i))
119-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
120-
.removeRow(selectedRows(i))
123+
table.removeRow(selectedRows(i))
121124
}
122-
}
123125
})
124126

125127
table = new JTable()
@@ -134,26 +136,23 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
134136

135137
def createButtonForRowsMoving(title: String, func: (Array[Int]) => (Int, Int)): JButton = {
136138
val result = new JButton(title)
137-
result.addActionListener(new ActionListener() {
138-
139-
def actionPerformed(e: ActionEvent) {
139+
result.addActionListener((_: ActionEvent) => {
140140
val selectedRows = table.getSelectedRows
141141
logger.debug("selectedRows = " + selectedRows)
142142
if (selectedRows.length > 0) {
143143
val position = func(selectedRows)
144144
table.setRowSelectionInterval(position._1, position._2)
145145
}
146-
}
147146
})
148147
result
149148
}
150149

151150
val upButton = createButtonForRowsMoving(
152151
"Up",
153-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel].moveRowsUp)
152+
table.moveRowsUp)
154153
val downButton = createButtonForRowsMoving(
155154
"Down",
156-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel].moveRowsDown)
155+
table.moveRowsDown)
157156

158157
Array(addButton, removeButton, Box.createHorizontalStrut(40),
159158
upButton, downButton).foreach(outputButtonPanel.add)
@@ -199,11 +198,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
199198
scriptPanel = new JPanel(new BorderLayout())
200199
val scriptButtonPanel = new JPanel()
201200
val scriptButton = new JButton("Load Script from File")
202-
scriptButton.addActionListener(new ActionListener() {
203-
204-
private var fileChooser: JFileChooser = new JFileChooser()
205-
206-
def actionPerformed(e: ActionEvent) {
201+
scriptButton.addActionListener((e: ActionEvent) => {
207202
val returnVal = fileChooser.showOpenDialog(
208203
e.getSource.asInstanceOf[Component])
209204
if (returnVal != JFileChooser.APPROVE_OPTION) {
@@ -213,21 +208,10 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
213208
if (!file.exists()) {
214209
return
215210
}
216-
val buffer = new StringBuffer()
217-
var reader: BufferedReader = null
218-
try {
219-
reader = new BufferedReader(new FileReader(file))
220-
while (reader.ready()) {
221-
val line = reader.readLine()
222-
buffer.append(line + "\n")
223-
}
224-
reader.close()
225-
} catch {
226-
case exc: IOException => exc.printStackTrace()
227-
}
228-
scriptTextArea.setText(buffer.toString)
211+
212+
val file_content = scala.io.Source.fromFile(file, "utf-8").mkString
213+
scriptTextArea.setText(file_content)
229214
clearErrorHighlight()
230-
}
231215
})
232216
scriptButtonPanel.add(scriptButton)
233217
val scriptMainPanel = new JPanel(new BorderLayout())
@@ -313,8 +297,7 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
313297
private def updateColumnTable(specs: Array[DataTableSpec]) {
314298
if (specs != null) {
315299
for (i <- 0 until specs.length) {
316-
val model = (columnTables(i).getModel)
317-
.asInstanceOf[ScriptNodeOutputColumnsTableModel]
300+
val model:ScriptNodeOutputColumnsTableModel = columnTables(i)
318301
model.clearRows()
319302
for (spec <- specs(i)) {
320303
model.addRow(spec.getName, spec.getType.toString)
@@ -346,19 +329,17 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
346329
val p = event.getPoint
347330
val row = table.rowAtPoint(p)
348331
if (row >= 0) {
349-
scriptTextArea.insert(String.format(TEMPLATE_FLOW_VAR,
350-
table.getModel.getValueAt(row, 0).toString),
332+
scriptTextArea.insert(String.format(TEMPLATE_FLOW_VAR,
333+
table.getModel.getValueAt(row, 0).toString),
351334
scriptTextArea.getCaretPosition)
352335
}
353336
}
354337
}
355338
})
356-
val flow_variables = factory.getModel.getAvailableFlowVariables
357-
358-
for (varDescr <- flow_variables.values) {
359-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
360-
.addRow(varDescr.getName, varDescr.getStringValue)
361-
}
339+
factory.getModel.getAvailableFlowVariables
340+
.values.foreach(
341+
varDescr => table.addRow(varDescr.getName, varDescr.getStringValue)
342+
)
362343
val scrollPane = new JScrollPane(table)
363344
table.setFillsViewportHeight(true)
364345
flowVariablesPanel.add(new JLabel(label), BorderLayout.NORTH)
@@ -383,9 +364,9 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
383364
case e1: BadLocationException =>
384365
}
385366
val outstr = new StringBuilder()
386-
outstr.append(error.text)
387-
outstr.append("\nline:\t class ( method )\t file\n")
388-
outstr.append(error.trace)
367+
outstr ++= error.text
368+
outstr ++= "\nline:\t class ( method )\t file\n"
369+
outstr ++= error.trace
389370
errorMessage.setText(outstr.toString)
390371
spErrorMessage.setVisible(true)
391372
setSelected("Script")
@@ -398,13 +379,12 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
398379
val dataTableColumnTypes =
399380
settings.getStringArray(RubyScriptNodeModel.COLUMN_TYPES,
400381
Array[String](): _*)
401-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel].clearRows()
382+
table.clearRows()
402383
if (dataTableColumnNames == null) {
403384
return
404385
}
405386
for (i <- 0 until dataTableColumnNames.length) {
406-
table.getModel.asInstanceOf[ScriptNodeOutputColumnsTableModel]
407-
.addRow(dataTableColumnNames(i), dataTableColumnTypes(i))
387+
table.addRow(dataTableColumnNames(i), dataTableColumnTypes(i))
408388
}
409389
updateColumnTable(specs)
410390
}
@@ -426,11 +406,9 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
426406
settings.addString(RubyScriptNodeModel.SCRIPT, scriptTextArea.getText)
427407
settings.addBoolean(RubyScriptNodeModel.APPEND_COLS,
428408
doAppendInputColumns.isSelected)
429-
val columnNames = table.getModel
430-
.asInstanceOf[ScriptNodeOutputColumnsTableModel].getDataTableColumnNames
409+
val columnNames = table.getDataTableColumnNames
431410
settings.addStringArray(RubyScriptNodeModel.COLUMN_NAMES, columnNames: _*)
432-
val columnTypes = table.getModel
433-
.asInstanceOf[ScriptNodeOutputColumnsTableModel].getDataTableColumnTypes
411+
val columnTypes = table.getDataTableColumnTypes
434412
settings.addStringArray(RubyScriptNodeModel.COLUMN_TYPES, columnTypes: _*)
435413
}
436414

0 commit comments

Comments
 (0)