1111import java .awt .BorderLayout ;
1212import java .awt .Color ;
1313import java .awt .Component ;
14+ import java .awt .Point ;
1415import java .awt .event .*;
1516import java .io .BufferedReader ;
1617import java .io .File ;
1718import java .io .FileReader ;
1819import java .io .IOException ;
20+ import java .util .Iterator ;
21+ import java .util .List ;
22+ import java .util .Map ;
23+
1924import javax .swing .AbstractAction ;
2025import javax .swing .BoxLayout ;
2126import javax .swing .DefaultCellEditor ;
3136import javax .swing .table .TableCellEditor ;
3237import javax .swing .text .BadLocationException ;
3338
39+ import org .knime .core .data .DataColumnSpec ;
3440import org .knime .core .data .DataTableSpec ;
3541import org .knime .core .node .*;
42+ import org .knime .core .node .workflow .FlowVariable ;
3643
3744import javax .swing .JScrollPane ;
3845import javax .swing .JTextArea ;
4350
4451
4552public class RubyScriptNodeDialog extends NodeDialogPane {
53+
54+ private final static String TEMPLATE_FLOW_VAR = "FlowVariableList['%s'] " ;
55+
4656 private static NodeLogger logger = NodeLogger
4757 .getLogger (RubyScriptNodeDialog .class );
4858
@@ -67,25 +77,14 @@ protected RubyScriptNodeDialog(RubyScriptNodeFactory factory) {
6777
6878 m_factory = factory ;
6979
70- //scriptTextArea.setAutoscrolls(true);
71- //Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
72- //scriptTextArea.setFont(font);
73-
74- m_errorMessage = new JTextArea ();
75- m_sp_errorMessage = new JScrollPane (m_errorMessage );
76-
77- m_scriptTextArea = new RSyntaxTextArea ();
78- m_scriptTextArea .setSyntaxEditingStyle (SyntaxConstants .SYNTAX_STYLE_RUBY );
79- m_scriptTextArea .setCodeFoldingEnabled (true );
80- m_scriptTextArea .setAntiAliasingEnabled (true );
81- RTextScrollPane spScript = new RTextScrollPane (m_scriptTextArea );
82- spScript .setFoldIndicatorEnabled (true );
83-
84- Font font = new Font (Font .MONOSPACED , Font .PLAIN , 12 );
85- m_errorMessage .setFont (font );
86- m_errorMessage .setForeground (Color .RED );
87- m_errorMessage .setEditable (false );
80+ createColumnSelectionTab ();
81+ createScriptTab ();
82+ }
8883
84+ /**
85+ * Creates column selection tab panel
86+ */
87+ private final void createColumnSelectionTab () {
8988 // construct the output column selection panel
9089 JPanel outputPanel = new JPanel ();
9190 outputPanel .setLayout (new BoxLayout (outputPanel , BoxLayout .Y_AXIS ));
@@ -94,22 +93,23 @@ protected RubyScriptNodeDialog(RubyScriptNodeFactory factory) {
9493 JPanel newtableCBPanel = new JPanel ();
9594 m_appendColsCB = new JCheckBox ("Append columns to input table spec" );
9695 newtableCBPanel .add (m_appendColsCB , BorderLayout .WEST );
96+
9797 JButton addButton = new JButton (new AbstractAction () {
9898
9999 private static final long serialVersionUID = -743704737927962277L ;
100100
101101 public void actionPerformed (final ActionEvent e ) {
102102 String name ;
103- ScriptNodeOutputColumnsTableModel model =
104- (( ScriptNodeOutputColumnsTableModel ) m_table .getModel ());
103+ ScriptNodeOutputColumnsTableModel model = (( ScriptNodeOutputColumnsTableModel ) m_table
104+ .getModel ());
105105 String [] columns = model .getDataTableColumnNames ();
106106 boolean found ;
107107
108108 do {
109109 found = false ;
110110 name = "script output " + m_counter ;
111111 m_counter ++;
112- for (String s : columns ){
112+ for (String s : columns ) {
113113 if (name .equals (s )) {
114114 found = true ;
115115 break ;
@@ -172,11 +172,39 @@ public void actionPerformed(final ActionEvent e) {
172172 typeSelector .setEditable (true );
173173
174174 typeColumn .setCellEditor (new DefaultCellEditor (typeSelector ));
175+ addTab ("Script Output" , outputPanel );
176+ }
177+
178+ /**
179+ * Creates script tab panel which contains lists of columns, flow variables,
180+ * Ruby script etc.
181+ */
182+ private final void createScriptTab () {
183+ // scriptTextArea.setAutoscrolls(true);
184+ // Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
185+ // scriptTextArea.setFont(font);
186+
187+ m_errorMessage = new JTextArea ();
188+ m_sp_errorMessage = new JScrollPane (m_errorMessage );
189+
190+ m_scriptTextArea = new RSyntaxTextArea ();
191+ m_scriptTextArea
192+ .setSyntaxEditingStyle (SyntaxConstants .SYNTAX_STYLE_RUBY );
193+ m_scriptTextArea .setCodeFoldingEnabled (true );
194+ m_scriptTextArea .setAntiAliasingEnabled (true );
195+ RTextScrollPane spScript = new RTextScrollPane (m_scriptTextArea );
196+ spScript .setFoldIndicatorEnabled (true );
197+
198+ Font font = new Font (Font .MONOSPACED , Font .PLAIN , 12 );
199+ m_errorMessage .setFont (font );
200+ m_errorMessage .setForeground (Color .RED );
201+ m_errorMessage .setEditable (false );
175202
176- // construct the panel for script loading/authoring
177203 m_scriptPanel = new JPanel (new BorderLayout ());
178204
179205 JPanel scriptButtonPanel = new JPanel ();
206+
207+ // script load button
180208 JButton scriptButton = new JButton (new AbstractAction () {
181209
182210 private static final long serialVersionUID = 6097485154386131768L ;
@@ -219,26 +247,137 @@ public void actionPerformed(final ActionEvent e) {
219247 }
220248 });
221249 scriptButton .setText ("Load Script from File" );
222-
223250 scriptButtonPanel .add (scriptButton );
224251
225252 JPanel scriptMainPanel = new JPanel (new BorderLayout ());
226253 scriptMainPanel .add (new JLabel ("Script: " ), BorderLayout .NORTH );
227254
228- //scriptMainPanel.add(new JScrollPane(scriptTextArea),
229- // BorderLayout.CENTER);
255+ // scriptMainPanel.add(new JScrollPane(scriptTextArea),
256+ // BorderLayout.CENTER);
230257
231- //scriptMainPanel.add(spScript, BorderLayout.CENTER);
258+ // scriptMainPanel.add(spScript, BorderLayout.CENTER);
232259
233260 JSplitPane splitPane = new JSplitPane (JSplitPane .VERTICAL_SPLIT ,
234261 spScript , m_sp_errorMessage );
235262 scriptMainPanel .add (splitPane , BorderLayout .CENTER );
236-
263+
264+ // add output column list
265+ List <DataColumnSpec > list = m_factory .getModel ().getInputColumnList ();
266+ JPanel inputColumnsPanel = addColumnPane ("Input[0] columns: " , list );
267+
268+ // add flow variables
269+ JPanel flowVariablesPanel = addFlowVariablesPane ("Flow variables: " );
270+
271+ splitPane = new JSplitPane (JSplitPane .VERTICAL_SPLIT ,
272+ inputColumnsPanel , flowVariablesPanel );
273+
274+ splitPane .setDividerLocation (splitPane .getSize ().height
275+ - splitPane .getInsets ().bottom - splitPane .getDividerSize ()
276+ - 50 );
277+
278+ // scriptMainPanel.add(splitPane, BorderLayout.WEST);
279+ // scriptMainPanel.add(new JLabel("Script: "), BorderLayout.NORTH);
280+
237281 m_scriptPanel .add (scriptButtonPanel , BorderLayout .PAGE_START );
238282 m_scriptPanel .add (scriptMainPanel , BorderLayout .CENTER );
239-
240- addTab ("Script Output" , outputPanel );
241- addTab ("Script" , m_scriptPanel , false );
283+
284+ JSplitPane config_and_sript = new JSplitPane (
285+ JSplitPane .HORIZONTAL_SPLIT , splitPane , m_scriptPanel );
286+
287+ config_and_sript .setDividerLocation (200 );
288+
289+ // addTab("Script", m_scriptPanel, false);
290+ addTab ("Script" , config_and_sript , false );
291+ }
292+
293+ /**
294+ * Creates a panel of of input columns
295+ * @param label
296+ * @param list of columns
297+ * @return JPanel
298+ */
299+ private final JPanel addColumnPane (String label , List <DataColumnSpec > list ) {
300+ JPanel panel = new JPanel (new BorderLayout ());
301+ JTable table = new JTable ();
302+ table .putClientProperty ("terminateEditOnFocusLost" , Boolean .TRUE );
303+
304+ table .setAutoscrolls (true );
305+ //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
306+ table .setAutoResizeMode (JTable .AUTO_RESIZE_LAST_COLUMN );
307+
308+ ScriptNodeOutputColumnsTableModel model = new ScriptNodeOutputColumnsTableModel ();
309+ model .addColumn ("Column name" );
310+ model .addColumn ("Column type" );
311+ model .setReadOnly (true );
312+ table .setModel (model );
313+
314+ if (list != null ) {
315+ for (Iterator <DataColumnSpec > i = list .iterator (); i .hasNext ();) {
316+ DataColumnSpec spec = i .next ();
317+ ((ScriptNodeOutputColumnsTableModel ) (table .getModel ()))
318+ .addRow (spec .getName (), spec .getType ().toString ());
319+ }
320+ }
321+ JScrollPane scrollPane = new JScrollPane (table );
322+ table .setFillsViewportHeight (true );
323+
324+ panel .add (new JLabel (label ), BorderLayout .NORTH );
325+ // inputColumnsPanel.add(m_inpputColumnsTable.getTableHeader(),
326+ // BorderLayout.PAGE_START);
327+ panel .add (scrollPane , BorderLayout .CENTER );
328+ return panel ;
329+ }
330+
331+ /**
332+ * Creates a panel with flow variable list
333+ * @param label
334+ * @return JPanel
335+ */
336+ private final JPanel addFlowVariablesPane (String label ) {
337+ JPanel flowVariablesPanel = new JPanel (new BorderLayout ());
338+ JTable table = new JTable ();
339+ table .putClientProperty ("terminateEditOnFocusLost" , Boolean .TRUE );
340+
341+ table .setAutoscrolls (true );
342+ ScriptNodeOutputColumnsTableModel model = new ScriptNodeOutputColumnsTableModel ();
343+ model .addColumn ("Name" );
344+ model .addColumn ("Value" );
345+ model .setReadOnly (true );
346+ table .setModel (model );
347+ table .addMouseListener (new MouseAdapter () {
348+ public void mouseClicked (MouseEvent event ) {
349+ if (event .getClickCount () == 2 ) {
350+ JTable table = (JTable ) event .getSource ();
351+ Point p = event .getPoint ();
352+ int row = table .rowAtPoint (p );
353+
354+ m_scriptTextArea .insert (
355+ String .format (TEMPLATE_FLOW_VAR , table .getModel ()
356+ .getValueAt (row , 0 ).toString ()),
357+ m_scriptTextArea .getCaretPosition ());
358+ }
359+ }
360+ });
361+
362+ Map <String , FlowVariable > flow_variables = m_factory .getModel ()
363+ .getAvailableFlowVariables ();
364+ for (Iterator <FlowVariable > i = flow_variables .values ().iterator (); i
365+ .hasNext ();) {
366+ FlowVariable var = i .next ();
367+ ((ScriptNodeOutputColumnsTableModel ) (table .getModel ())).addRow (
368+ var .getName (), var .getStringValue ());
369+ }
370+
371+ JScrollPane scrollPane = new JScrollPane (table );
372+ table .setFillsViewportHeight (true );
373+
374+ flowVariablesPanel .add (new JLabel (label ),
375+ BorderLayout .NORTH );
376+ // flowVariablesPanel.add(m_flowVariableTable.getTableHeader(),
377+ // BorderLayout.PAGE_START);
378+ // flowVariablesPanel.add(m_flowVariableTable, BorderLayout.CENTER);
379+ flowVariablesPanel .add (scrollPane , BorderLayout .CENTER );
380+ return flowVariablesPanel ;
242381 }
243382
244383 /**
@@ -268,7 +407,7 @@ protected final void loadSettingsFrom(final NodeSettingsRO settings,
268407 m_errorMessage .setText (outstr .toString ());
269408
270409 m_sp_errorMessage .setVisible (true );
271-
410+
272411 setSelected ("Script" );
273412 }
274413
0 commit comments