Search in sources :

Example 1 with ValueWidget

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();
    }
}
Also used : ValueWidget(com.twosigma.beakerx.widget.ValueWidget) Message(com.twosigma.beakerx.message.Message) Widget(com.twosigma.beakerx.widget.Widget) ValueWidget(com.twosigma.beakerx.widget.ValueWidget) Label(com.twosigma.beakerx.widget.Label) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Message (com.twosigma.beakerx.message.Message)1 Label (com.twosigma.beakerx.widget.Label)1 ValueWidget (com.twosigma.beakerx.widget.ValueWidget)1 Widget (com.twosigma.beakerx.widget.Widget)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1