Custom Actions

A Custom Action is a block of custom code (a Groovy closure) that can be run from a table's context menu. Custom Action code is executed in the Deephaven console (the client side), not on the query server. However, the full power of the query language is available in a Custom Action, and all query operations invoked by a Custom Action are still executed on the server.

Because Custom Action code runs in the Deephaven console, it is aware of the rows the user has selected, and can prompt for additional inputs. This is especially useful for updating Input Tables — combining the user's selection, the user's input, and query data allows sophisticated settings and calculations stored in Input Tables to be easily adjusted on the fly. The values in the input table, in turn, can drive further analyses.

Note

At this time, Custom Actions can only be used in persistent queries.

Variables in a Custom Action

All variables in the query's Groovy binding, including tables, will be available in the Custom Action's binding as it executes. In addition, Custom Actions may use two special variables that are automatically set on the Deephaven console:

  • selectionTable — Table object containing the rows that were selected when the Custom Action was run
  • parentFrame — A reference to the Java Frame object that contains the table from which the action was run. This is used to assist with dialog placement.

Defining a Custom Action

To define a Custom Action is to define a Groovy closure. The example below creates a Groovy closure called myAction, which updates the rows of an input table declared earlier.

Note

See also: Master groovy closures

Caution

Note: The table used in the add() method must have compatible columns with the input table. For the following Custom Action to run, the user will first need to import JOptionPane.

import javax.swing.JOptionPane

myAction = { ->
String name = JOptionPane.showInputDialog(parentFrame, "What is your name?")
updatedRows = selectionTable.update("Comment=`" + name +" was here`")
inputTable.add(updatedRows)
}

When myAction runs, it will present a dialog asking the user's name, then update the InputTable based on the data the user entered. Note: the updatedRows table will never be displayed, and will be discarded after the action completes.

To add Custom Action to a table, use the addCustomAction() function.

addCustomAction(theTable, "Update Comment", myAction)

When the query initializes, the context menu that appears when right-clicking on rows in the table will contain an "Update Comment" item. Clicking on this will execute the code for myAction.

Defining a Custom Action with No Selection

Some Custom Actions do not require a selection, such as those that add rows to an empty table or those that operate on an entire table. These Custom Actions must be activated from the column header context menu rather than from a table row. Consider the following closure, a slight modification from earlier:

myNoSelectionAction = { ->
String value = JOptionPane.showInputDialog(parentFrame, "What is your name?")
updatedRows = theInputTable.update("LastEditor=`" + value +"`")
(InputTable)theInputTable.getAttribute("InputTable")).add(updatedRows)
}

When myNoSelectionAction runs, the selectionTable is unused (and cannot be used), and all rows in the input table will be updated.

addCustomActionNoSelection(theTable, myAction, "Update Comment No Selection", myAction)