use of com.twosigma.beakerx.widget.ValueWidget in project beakerx by twosigma.
the class Interactive method interact.
@SuppressWarnings("unchecked")
public static synchronized void interact(MethodClosure function, Object... parameters) {
final List<ValueWidget<?>> widgets = widgetsFromAbbreviations(parameters);
for (ValueWidget<?> widget : widgets) {
widget.getComm().addMsgCallbackList(widget.new ValueChangeMsgCallbackHandler() {
private void processCode(Object... params) throws Exception {
Object call = function.call(getWidgetValues());
if (call instanceof String || call instanceof Number) {
label.setValue(call);
}
}
@Override
public void updateValue(Object value, Message message) {
try {
processCode();
} catch (Exception e) {
throw new IllegalStateException("Error occurred during updating interactive widget.", e);
}
}
private Object[] getWidgetValues() {
List<Object> ret = new ArrayList<>(widgets.size());
for (ValueWidget<?> wid : widgets) {
ret.add(wid.getValue());
}
return ret.toArray(new Object[ret.size()]);
}
});
logger.info("interact Widget: " + widget.getClass().getName());
}
widgets.forEach(Widget::display);
Object response = function.call(widgets.stream().map(ValueWidget::getValue).toArray());
if (response instanceof Widget) {
((Widget) response).display();
} else {
label = new Label();
label.setValue(response);
label.display();
}
}