Example usage for com.vaadin.ui Alignment BOTTOM_RIGHT

List of usage examples for com.vaadin.ui Alignment BOTTOM_RIGHT

Introduction

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

Prototype

Alignment BOTTOM_RIGHT

To view the source code for com.vaadin.ui Alignment BOTTOM_RIGHT.

Click Source Link

Usage

From source file:org.opennms.features.topology.ssh.internal.AuthWindow.java

License:Open Source License

/**
 * This constructor method spawns a window to authorize the
 * username and password input by the user. If the authroization
 * is sucessful, the user will be connected to the host at the 
 * given port through SSH, and the terminal emulator this window
 * will be replaced by a terminal emulator. 
 * /*from  w w  w . j  a v a 2  s  . c  om*/
 * @param host - The host name to connect to
 * @param port - The port number to connect to
 */
public AuthWindow(String host, int port) {
    super("Login");
    m_host = host;
    m_port = port;
    if ("".equals(m_host) || m_port == 0) {
        showOptions = true;
    }
    setCaption("Auth Window");
    setModal(true);
    setWidth("260px");
    setHeight("190px");
    if (showOptions)
        setHeight("260px");
    setResizable(false);

    Label hostLabel = new Label("Host: ");
    hostField = new TextField();
    //        hostField.setMaxLength(FIELD_BUFFER);

    Label portLabel = new Label("Port: ");
    portField = new TextField();
    //        portField.setMaxLength(FIELD_BUFFER);

    Label usernameLabel = new Label("Username: ");
    usernameField = new TextField();
    //        usernameField.setMaxLength(FIELD_BUFFER);

    Label passwordLabel = new Label("Password: ");
    passwordField = new PasswordField();
    passwordField.setMaxLength(FIELD_BUFFER);

    final Button loginButton = new Button("Login");
    loginButton.setClickShortcut(KeyCode.ENTER);
    client = SshClient.setUpDefaultClient();
    client.start();
    loginButton.addClickListener(this);
    GridLayout grid = new GridLayout(2, 2);
    if (showOptions) {
        grid = new GridLayout(2, 4);
        grid.addComponent(hostLabel);
        grid.addComponent(hostField);
        grid.addComponent(portLabel);
        grid.addComponent(portField);
    }
    grid.addComponent(usernameLabel);
    grid.addComponent(usernameField);
    grid.addComponent(passwordLabel);
    grid.addComponent(passwordField);
    grid.setSpacing(true);
    grid.setMargin(new MarginInfo(false, false, true, false));
    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(grid);
    layout.addComponent(loginButton);
    layout.setComponentAlignment(loginButton, Alignment.BOTTOM_RIGHT);
    setContent(layout);
}

From source file:org.opennms.features.vaadin.config.PromptWindow.java

License:Open Source License

/**
 * Instantiates a new Event Generator window.
 *
 * @param caption the caption//  w w  w .  ja v a 2s . c  om
 * @param fieldLabel the field label
 */
public PromptWindow(String caption, String fieldLabel) {
    setCaption(caption);
    setModal(true);
    setWidth("400px");
    setHeight("150px");
    setResizable(false);
    setClosable(false);
    addStyleName("dialog");

    fileName = new TextField(fieldLabel);
    fileName.setNullSettingAllowed(false);
    fileName.setWidth("100%");
    fileName.setRequired(true);
    fileName.setRequiredError("This field cannot be null.");

    okButton = new Button("Continue");
    okButton.addClickListener(this);

    cancelButton = new Button("Cancel");
    cancelButton.addClickListener(this);

    HorizontalLayout toolbar = new HorizontalLayout();
    toolbar.addComponent(okButton);
    toolbar.addComponent(cancelButton);

    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(fileName);
    layout.addComponent(toolbar);
    layout.setComponentAlignment(toolbar, Alignment.BOTTOM_RIGHT);

    setContent(layout);
}

From source file:org.opennms.features.vaadin.datacollection.IncludeCollectionWindow.java

License:Open Source License

/**
 * Instantiates a new include collection window.
 * // w  ww .  jav a  2  s.c  om
 * @param dataCollectionConfigDao the data collection configuration DAO
 * @param container the source list of elements
 * @param wrapper the current selected value
 */
public IncludeCollectionWindow(final DataCollectionConfigDao dataCollectionConfigDao,
        final OnmsBeanContainer<IncludeCollectionWrapper> container, final IncludeCollectionWrapper wrapper) {

    setCaption("Include SystemDef/DataCollectionGroup");
    setModal(true);
    setWidth("400px");
    setHeight("2000px");
    setResizable(false);
    setClosable(false);
    addStyleName("dialog");

    final ComboBox valueField = new ComboBox("Value");
    valueField.setEnabled(false);
    valueField.setRequired(true);
    valueField.setImmediate(true);
    valueField.setNewItemsAllowed(false);
    valueField.setNullSelectionAllowed(false);

    final ComboBox typeField = new ComboBox("Type");
    typeField.setRequired(true);
    typeField.setImmediate(true);
    typeField.setNewItemsAllowed(false);
    typeField.setNullSelectionAllowed(false);
    typeField.addItem(IncludeCollectionWrapper.DC_GROUP);
    typeField.addItem(IncludeCollectionWrapper.SYSTEM_DEF);
    typeField.addValueChangeListener(new Property.ValueChangeListener() {
        @Override
        public void valueChange(Property.ValueChangeEvent event) {
            String selected = (String) typeField.getValue();
            if (selected == null) {
                return;
            }
            // Get available fields.
            // FIXME If a new dcGroup is added, DataCollectionConfigDao is not able to reach it.
            List<String> values = selected.equals(IncludeCollectionWrapper.SYSTEM_DEF)
                    ? dataCollectionConfigDao.getAvailableSystemDefs()
                    : dataCollectionConfigDao.getAvailableDataCollectionGroups();
            // Remove already selected
            for (IncludeCollectionWrapper obj : container.getOnmsBeans()) {
                if (obj.getType().equals(selected)) {
                    values.remove(obj.getValue());
                }
            }
            // Updating combo-box
            valueField.removeAllItems();
            for (String v : values) {
                valueField.addItem(v);
            }
            if (wrapper.getValue() != null) {
                valueField.addItem(wrapper.getValue());
            }
            valueField.setEnabled(valueField.getItemIds().size() > 1);
        }
    });

    formLayout.setImmediate(true);
    formLayout.setWidth("100%");
    formLayout.addComponent(typeField);
    formLayout.addComponent(valueField);

    formEditor.bind(typeField, "type");
    formEditor.bind(valueField, "value");
    formEditor.setItemDataSource(wrapper);

    final HorizontalLayout toolbar = new HorizontalLayout();
    toolbar.addComponent(okButton);
    toolbar.addComponent(cancelButton);

    final VerticalLayout layout = new VerticalLayout();
    layout.addComponent(formLayout);
    layout.addComponent(toolbar);
    layout.setComponentAlignment(toolbar, Alignment.BOTTOM_RIGHT);
    layout.setMargin(true);
    setContent(layout);
}

From source file:org.opennms.features.vaadin.jmxconfiggenerator.ui.ConfirmationDialog.java

License:Open Source License

public ConfirmationDialog(String caption, String description) {
    setCaption(caption);//from   www  . ja v a 2 s  . co m
    setModal(true);
    setResizable(false);
    setClosable(false);
    setWidth(400, Unit.PIXELS);
    setHeight(200, Unit.PIXELS);
    addCloseListener(this);

    okButton = UIHelper.createButton("ok", null, null, this);
    cancelButton = UIHelper.createButton("cancel", "cancels the current action.", null, this);
    label.setDescription(description);

    final HorizontalLayout buttonLayout = new HorizontalLayout(okButton, cancelButton);
    buttonLayout.setSpacing(true);

    layout.setSpacing(true);
    layout.setMargin(true);
    layout.addComponent(label);
    layout.addComponent(buttonLayout);
    layout.setComponentAlignment(buttonLayout, Alignment.BOTTOM_RIGHT);

    setContent(layout);
    center();
}

From source file:org.opennms.features.vaadin.jmxconfiggenerator.ui.ProgressWindow.java

License:Open Source License

public ProgressWindow(final JobManager jobManager) {
    setCaption("processing...");
    setModal(true);//from   w w  w .  ja va2s  .co m
    setResizable(false);
    setClosable(false);
    setWidth(400, Unit.PIXELS);
    setHeight(150, Unit.PIXELS);

    Button cancelButton = UIHelper.createButton("cancel", "Cancels the current process", null,
            new Button.ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent event) {
                    jobManager.cancelAllJobs();
                    UIHelper.getCurrent(JmxConfigGeneratorUI.class)
                            .updateView(UiState.ServiceConfigurationView);
                }
            });

    progress.setIndeterminate(true); // infinite wheel, instead of 100% bar

    HorizontalLayout contentLayout = new HorizontalLayout(progress, label);
    contentLayout.setSpacing(true);

    layout.addComponent(contentLayout);
    layout.addComponent(cancelButton);
    layout.setComponentAlignment(cancelButton, Alignment.BOTTOM_RIGHT);
    layout.setSpacing(true);
    layout.setMargin(true);

    setContent(layout);
    center();

    addCloseListener(new CloseListener() {
        @Override
        public void windowClose(CloseEvent e) {
            // By default polling is disabled
            UIHelper.getCurrent(JmxConfigGeneratorUI.class).setPollInterval(-1);
        }
    });
}

From source file:org.opennms.features.vaadin.mibcompiler.EventUeiWindow.java

License:Open Source License

/**
 * Instantiates a new Event Generator window.
 * /* w  w  w  . j a v a  2 s .co m*/
 * @param defaultUei the default value for UEI base
 */
public EventUeiWindow(String defaultUei) {
    setCaption("Generate Events");
    setModal(true);
    setWidth("400px");
    setHeight("150px");
    setResizable(false);
    setClosable(false);
    addStyleName("dialog");

    ueiBase = new TextField("UEI Base");
    ueiBase.setNullSettingAllowed(false);
    ueiBase.setWidth("100%");
    ueiBase.setRequired(true);
    ueiBase.setValue(defaultUei);
    ueiBase.setRequiredError("UEI Base cannot be null.");

    okButton = new Button("Continue");
    okButton.addClickListener(this);

    cancelButton = new Button("Cancel");
    cancelButton.addClickListener(this);

    HorizontalLayout toolbar = new HorizontalLayout();
    toolbar.addComponent(okButton);
    toolbar.addComponent(cancelButton);

    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(ueiBase);
    layout.addComponent(toolbar);
    layout.setComponentAlignment(toolbar, Alignment.BOTTOM_RIGHT);
    setContent(layout);
}

From source file:org.opennms.features.vaadin.mibcompiler.FileEditorWindow.java

License:Open Source License

/**
 * Instantiates a new file editor window.
 *
 * @param file the file/*from   w ww.  ja v a  2s.  c o  m*/
 * @param logger the logger
 * @param readOnly true, if you want to display a read only window.
 */
public FileEditorWindow(final File file, final Logger logger, boolean readOnly) {
    this.file = file;
    this.logger = logger;

    setCaption((readOnly ? "View" : "Edit") + " MIB");
    addStyleName("dialog");
    setModal(true);
    setClosable(false);
    setWidth("800px");
    setHeight("540px");

    editor = new TextArea();
    editor.setPropertyDataSource(new TextFileProperty(file));
    editor.setImmediate(false);
    editor.setSizeFull();
    editor.setRows(30);
    editor.setReadOnly(readOnly);

    cancel = new Button(readOnly ? "Close" : "Cancel");
    cancel.setImmediate(false);
    cancel.addClickListener(this);
    save = new Button("Save");
    save.setImmediate(false);
    save.addClickListener(this);

    HorizontalLayout toolbar = new HorizontalLayout();
    toolbar.addComponent(cancel);
    if (!readOnly)
        toolbar.addComponent(save);

    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(editor);
    layout.addComponent(toolbar);
    layout.setExpandRatio(editor, 1.0f);
    layout.setComponentAlignment(toolbar, Alignment.BOTTOM_RIGHT);
    setContent(layout);
}

From source file:org.opennms.features.vaadin.nodemaps.internal.InvalidConfigurationWindow.java

License:Open Source License

public InvalidConfigurationWindow() {
    setModal(true);//from   w  ww. jav a 2  s.  c o  m
    setResizable(false);
    setWidth(500, Unit.PIXELS);
    setHeight(250, Unit.PIXELS);
    setCaption("Configuration Error");

    final Button btnClose = new Button("OK", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            close();
        }
    });
    final String errorMessage = getErrorLabel();
    final Label label = new Label(errorMessage, ContentMode.HTML);

    VerticalLayout rootLayout = new VerticalLayout();
    rootLayout.setSpacing(true);
    rootLayout.setMargin(true);
    rootLayout.addComponent(label);
    rootLayout.addComponent(btnClose);
    rootLayout.setExpandRatio(label, 1.0f);
    rootLayout.setComponentAlignment(btnClose, Alignment.BOTTOM_RIGHT);

    setContent(rootLayout);
    center();
}

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

License:Open Source License

/**
 * Constructor//  w  ww .  j  a va 2s. c  o  m
 *
 * @param businessService        the Business Service DTO instance to be configured
 * @param businessServiceManager the Business Service Manager
 */
@SuppressWarnings("unchecked")
public BusinessServiceEdgeEditWindow(final BusinessService businessService,
        final BusinessServiceManager businessServiceManager, final Edge edge) {
    super("Business Service Edge Edit");

    /**
     * Basic window setup
     */
    setModal(true);
    setClosable(false);
    setResizable(false);
    setWidth(650, Unit.PIXELS);
    setHeight(325, Unit.PIXELS);

    /**
     * Creating the root layout...
     */
    final VerticalLayout rootLayout = new VerticalLayout();
    rootLayout.setSpacing(true);
    rootLayout.setMargin(false);

    /**
     * ...and the nested layout
     */
    final FormLayout formLayout = new FormLayout();
    formLayout.setSpacing(true);
    formLayout.setMargin(true);

    /**
     * type selector box
     */
    m_typeSelect = new NativeSelect("Type");
    m_typeSelect.setId("edgeTypeSelector");
    m_typeSelect.setMultiSelect(false);
    m_typeSelect.setNewItemsAllowed(false);
    m_typeSelect.setNullSelectionAllowed(false);
    m_typeSelect.setRequired(true);
    m_typeSelect.addItem(EdgeType.CHILD_SERVICE);
    m_typeSelect.setItemCaption(EdgeType.CHILD_SERVICE, "Child Service");
    m_typeSelect.addItem(EdgeType.IP_SERVICE);
    m_typeSelect.setItemCaption(EdgeType.IP_SERVICE, "IP Service");
    m_typeSelect.addItem(EdgeType.REDUCTION_KEY);
    m_typeSelect.setItemCaption(EdgeType.REDUCTION_KEY, "Reduction Key");
    m_typeSelect.setWidth(100.0f, Unit.PERCENTAGE);
    formLayout.addComponent(m_typeSelect);

    // List of child services
    m_childServiceComponent = new ComboBox("Child Service");
    m_childServiceComponent.setId("childServiceList");
    m_childServiceComponent.setInputPrompt("No child service selected");
    m_childServiceComponent.setNewItemsAllowed(false);
    m_childServiceComponent.setNullSelectionAllowed(false);
    m_childServiceComponent.setWidth(100.0f, Unit.PERCENTAGE);
    m_childServiceComponent.setVisible(false);
    m_childServiceComponent.setImmediate(true);
    m_childServiceComponent.setValidationVisible(true);
    m_childServiceComponent.setFilteringMode(FilteringMode.CONTAINS);
    m_childServiceComponent.addItems(businessServiceManager.getFeasibleChildServices(businessService).stream()
            .sorted(Ordering.natural().onResultOf(s -> BusinessServiceEditWindow.describeBusinessService(s)))
            .collect(Collectors.toList()));
    m_childServiceComponent.getItemIds().forEach(item -> m_childServiceComponent.setItemCaption(item,
            BusinessServiceEditWindow.describeBusinessService((BusinessService) item)));
    formLayout.addComponent(m_childServiceComponent);

    // List of IP services
    m_ipServiceComponent = new ComboBox("IP Service");
    m_ipServiceComponent.setId("ipServiceList");
    m_ipServiceComponent.setInputPrompt("No IP service selected");
    m_ipServiceComponent.setNewItemsAllowed(false);
    m_ipServiceComponent.setNullSelectionAllowed(false);
    m_ipServiceComponent.setWidth(100.0f, Unit.PERCENTAGE);
    m_ipServiceComponent.setVisible(false);
    m_ipServiceComponent.setImmediate(true);
    m_ipServiceComponent.setValidationVisible(true);
    m_ipServiceComponent.setFilteringMode(FilteringMode.CONTAINS);
    m_ipServiceComponent.addItems(businessServiceManager.getAllIpServices().stream()
            .sorted(Ordering.natural().onResultOf(s -> BusinessServiceEditWindow.describeIpService(s)))
            .collect(Collectors.toList()));
    m_ipServiceComponent.getItemIds().forEach(item -> m_ipServiceComponent.setItemCaption(item,
            BusinessServiceEditWindow.describeIpService((IpService) item)));
    formLayout.addComponent(m_ipServiceComponent);

    /**
     * reduction key input field
     */
    m_reductionKeyComponent = new TextField("Reduction Key");
    m_reductionKeyComponent.setId("reductionKeyField");
    m_reductionKeyComponent.setWidth(100.0f, Unit.PERCENTAGE);
    m_reductionKeyComponent.setVisible(false);
    m_reductionKeyComponent.setImmediate(true);
    m_reductionKeyComponent.setValidationVisible(true);
    formLayout.addComponent(m_reductionKeyComponent);

    /**
     * the friendly name
     */

    m_friendlyNameField = new TextField("Friendly Name");
    m_friendlyNameField.setId("friendlyNameField");
    m_friendlyNameField.setWidth(100.0f, Unit.PERCENTAGE);
    m_friendlyNameField.setVisible(false);
    m_friendlyNameField.setImmediate(true);
    m_friendlyNameField.setValidationVisible(true);
    m_friendlyNameField.setNullSettingAllowed(true);
    m_friendlyNameField.setNullRepresentation("");
    m_friendlyNameField.setMaxLength(FRIENDLY_NAME_MAXLENGTH);
    formLayout.addComponent(m_friendlyNameField);

    /**
     * show and hide components
     */
    m_typeSelect.addValueChangeListener(event -> {
        m_childServiceComponent.setVisible(m_typeSelect.getValue() == EdgeType.CHILD_SERVICE);
        m_childServiceComponent.setRequired(m_typeSelect.getValue() == EdgeType.CHILD_SERVICE);
        m_ipServiceComponent.setVisible(m_typeSelect.getValue() == EdgeType.IP_SERVICE);
        m_ipServiceComponent.setRequired(m_typeSelect.getValue() == EdgeType.IP_SERVICE);
        m_reductionKeyComponent.setVisible(m_typeSelect.getValue() == EdgeType.REDUCTION_KEY);
        m_reductionKeyComponent.setRequired(m_typeSelect.getValue() == EdgeType.REDUCTION_KEY);
        m_friendlyNameField.setVisible(m_typeSelect.getValue() == EdgeType.REDUCTION_KEY
                || m_typeSelect.getValue() == EdgeType.IP_SERVICE);
    });

    /**
     * map function field
     */
    m_mapFunctionSelect = new NativeSelect("Map Function", ImmutableList.builder().add(Decrease.class)
            .add(Identity.class).add(Ignore.class).add(Increase.class).add(SetTo.class).build());
    m_mapFunctionSelect.setId("mapFunctionSelector");
    m_mapFunctionSelect.setNullSelectionAllowed(false);
    m_mapFunctionSelect.setMultiSelect(false);
    m_mapFunctionSelect.setNewItemsAllowed(false);
    m_mapFunctionSelect.setRequired(true);
    m_mapFunctionSelect.setWidth(100.0f, Unit.PERCENTAGE);

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

    formLayout.addComponent(m_mapFunctionSelect);

    /**
     * severity selection field
     */
    m_mapFunctionSeveritySelect = new NativeSelect("Severity");
    m_mapFunctionSeveritySelect.setMultiSelect(false);
    m_mapFunctionSeveritySelect.setNewItemsAllowed(false);
    m_mapFunctionSeveritySelect.setNullSelectionAllowed(false);
    m_mapFunctionSeveritySelect.setRequired(false);
    m_mapFunctionSeveritySelect.addItem(Status.CRITICAL);
    m_mapFunctionSeveritySelect.setItemCaption(Status.CRITICAL, "Critical");
    m_mapFunctionSeveritySelect.addItem(Status.MAJOR);
    m_mapFunctionSeveritySelect.setItemCaption(Status.MAJOR, "Major");
    m_mapFunctionSeveritySelect.addItem(Status.MINOR);
    m_mapFunctionSeveritySelect.setItemCaption(Status.MINOR, "Minor");
    m_mapFunctionSeveritySelect.addItem(Status.WARNING);
    m_mapFunctionSeveritySelect.setItemCaption(Status.WARNING, "Warning");
    m_mapFunctionSeveritySelect.addItem(Status.NORMAL);
    m_mapFunctionSeveritySelect.setItemCaption(Status.NORMAL, "Normal");
    m_mapFunctionSeveritySelect.addItem(Status.INDETERMINATE);
    m_mapFunctionSeveritySelect.setItemCaption(Status.INDETERMINATE, "Indeterminate");
    m_mapFunctionSeveritySelect.setWidth(100.0f, Unit.PERCENTAGE);
    m_mapFunctionSeveritySelect.setEnabled(false);
    m_mapFunctionSeveritySelect.setImmediate(true);
    m_mapFunctionSeveritySelect.setValidationVisible(true);
    formLayout.addComponent(m_mapFunctionSeveritySelect);

    /**
     * hide or show additional severity input field
     */
    m_mapFunctionSelect.addValueChangeListener(event -> {
        m_mapFunctionSeveritySelect.setEnabled(SetTo.class.equals(m_mapFunctionSelect.getValue()));
        m_mapFunctionSeveritySelect.setRequired(SetTo.class.equals(m_mapFunctionSelect.getValue()));
    });

    /**
     * the weight input field
     */
    m_weightField = new TextField("Weight");
    m_weightField.setId("weightField");
    m_weightField.setRequired(true);
    m_weightField.setWidth(100.0f, Unit.PERCENTAGE);
    m_weightField.addValidator(value -> {
        try {
            int intValue = Integer.parseInt((String) value);
            if (intValue <= 0) {
                throw new Validator.InvalidValueException("Weight must be > 0");
            }
        } catch (final NumberFormatException e) {
            throw new Validator.InvalidValueException("Weight must be a number");
        }
    });
    m_weightField.setImmediate(true);
    m_weightField.setValidationVisible(true);
    formLayout.addComponent(m_weightField);

    /**
     * setting the defaults
     */
    m_typeSelect.setValue(EdgeType.CHILD_SERVICE);
    m_mapFunctionSelect.setValue(Identity.class);
    m_mapFunctionSeveritySelect.setValue(Status.INDETERMINATE);
    m_weightField.setValue(Integer.toString(Edge.DEFAULT_WEIGHT));

    /**
     * add the button layout...
     */
    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    buttonLayout.setMargin(true);

    /**
     * ...and the save button
     */
    final Button saveButton = new Button(edge == null ? "Add Edge" : "Update Edge");
    saveButton.setId("saveEdgeButton");
    saveButton.addClickListener(UIHelper.getCurrent(TransactionAwareUI.class)
            .wrapInTransactionProxy((Button.ClickListener) event -> {
                if (!m_weightField.isValid())
                    return;
                if (!m_ipServiceComponent.isValid())
                    return;
                if (!m_childServiceComponent.isValid())
                    return;
                if (!m_reductionKeyComponent.isValid())
                    return;

                final MapFunction mapFunction = getMapFunction();
                final int weight = Integer.parseInt(m_weightField.getValue());

                /**
                 * in the case edge is not null, remove the old object...
                 */
                if (edge != null) {
                    businessService.removeEdge(edge);
                }

                /**
                 * ...and add the new edge
                 */
                switch ((EdgeType) m_typeSelect.getValue()) {
                case CHILD_SERVICE:
                    businessService.addChildEdge((BusinessService) m_childServiceComponent.getValue(),
                            mapFunction, weight);
                    break;
                case IP_SERVICE:
                    businessService.addIpServiceEdge((IpService) m_ipServiceComponent.getValue(), mapFunction,
                            weight, m_friendlyNameField.getValue());
                    break;
                case REDUCTION_KEY:
                    businessService.addReductionKeyEdge(m_reductionKeyComponent.getValue(), mapFunction, weight,
                            m_friendlyNameField.getValue());
                    break;
                }

                close();
            }));
    buttonLayout.addComponent(saveButton);

    /**
     * ...and a cancel button
     */
    final Button cancelButton = new Button("Cancel");
    cancelButton.setId("cancelEdgeButton");
    cancelButton.addClickListener((Button.ClickListener) event -> close());
    buttonLayout.addComponent(cancelButton);

    /**
     * when edge is not null, fill the components with values
     */
    if (edge != null) {
        edge.accept(new EdgeVisitor<Void>() {
            @Override
            public Void visit(IpServiceEdge edge) {
                m_typeSelect.setValue(EdgeType.IP_SERVICE);

                for (IpService ipService : (Collection<IpService>) m_ipServiceComponent.getItemIds()) {
                    if (ipService.getId() == edge.getIpService().getId()) {
                        m_ipServiceComponent.setValue(ipService);
                        break;
                    }
                }
                m_friendlyNameField.setValue(edge.getFriendlyName());
                m_ipServiceComponent.setEnabled(false);
                return null;
            }

            @Override
            public Void visit(ReductionKeyEdge edge) {
                m_typeSelect.setValue(EdgeType.REDUCTION_KEY);
                m_reductionKeyComponent.setValue(edge.getReductionKey());
                m_friendlyNameField.setValue(edge.getFriendlyName());
                m_reductionKeyComponent.setEnabled(false);
                return null;
            }

            @Override
            public Void visit(ChildEdge edge) {
                m_typeSelect.setValue(EdgeType.CHILD_SERVICE);
                m_childServiceComponent.setValue(edge.getChild());
                m_childServiceComponent.setEnabled(false);
                return null;
            }
        });

        m_typeSelect.setEnabled(false);
        m_mapFunctionSelect.setValue(edge.getMapFunction().getClass());

        edge.getMapFunction().accept(new MapFunctionVisitor<Void>() {
            @Override
            public Void visit(Decrease decrease) {
                m_mapFunctionSeveritySelect.setValue(Status.INDETERMINATE);
                return null;
            }

            @Override
            public Void visit(Identity identity) {
                m_mapFunctionSeveritySelect.setValue(Status.INDETERMINATE);
                return null;
            }

            @Override
            public Void visit(Ignore ignore) {
                m_mapFunctionSeveritySelect.setValue(Status.INDETERMINATE);
                return null;
            }

            @Override
            public Void visit(Increase increase) {
                m_mapFunctionSeveritySelect.setValue(Status.INDETERMINATE);
                return null;
            }

            @Override
            public Void visit(SetTo setTo) {
                m_mapFunctionSeveritySelect.setValue(((SetTo) edge.getMapFunction()).getStatus());
                return null;
            }
        });

        m_weightField.setValue(String.valueOf(edge.getWeight()));
    }

    /**
     * now set the root layout
     */
    rootLayout.addComponent(formLayout);
    rootLayout.addComponent(buttonLayout);
    rootLayout.setComponentAlignment(buttonLayout, Alignment.BOTTOM_RIGHT);
    setContent(rootLayout);
}

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

License:Open Source License

/**
 * Constructor/*from   w  w  w  .  j  av a  2 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);
}