Search in sources :

Example 1 with Label

use of com.twosigma.beakerx.widget.Label 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)

Example 2 with Label

use of com.twosigma.beakerx.widget.Label in project beakerx by twosigma.

the class LabelTest method label.

private Label label() throws NoSuchAlgorithmException {
    Label widget = new Label();
    groovyKernel.clearPublishedMessages();
    return widget;
}
Also used : Label(com.twosigma.beakerx.widget.Label)

Example 3 with Label

use of com.twosigma.beakerx.widget.Label in project beakerx by twosigma.

the class LabelTest method shouldSendCommMsgWhenValueChange.

@Test
public void shouldSendCommMsgWhenValueChange() throws Exception {
    // given
    Label widget = label();
    // when
    widget.setValue("1");
    // then
    verifyMsgForProperty(groovyKernel, Label.VALUE, "1");
}
Also used : Label(com.twosigma.beakerx.widget.Label) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Aggregations

Label (com.twosigma.beakerx.widget.Label)3 KernelTest (com.twosigma.beakerx.KernelTest)1 Message (com.twosigma.beakerx.message.Message)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 Test (org.junit.Test)1