Example usage for com.vaadin.server UserError UserError

List of usage examples for com.vaadin.server UserError UserError

Introduction

In this page you can find the example usage for com.vaadin.server UserError UserError.

Prototype

public UserError(String message, ContentMode contentMode, ErrorLevel errorLevel) 

Source Link

Document

Creates an error message with level and content mode.

Usage

From source file:org.opennms.features.topology.app.internal.operations.AddVertexToGroupOperation.java

License:Open Source License

@Override
public void execute(List<VertexRef> targets, final OperationContext operationContext) {
    if (targets == null || targets.isEmpty()) {
        return;//from w ww  .  j a v a2s . c  o m
    }

    final Logger log = LoggerFactory.getLogger(this.getClass());
    final GraphContainer graphContainer = operationContext.getGraphContainer();

    final Collection<VertexRef> vertices = removeChildren(operationContext.getGraphContainer(),
            determineTargets(targets.get(0), operationContext.getGraphContainer().getSelectionManager()));
    final Collection<Vertex> vertexIds = graphContainer.getBaseTopology().getRootGroup();
    final Collection<Vertex> groupIds = findGroups(graphContainer.getBaseTopology(), vertexIds);

    final UI window = operationContext.getMainWindow();

    final Window groupNamePrompt = new GroupWindow("Add This Item To a Group", "300px", "210px");

    // Define the fields for the form
    final PropertysetItem item = new PropertysetItem();
    item.addItemProperty("Group", new ObjectProperty<String>(null, String.class));

    // field factory for the form
    FormFieldFactory fieldFactory = new FormFieldFactory() {
        private static final long serialVersionUID = 2963683658636386720L;

        public Field<?> createField(Item item, Object propertyId, Component uiContext) {
            // Identify the fields by their Property ID.
            String pid = (String) propertyId;
            if ("Group".equals(pid)) {
                final ComboBox select = new ComboBox("Group");
                for (Vertex childId : groupIds) {
                    log.debug("Adding child: {}, {}", childId.getId(), childId.getLabel());
                    select.addItem(childId.getId());
                    select.setItemCaption(childId.getId(), childId.getLabel());
                }
                select.setNewItemsAllowed(false);
                select.setNullSelectionAllowed(false);
                select.setRequired(true);
                select.setRequiredError("You must select a group");
                select.addValidator(new Validator() {
                    private static final long serialVersionUID = -2466240291882827117L;

                    @Override
                    public void validate(Object value) throws InvalidValueException {
                        if (isValid(value))
                            return;
                        throw new InvalidValueException(String.format("You cannot add group '%s' to itself.",
                                select.getItemCaption(value)));
                    };

                    /**
                     * Ensures that if only one element is selected that this element cannot be added to itself.
                     * If there are more than one elements selected, we assume as valid. 
                     */
                    private boolean isValid(Object value) {
                        if (vertices.size() > 1)
                            return true; // more than 1 -> assume valid
                        final String groupId = (String) select.getValue();
                        // only one, check if we want to assign to ourself
                        for (VertexRef eachVertex : vertices) {
                            if (groupId.equals(eachVertex.getId())) {
                                return false;
                            }
                        }
                        return true;
                    }
                });
                return select;
            }
            return null; // Invalid field (property) name.
        }
    };

    // create the form
    final Form promptForm = new Form() {
        private static final long serialVersionUID = 8310646938173207767L;

        @Override
        public void commit() throws SourceException, InvalidValueException {
            super.commit();
            String groupId = (String) getField("Group").getValue();
            Vertex group = graphContainer.getBaseTopology()
                    .getVertex(graphContainer.getBaseTopology().getVertexNamespace(), groupId);
            log.debug("Field value: {}", group.getId());
            for (VertexRef eachChild : vertices) {
                if (eachChild == group) {
                    log.warn("Ignoring group:(id={},label={}), because otherwise we should add it to itself.",
                            eachChild.getId(), eachChild.getLabel());
                    continue;
                }
                log.debug("Adding item:(id={},label={}) to group:(id={},label={})", eachChild.getId(),
                        eachChild.getLabel(), group.getId(), group.getLabel());
                graphContainer.getBaseTopology().setParent(eachChild, group);
            }
            graphContainer.getBaseTopology().save();
            graphContainer.redoLayout();
        }
    };
    // Buffer changes to the datasource
    promptForm.setBuffered(true);
    // You must set the FormFieldFactory before you set the data source
    promptForm.setFormFieldFactory(fieldFactory);
    promptForm.setItemDataSource(item);
    promptForm.setDescription("Please select a group.");

    // Footer
    Button ok = new Button("OK");
    ok.addClickListener(new ClickListener() {

        private static final long serialVersionUID = 7388841001913090428L;

        @Override
        public void buttonClick(ClickEvent event) {
            try {
                promptForm.validate();
                promptForm.commit();
                window.removeWindow(groupNamePrompt); // Close the prompt window
            } catch (InvalidValueException exception) {
                promptForm.setComponentError(
                        new UserError(exception.getMessage(), ContentMode.TEXT, ErrorLevel.WARNING));
            }
        }
    });

    Button cancel = new Button("Cancel");
    cancel.addClickListener(new ClickListener() {

        private static final long serialVersionUID = 8780989646038333243L;

        @Override
        public void buttonClick(ClickEvent event) {
            window.removeWindow(groupNamePrompt); // Close the prompt window
        }
    });

    promptForm.setFooter(new HorizontalLayout());
    promptForm.getFooter().addComponent(ok);
    promptForm.getFooter().addComponent(cancel);

    groupNamePrompt.setContent(promptForm);

    window.addWindow(groupNamePrompt);
}

From source file:org.opennms.features.topology.app.internal.operations.CreateGroupOperation.java

License:Open Source License

@Override
public void execute(final List<VertexRef> targets, final OperationContext operationContext) {
    if (targets == null || targets.isEmpty()) {
        return;/*from ww w.j  av  a 2  s. c o m*/
    }

    final GraphContainer graphContainer = operationContext.getGraphContainer();

    final UI window = operationContext.getMainWindow();

    final Window groupNamePrompt = new GroupWindow("Create Group", "300px", "200px");

    // Define the fields for the form
    final PropertysetItem item = new PropertysetItem();
    item.addItemProperty("Group Label", new ObjectProperty<String>("", String.class) {
        private static final long serialVersionUID = -7904501088179818863L;

        @Override
        public void setValue(String newValue) throws ReadOnlyException, ConversionException {
            if (newValue == null) {
                super.setValue(newValue);
            } else {
                super.setValue(newValue.trim());
            }
        }

        @Override
        public String getValue() {
            String value = super.getValue();
            if (value != null)
                return value.trim();
            return value;
        }
    });

    final Form promptForm = new Form() {

        private static final long serialVersionUID = 8938663493202118574L;

        @Override
        public void commit() {
            // Trim the form value
            Field<String> field = getField("Group Label");
            String groupLabel = field.getValue();
            if (groupLabel == null) {
                throw new InvalidValueException("Group label cannot be null.");
            }
            getField("Group Label").setValue(groupLabel.trim());
            super.commit();
            createGroup(graphContainer, (String) getField("Group Label").getValue(), targets);
        }

        private void createGroup(final GraphContainer graphContainer, final String groupLabel,
                final List<VertexRef> targets) {

            // Add the new group
            VertexRef groupId = graphContainer.getBaseTopology().addGroup(groupLabel, GROUP_ICON_KEY);

            // Find a common parent group. If none can be found, then link the group to the
            // top of the topology
            Vertex parentGroup = null;
            for (VertexRef vertexRef : targets) {
                Vertex parent = graphContainer.getBaseTopology().getParent(vertexRef);
                if (parentGroup == null) {
                    parentGroup = parent;
                } else if (!parentGroup.equals(parent)) {
                    // If there are multiple parents present then attach the new group 
                    // to the top level of the hierarchy
                    parentGroup = null;
                    break;
                }
            }

            // Link all targets to the newly-created group
            for (VertexRef vertexRef : targets) {
                graphContainer.getBaseTopology().setParent(vertexRef, groupId);
            }

            // Set the parent of the new group to the selected top-level parent
            graphContainer.getBaseTopology().setParent(groupId, parentGroup);

            // Save the topology
            operationContext.getGraphContainer().getBaseTopology().save();

            graphContainer.redoLayout();
        }
    };

    // Buffer changes to the datasource
    promptForm.setBuffered(true);
    // Bind the item to create all of the fields
    promptForm.setItemDataSource(item);
    promptForm.setDescription("Please Enter the Name of the Group");
    // Add validators to the fields
    addValidators(promptForm, graphContainer);

    // Footer
    Button ok = new Button("OK");
    ok.addClickListener(new ClickListener() {

        private static final long serialVersionUID = 7388841001913090428L;

        @Override
        public void buttonClick(ClickEvent event) {
            try {
                promptForm.validate();
                promptForm.commit();
                window.removeWindow(groupNamePrompt); // Close the prompt window
            } catch (InvalidValueException exception) {
                promptForm.setComponentError(
                        new UserError(exception.getMessage(), ContentMode.TEXT, ErrorLevel.WARNING));
            }
        }
    });

    Button cancel = new Button("Cancel");
    cancel.addClickListener(new ClickListener() {

        private static final long serialVersionUID = 8780989646038333243L;

        @Override
        public void buttonClick(ClickEvent event) {
            window.removeWindow(groupNamePrompt); // Close the prompt window
        }
    });

    promptForm.setFooter(new HorizontalLayout());
    promptForm.getFooter().addComponent(ok);
    promptForm.getFooter().addComponent(cancel);

    groupNamePrompt.setContent(promptForm);

    window.addWindow(groupNamePrompt);
}

From source file:songstock.web.extensions.login.basic.BasicLoginForm.java

License:Open Source License

public void setErrorMessage(String message) {
    Notification.show("SongStock", message, Type.TRAY_NOTIFICATION);
    this.labelError.setValue("<i>" + message + "</i>");
    this.buttonLogin.setComponentError(new UserError(message, ContentMode.TEXT, ErrorLevel.WARNING));
}

From source file:songstock.web.extensions.payments.creditcard.CreditCardForm.java

License:Open Source License

public void setErrorMessage(String message) {
    Notification.show("SongStock", message, Type.TRAY_NOTIFICATION);
    this.labelError.setValue("<i>" + message + "</i>");
    this.buttonPay.setComponentError(new UserError(message, ContentMode.TEXT, ErrorLevel.WARNING));
}

From source file:songstock.web.extensions.searching.basic.BasicSearchForm.java

License:Open Source License

public void setErrorMessage(String message) {
    Notification.show("SongStock", message, Type.TRAY_NOTIFICATION);
    this.labelError.setValue("<i>" + message + "</i>");
    this.buttonSearch.setComponentError(new UserError(message, ContentMode.TEXT, ErrorLevel.WARNING));
}