Example usage for com.vaadin.ui NativeSelect NativeSelect

List of usage examples for com.vaadin.ui NativeSelect NativeSelect

Introduction

In this page you can find the example usage for com.vaadin.ui NativeSelect NativeSelect.

Prototype

public NativeSelect(String caption, DataProvider<T, ?> dataProvider) 

Source Link

Document

Creates a new NativeSelect with the given caption, using the given DataProvider as the source of data items.

Usage

From source file:org.opennms.netmgt.bsm.vaadin.adminpage.BusinessServiceEditWindow.java

License:Open Source License

/**
 * Constructor//from  w  w w.  j  ava2  s .  c  o m
 *
 * @param businessService the Business Service DTO instance to be configured
 */
@SuppressWarnings("unchecked")
public BusinessServiceEditWindow(BusinessService businessService,
        BusinessServiceManager businessServiceManager) {
    /**
     * set window title...
     */
    super("Business Service Edit");

    m_businessService = businessService;

    /**
     * ...and basic properties
     */
    setModal(true);
    setClosable(false);
    setResizable(false);
    setWidth(650, Unit.PIXELS);
    setHeight(550, Unit.PIXELS);

    /**
     * create set for Business Service names
     */
    m_businessServiceNames = businessServiceManager.getAllBusinessServices().stream()
            .map(BusinessService::getName).collect(Collectors.toSet());

    if (m_businessService.getName() != null) {
        m_businessServiceNames.remove(m_businessService.getName());
    }

    /**
     * construct the main layout
     */
    VerticalLayout verticalLayout = new VerticalLayout();
    verticalLayout.setSizeFull();
    verticalLayout.setSpacing(true);
    verticalLayout.setMargin(true);

    /**
     * add saveBusinessService button
     */
    Button saveButton = new Button("Save");
    saveButton.setId("saveButton");
    saveButton.addClickListener(
            UIHelper.getCurrent(TransactionAwareUI.class).wrapInTransactionProxy(new Button.ClickListener() {
                private static final long serialVersionUID = -5985304347211214365L;

                @Override
                public void buttonClick(Button.ClickEvent event) {
                    if (!m_thresholdTextField.isValid() || !m_nameTextField.isValid()) {
                        return;
                    }

                    final ReductionFunction reductionFunction = getReduceFunction();
                    businessService.setName(m_nameTextField.getValue().trim());
                    businessService.setReduceFunction(reductionFunction);
                    businessService.save();
                    close();
                }

                private ReductionFunction getReduceFunction() {
                    try {
                        final ReductionFunction reductionFunction = ((Class<? extends ReductionFunction>) m_reduceFunctionNativeSelect
                                .getValue()).newInstance();
                        reductionFunction.accept(new ReduceFunctionVisitor<Void>() {
                            @Override
                            public Void visit(HighestSeverity highestSeverity) {
                                return null;
                            }

                            @Override
                            public Void visit(HighestSeverityAbove highestSeverityAbove) {
                                highestSeverityAbove.setThreshold((Status) m_thresholdStatusSelect.getValue());
                                return null;
                            }

                            @Override
                            public Void visit(Threshold threshold) {
                                threshold.setThreshold(Float.parseFloat(m_thresholdTextField.getValue()));
                                return null;
                            }
                        });
                        return reductionFunction;
                    } catch (final InstantiationException | IllegalAccessException e) {
                        throw Throwables.propagate(e);
                    }
                }
            }));

    /**
     * add the cancel button
     */
    Button cancelButton = new Button("Cancel");
    cancelButton.setId("cancelButton");
    cancelButton.addClickListener(new Button.ClickListener() {
        private static final long serialVersionUID = 5306168797758047745L;

        @Override
        public void buttonClick(Button.ClickEvent event) {
            close();
        }
    });

    /**
     * add the buttons to a HorizontalLayout
     */
    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    buttonLayout.addComponent(saveButton);
    buttonLayout.addComponent(cancelButton);

    /**
     * instantiate the input fields
     */
    m_nameTextField = new TextField("Business Service Name");
    m_nameTextField.setId("nameField");
    m_nameTextField.setNullRepresentation("");
    m_nameTextField.setNullSettingAllowed(true);
    m_nameTextField.setValue(businessService.getName());
    m_nameTextField.setWidth(100, Unit.PERCENTAGE);
    m_nameTextField.setRequired(true);
    m_nameTextField.focus();
    m_nameTextField.addValidator(new AbstractStringValidator("Name must be unique") {
        private static final long serialVersionUID = 1L;

        @Override
        protected boolean isValidValue(String value) {
            return value != null && !m_businessServiceNames.contains(value);
        }
    });
    verticalLayout.addComponent(m_nameTextField);

    /**
     * create the reduce function component
     */

    m_reduceFunctionNativeSelect = new NativeSelect("Reduce Function", ImmutableList.builder()
            .add(HighestSeverity.class).add(Threshold.class).add(HighestSeverityAbove.class).build());
    m_reduceFunctionNativeSelect.setId("reduceFunctionNativeSelect");
    m_reduceFunctionNativeSelect.setWidth(100.0f, Unit.PERCENTAGE);
    m_reduceFunctionNativeSelect.setNullSelectionAllowed(false);
    m_reduceFunctionNativeSelect.setMultiSelect(false);
    m_reduceFunctionNativeSelect.setImmediate(true);
    m_reduceFunctionNativeSelect.setNewItemsAllowed(false);

    /**
     * setting the captions for items
     */
    m_reduceFunctionNativeSelect.getItemIds().forEach(
            itemId -> m_reduceFunctionNativeSelect.setItemCaption(itemId, ((Class<?>) itemId).getSimpleName()));

    verticalLayout.addComponent(m_reduceFunctionNativeSelect);

    m_thresholdTextField = new TextField("Threshold");
    m_thresholdTextField.setId("thresholdTextField");
    m_thresholdTextField.setRequired(false);
    m_thresholdTextField.setEnabled(false);
    m_thresholdTextField.setImmediate(true);
    m_thresholdTextField.setWidth(100.0f, Unit.PERCENTAGE);
    m_thresholdTextField.setValue("0.0");
    m_thresholdTextField.addValidator(v -> {
        if (m_thresholdTextField.isEnabled()) {
            try {
                final float value = Float.parseFloat(m_thresholdTextField.getValue());
                if (0.0f >= value || value > 1.0) {
                    throw new NumberFormatException();
                }
            } catch (final NumberFormatException e) {
                throw new Validator.InvalidValueException("Threshold must be a positive number");
            }
        }
    });

    verticalLayout.addComponent(m_thresholdTextField);

    /**
     * Status selection for "Highest Severity Above"
     */
    m_thresholdStatusSelect = new NativeSelect("Threshold");
    m_thresholdStatusSelect.setId("thresholdStatusSelect");
    m_thresholdStatusSelect.setRequired(false);
    m_thresholdStatusSelect.setEnabled(false);
    m_thresholdStatusSelect.setImmediate(true);
    m_thresholdStatusSelect.setWidth(100.0f, Unit.PERCENTAGE);
    m_thresholdStatusSelect.setMultiSelect(false);
    m_thresholdStatusSelect.setNewItemsAllowed(false);
    m_thresholdStatusSelect.setNullSelectionAllowed(false);
    for (Status eachStatus : Status.values()) {
        m_thresholdStatusSelect.addItem(eachStatus);
    }
    m_thresholdStatusSelect.setValue(Status.INDETERMINATE);
    m_thresholdStatusSelect.getItemIds()
            .forEach(itemId -> m_thresholdStatusSelect.setItemCaption(itemId, ((Status) itemId).getLabel()));
    verticalLayout.addComponent(m_thresholdStatusSelect);

    m_reduceFunctionNativeSelect.addValueChangeListener(ev -> {
        boolean thresholdFunction = m_reduceFunctionNativeSelect.getValue() == Threshold.class;
        boolean highestSeverityAboveFunction = m_reduceFunctionNativeSelect
                .getValue() == HighestSeverityAbove.class;

        setVisible(m_thresholdTextField, thresholdFunction);
        setVisible(m_thresholdStatusSelect, highestSeverityAboveFunction);
    });

    if (Objects.isNull(businessService.getReduceFunction())) {
        m_reduceFunctionNativeSelect.setValue(HighestSeverity.class);
    } else {
        m_reduceFunctionNativeSelect.setValue(businessService.getReduceFunction().getClass());

        businessService.getReduceFunction().accept(new ReduceFunctionVisitor<Void>() {
            @Override
            public Void visit(HighestSeverity highestSeverity) {
                return null;
            }

            @Override
            public Void visit(HighestSeverityAbove highestSeverityAbove) {
                m_thresholdStatusSelect.setValue(highestSeverityAbove.getThreshold());
                return null;
            }

            @Override
            public Void visit(Threshold threshold) {
                m_thresholdTextField.setValue(String.valueOf(threshold.getThreshold()));
                return null;
            }
        });
    }

    /**
     * create the edges list box
     */
    m_edgesListSelect = new ListSelect("Edges");
    m_edgesListSelect.setId("edgeList");
    m_edgesListSelect.setWidth(100.0f, Unit.PERCENTAGE);
    m_edgesListSelect.setRows(10);
    m_edgesListSelect.setNullSelectionAllowed(false);
    m_edgesListSelect.setMultiSelect(false);
    refreshEdges();

    HorizontalLayout edgesListAndButtonLayout = new HorizontalLayout();

    edgesListAndButtonLayout.setWidth(100.0f, Unit.PERCENTAGE);

    VerticalLayout edgesButtonLayout = new VerticalLayout();
    edgesButtonLayout.setWidth(110.0f, Unit.PIXELS);
    edgesButtonLayout.setSpacing(true);

    Button addEdgeButton = new Button("Add Edge");
    addEdgeButton.setId("addEdgeButton");
    addEdgeButton.setWidth(110.0f, Unit.PIXELS);
    addEdgeButton.addStyleName("small");
    edgesButtonLayout.addComponent(addEdgeButton);
    addEdgeButton.addClickListener((Button.ClickListener) event -> {
        final BusinessServiceEdgeEditWindow window = new BusinessServiceEdgeEditWindow(businessService,
                businessServiceManager, null);
        window.addCloseListener(e -> refreshEdges());
        this.getUI().addWindow(window);
    });

    Button editEdgeButton = new Button("Edit Edge");
    editEdgeButton.setId("editEdgeButton");
    editEdgeButton.setEnabled(false);
    editEdgeButton.setWidth(110.0f, Unit.PIXELS);
    editEdgeButton.addStyleName("small");
    edgesButtonLayout.addComponent(editEdgeButton);
    editEdgeButton.addClickListener((Button.ClickListener) event -> {
        final BusinessServiceEdgeEditWindow window = new BusinessServiceEdgeEditWindow(businessService,
                businessServiceManager, (Edge) m_edgesListSelect.getValue());
        window.addCloseListener(e -> refreshEdges());
        this.getUI().addWindow(window);
    });

    final Button removeEdgeButton = new Button("Remove Edge");
    removeEdgeButton.setId("removeEdgeButton");
    removeEdgeButton.setEnabled(false);
    removeEdgeButton.setWidth(110.0f, Unit.PIXELS);
    removeEdgeButton.addStyleName("small");
    edgesButtonLayout.addComponent(removeEdgeButton);

    m_edgesListSelect.addValueChangeListener((Property.ValueChangeListener) event -> {
        removeEdgeButton.setEnabled(event.getProperty().getValue() != null);
        editEdgeButton.setEnabled(event.getProperty().getValue() != null);
    });

    removeEdgeButton.addClickListener((Button.ClickListener) event -> {
        if (m_edgesListSelect.getValue() != null) {
            removeEdgeButton.setEnabled(false);
            ((Edge) m_edgesListSelect.getValue()).delete();
            refreshEdges();
        }
    });

    edgesListAndButtonLayout.setSpacing(true);
    edgesListAndButtonLayout.addComponent(m_edgesListSelect);
    edgesListAndButtonLayout.setExpandRatio(m_edgesListSelect, 1.0f);

    edgesListAndButtonLayout.addComponent(edgesButtonLayout);
    edgesListAndButtonLayout.setComponentAlignment(edgesButtonLayout, Alignment.BOTTOM_CENTER);
    verticalLayout.addComponent(edgesListAndButtonLayout);

    /**
     * create the attributes list box
     */
    m_attributesListSelect = new ListSelect("Attributes");
    m_attributesListSelect.setId("attributeList");
    m_attributesListSelect.setWidth(100.0f, Unit.PERCENTAGE);
    m_attributesListSelect.setRows(10);
    m_attributesListSelect.setNullSelectionAllowed(false);
    m_attributesListSelect.setMultiSelect(false);

    refreshAttributes();

    HorizontalLayout attributesListAndButtonLayout = new HorizontalLayout();

    attributesListAndButtonLayout.setWidth(100.0f, Unit.PERCENTAGE);

    VerticalLayout attributesButtonLayout = new VerticalLayout();
    attributesButtonLayout.setWidth(110.0f, Unit.PIXELS);
    attributesButtonLayout.setSpacing(true);

    Button addAttributeButton = new Button("Add Attribute");
    addAttributeButton.setId("addAttributeButton");
    addAttributeButton.setWidth(110.0f, Unit.PIXELS);
    addAttributeButton.addStyleName("small");
    attributesButtonLayout.addComponent(addAttributeButton);
    addAttributeButton.addClickListener((Button.ClickListener) event -> {
        KeyValueInputDialogWindow keyValueInputDialogWindow = new KeyValueInputDialogWindow()
                .withKeyFieldName("Key").withValueFieldName("Value").withCaption("Attribute").withKey("")
                .withValue("").withOkAction(new KeyValueInputDialogWindow.Action() {
                    @Override
                    public void execute(KeyValueInputDialogWindow window) {
                        m_businessService.getAttributes().put(window.getKey(), window.getValue());
                        refreshAttributes();
                    }
                }).withKeyValidator(new AbstractStringValidator("Key must not be empty") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected boolean isValidValue(String value) {
                        return !Strings.isNullOrEmpty(value);
                    }
                }).withKeyValidator(new AbstractStringValidator("Key must be unique") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected boolean isValidValue(String value) {
                        return !m_businessService.getAttributes().containsKey(value);
                    }
                }).focusKey();
        this.getUI().addWindow(keyValueInputDialogWindow);
        keyValueInputDialogWindow.focus();
    });

    Button editAttributeButton = new Button("Edit Attribute");
    editAttributeButton.setId("editAttributeButton");
    editAttributeButton.setEnabled(false);
    editAttributeButton.setWidth(110.0f, Unit.PIXELS);
    editAttributeButton.addStyleName("small");
    attributesButtonLayout.addComponent(editAttributeButton);
    editAttributeButton.addClickListener((Button.ClickListener) event -> {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) m_attributesListSelect.getValue();
        KeyValueInputDialogWindow keyValueInputDialogWindow = new KeyValueInputDialogWindow()
                .withKeyFieldName("Key").withValueFieldName("Value").withCaption("Attribute")
                .withKey(entry.getKey()).disableKey().withValue(entry.getValue())
                .withOkAction(new KeyValueInputDialogWindow.Action() {
                    @Override
                    public void execute(KeyValueInputDialogWindow window) {
                        m_businessService.getAttributes().put(window.getKey(), window.getValue());
                        refreshAttributes();
                    }
                }).focusValue();
        this.getUI().addWindow(keyValueInputDialogWindow);
        keyValueInputDialogWindow.focus();
    });

    final Button removeAttributeButton = new Button("Remove Attribute");
    removeAttributeButton.setId("removeAttributeButton");
    removeAttributeButton.setEnabled(false);
    removeAttributeButton.setWidth(110.0f, Unit.PIXELS);
    removeAttributeButton.addStyleName("small");
    attributesButtonLayout.addComponent(removeAttributeButton);

    m_attributesListSelect.addValueChangeListener((Property.ValueChangeListener) event -> {
        removeAttributeButton.setEnabled(event.getProperty().getValue() != null);
        editAttributeButton.setEnabled(event.getProperty().getValue() != null);
    });

    removeAttributeButton.addClickListener((Button.ClickListener) event -> {
        if (m_attributesListSelect.getValue() != null) {
            removeAttributeButton.setEnabled(false);
            m_businessService.getAttributes()
                    .remove(((Map.Entry<String, String>) m_attributesListSelect.getValue()).getKey());
            refreshAttributes();
        }
    });

    attributesListAndButtonLayout.setSpacing(true);
    attributesListAndButtonLayout.addComponent(m_attributesListSelect);
    attributesListAndButtonLayout.setExpandRatio(m_attributesListSelect, 1.0f);

    attributesListAndButtonLayout.addComponent(attributesButtonLayout);
    attributesListAndButtonLayout.setComponentAlignment(attributesButtonLayout, Alignment.BOTTOM_CENTER);
    verticalLayout.addComponent(attributesListAndButtonLayout);

    /**
     * now add the button layout to the main layout
     */
    verticalLayout.addComponent(buttonLayout);
    verticalLayout.setExpandRatio(buttonLayout, 1.0f);

    verticalLayout.setComponentAlignment(buttonLayout, Alignment.BOTTOM_RIGHT);

    /**
     * set the window's content
     */
    setContent(verticalLayout);
}

From source file:org.processbase.ui.bpm.generator.GeneratedWindow.java

License:Open Source License

private NativeSelect getNativeSelect(Widget widget, Collection options) {
    NativeSelect component = new NativeSelect(widget.getLabel(), options);
    return component;
}