Example usage for com.vaadin.data Validator Validator

List of usage examples for com.vaadin.data Validator Validator

Introduction

In this page you can find the example usage for com.vaadin.data Validator Validator.

Prototype

Validator

Source Link

Usage

From source file:de.essendi.vaadin.ui.component.numberfield.NumberField.java

License:Apache License

private void createNumberValidator() {
    // Create our server-side validator
    numberValidator = new Validator() {

        public boolean isValid(Object value) {
            return validateValue(value);
        }// www. j  av a2 s .c o m

        public void validate(Object value) throws InvalidValueException {
            if (!isValid(value)) {
                throw new InvalidValueException(errorText);
            }
        }

    };
}

From source file:fi.semantum.strategia.Utils.java

License:Open Source License

public static void manage(final Main main) {

    final Database database = main.getDatabase();

    VerticalLayout content = new VerticalLayout();
    content.setSizeFull();/*from  www  .  j a  va2 s  .com*/
    content.setSpacing(true);

    HorizontalLayout hl1 = new HorizontalLayout();
    hl1.setSpacing(true);
    hl1.setWidth("100%");

    final ComboBox users = new ComboBox();
    users.setWidth("100%");
    users.setNullSelectionAllowed(false);
    users.addStyleName(ValoTheme.COMBOBOX_SMALL);
    users.setCaption("Valitse kyttj:");

    final Map<String, Account> accountMap = new HashMap<String, Account>();
    makeAccountCombo(main, accountMap, users);

    for (Account a : Account.enumerate(database)) {
        accountMap.put(a.getId(database), a);
        users.addItem(a.getId(database));
        users.select(a.getId(database));
    }

    final Table table = new Table();
    table.setSelectable(true);
    table.setMultiSelect(true);
    table.addStyleName(ValoTheme.TABLE_SMALL);
    table.addStyleName(ValoTheme.TABLE_SMALL);
    table.addStyleName(ValoTheme.TABLE_COMPACT);

    users.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 5036991262418844060L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            users.removeValueChangeListener(this);
            makeAccountCombo(main, accountMap, users);
            makeAccountTable(database, users, accountMap, table);
            users.addValueChangeListener(this);
        }

    });

    final TextField tf = new TextField();

    Validator nameValidator = new Validator() {

        private static final long serialVersionUID = -4779239111120669168L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            String s = (String) value;
            if (s.isEmpty())
                throw new InvalidValueException("Nimi ei saa olla tyhj");
            if (accountMap.containsKey(s))
                throw new InvalidValueException("Nimi on jo kytss");
        }

    };

    final Button save = new Button("Luo", new Button.ClickListener() {

        private static final long serialVersionUID = -6053708137324681886L;

        public void buttonClick(ClickEvent event) {

            if (!tf.isValid())
                return;

            String pass = Long.toString(Math.abs(UUID.randomUUID().getLeastSignificantBits()), 36);
            Account.create(database, tf.getValue(), "", Utils.hash(pass));

            Updates.update(main, true);

            makeAccountCombo(main, accountMap, users);
            makeAccountTable(database, users, accountMap, table);

            Dialogs.infoDialog(main, "Uusi kyttj '" + tf.getValue() + "' luotu",
                    "Kyttjn salasana on " + pass + "", null);

        }

    });
    save.addStyleName(ValoTheme.BUTTON_SMALL);

    final Button remove = new Button("Poista", new Button.ClickListener() {

        private static final long serialVersionUID = -5359199320445328801L;

        public void buttonClick(ClickEvent event) {

            Object selection = users.getValue();
            Account state = accountMap.get(selection);

            // System cannot be removed
            if ("System".equals(state.getId(database)))
                return;

            state.remove(database);

            Updates.update(main, true);

            makeAccountCombo(main, accountMap, users);
            makeAccountTable(database, users, accountMap, table);

        }

    });
    remove.addStyleName(ValoTheme.BUTTON_SMALL);

    tf.setWidth("100%");
    tf.addStyleName(ValoTheme.TEXTFIELD_SMALL);
    tf.setCaption("Luo uusi kyttj nimell:");
    tf.setValue(findFreshUserName(nameValidator));
    tf.setCursorPosition(tf.getValue().length());
    tf.setValidationVisible(true);
    tf.setInvalidCommitted(true);
    tf.setImmediate(true);
    tf.addTextChangeListener(new TextChangeListener() {

        private static final long serialVersionUID = -8274588731607481635L;

        @Override
        public void textChange(TextChangeEvent event) {
            tf.setValue(event.getText());
            try {
                tf.validate();
            } catch (InvalidValueException e) {
                save.setEnabled(false);
                return;
            }
            save.setEnabled(true);
        }

    });
    tf.addValidator(nameValidator);
    if (!tf.isValid())
        save.setEnabled(false);

    hl1.addComponent(users);
    hl1.setExpandRatio(users, 1.0f);
    hl1.setComponentAlignment(users, Alignment.BOTTOM_CENTER);

    hl1.addComponent(tf);
    hl1.setExpandRatio(tf, 1.0f);
    hl1.setComponentAlignment(tf, Alignment.BOTTOM_CENTER);

    hl1.addComponent(save);
    hl1.setExpandRatio(save, 0.0f);
    hl1.setComponentAlignment(save, Alignment.BOTTOM_CENTER);

    hl1.addComponent(remove);
    hl1.setExpandRatio(remove, 0.0f);
    hl1.setComponentAlignment(remove, Alignment.BOTTOM_CENTER);

    content.addComponent(hl1);
    content.setExpandRatio(hl1, 0.0f);

    table.addContainerProperty("Kartta", String.class, null);
    table.addContainerProperty("Oikeus", String.class, null);
    table.addContainerProperty("Laajuus", String.class, null);

    table.setWidth("100%");
    table.setHeight("100%");
    table.setNullSelectionAllowed(true);
    table.setMultiSelect(true);
    table.setCaption("Kyttjn oikeudet");

    makeAccountTable(database, users, accountMap, table);

    content.addComponent(table);
    content.setExpandRatio(table, 1.0f);

    final Button removeRights = new Button("Poista valitut rivit", new Button.ClickListener() {

        private static final long serialVersionUID = 4699670345358079045L;

        public void buttonClick(ClickEvent event) {

            Object user = users.getValue();
            Account state = accountMap.get(user);

            Object selection = table.getValue();
            Collection<?> sel = (Collection<?>) selection;

            List<Right> toRemove = new ArrayList<Right>();

            for (Object s : sel) {
                Integer index = (Integer) s;
                toRemove.add(state.rights.get(index - 1));
            }

            for (Right r : toRemove)
                state.rights.remove(r);

            Updates.update(main, true);

            makeAccountTable(database, users, accountMap, table);

        }

    });
    removeRights.addStyleName(ValoTheme.BUTTON_SMALL);

    table.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 1188285609779556446L;

        @Override
        public void valueChange(ValueChangeEvent event) {

            Object selection = table.getValue();
            Collection<?> sel = (Collection<?>) selection;
            if (sel.isEmpty())
                removeRights.setEnabled(false);
            else
                removeRights.setEnabled(true);

        }

    });

    final ComboBox mapSelect = new ComboBox();
    mapSelect.setWidth("100%");
    mapSelect.setNullSelectionAllowed(false);
    mapSelect.addStyleName(ValoTheme.COMBOBOX_SMALL);
    mapSelect.setCaption("Valitse kartta:");
    for (Strategiakartta a : Strategiakartta.enumerate(database)) {
        mapSelect.addItem(a.uuid);
        mapSelect.setItemCaption(a.uuid, a.getText(database));
        mapSelect.select(a.uuid);
    }

    final ComboBox rightSelect = new ComboBox();
    rightSelect.setWidth("100px");
    rightSelect.setNullSelectionAllowed(false);
    rightSelect.addStyleName(ValoTheme.COMBOBOX_SMALL);
    rightSelect.setCaption("Valitse oikeus:");
    rightSelect.addItem("Muokkaus");
    rightSelect.addItem("Luku");
    rightSelect.select("Luku");

    final ComboBox propagateSelect = new ComboBox();
    propagateSelect.setWidth("130px");
    propagateSelect.setNullSelectionAllowed(false);
    propagateSelect.addStyleName(ValoTheme.COMBOBOX_SMALL);
    propagateSelect.setCaption("Valitse laajuus:");
    propagateSelect.addItem(VALITTU_KARTTA);
    propagateSelect.addItem(ALATASON_KARTAT);
    propagateSelect.select(VALITTU_KARTTA);

    final Button addRight = new Button("Lis rivi", new Button.ClickListener() {

        private static final long serialVersionUID = -4841787792917761055L;

        public void buttonClick(ClickEvent event) {

            Object user = users.getValue();
            Account state = accountMap.get(user);

            String mapUUID = (String) mapSelect.getValue();
            Strategiakartta map = database.find(mapUUID);
            String right = (String) rightSelect.getValue();
            String propagate = (String) propagateSelect.getValue();

            Right r = new Right(map, right.equals("Muokkaus"), propagate.equals(ALATASON_KARTAT));
            state.rights.add(r);

            Updates.update(main, true);

            makeAccountTable(database, users, accountMap, table);

        }

    });
    addRight.addStyleName(ValoTheme.BUTTON_SMALL);

    table.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 6439090862804667322L;

        @Override
        public void valueChange(ValueChangeEvent event) {

            Object selection = table.getValue();
            Collection<?> selected = (Collection<?>) selection;
            if (!selected.isEmpty()) {
                removeRights.setEnabled(true);
            } else {
                removeRights.setEnabled(false);
            }

        }

    });

    HorizontalLayout hl2 = new HorizontalLayout();
    hl2.setSpacing(true);
    hl2.setWidth("100%");

    hl2.addComponent(removeRights);
    hl2.setComponentAlignment(removeRights, Alignment.TOP_LEFT);
    hl2.setExpandRatio(removeRights, 0.0f);

    hl2.addComponent(addRight);
    hl2.setComponentAlignment(addRight, Alignment.BOTTOM_LEFT);
    hl2.setExpandRatio(addRight, 0.0f);

    hl2.addComponent(mapSelect);
    hl2.setComponentAlignment(mapSelect, Alignment.BOTTOM_LEFT);
    hl2.setExpandRatio(mapSelect, 1.0f);

    hl2.addComponent(rightSelect);
    hl2.setComponentAlignment(rightSelect, Alignment.BOTTOM_LEFT);
    hl2.setExpandRatio(rightSelect, 0.0f);

    hl2.addComponent(propagateSelect);
    hl2.setComponentAlignment(propagateSelect, Alignment.BOTTOM_LEFT);
    hl2.setExpandRatio(propagateSelect, 0.0f);

    content.addComponent(hl2);
    content.setComponentAlignment(hl2, Alignment.BOTTOM_LEFT);
    content.setExpandRatio(hl2, 0.0f);

    final VerticalLayout vl = new VerticalLayout();

    final Panel p = new Panel();
    p.setWidth("100%");
    p.setHeight("200px");
    p.setContent(vl);

    final TimeConfiguration tc = TimeConfiguration.getInstance(database);

    final TextField tf2 = new TextField();
    tf2.setWidth("200px");
    tf2.addStyleName(ValoTheme.TEXTFIELD_SMALL);
    tf2.setCaption("Strategiakartan mritysaika:");
    tf2.setValue(tc.getRange());
    tf2.setCursorPosition(tf.getValue().length());
    tf2.setValidationVisible(true);
    tf2.setInvalidCommitted(true);
    tf2.setImmediate(true);
    tf2.addTextChangeListener(new TextChangeListener() {

        private static final long serialVersionUID = -8274588731607481635L;

        @Override
        public void textChange(TextChangeEvent event) {
            tf2.setValue(event.getText());
            try {
                tf2.validate();
                tc.setRange(event.getText());
                updateYears(database, vl);
                Updates.update(main, true);
            } catch (InvalidValueException e) {
                return;
            }
        }

    });
    tf2.addValidator(new Validator() {

        private static final long serialVersionUID = -4779239111120669168L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            String s = (String) value;
            TimeInterval ti = TimeInterval.parse(s);
            long start = ti.startYear;
            long end = ti.endYear;
            if (start < 2015)
                throw new InvalidValueException("Alkuvuosi ei voi olla aikaisempi kuin 2015.");
            if (end > 2025)
                throw new InvalidValueException("Pttymisvuosi ei voi olla myhisempi kuin 2025.");
            if (end - start > 9)
                throw new InvalidValueException("Strategiakartta ei tue yli 10 vuoden tarkasteluja.");
        }

    });

    content.addComponent(tf2);
    content.setComponentAlignment(tf2, Alignment.BOTTOM_LEFT);
    content.setExpandRatio(tf2, 0.0f);

    updateYears(database, vl);

    content.addComponent(p);
    content.setComponentAlignment(p, Alignment.BOTTOM_LEFT);
    content.setExpandRatio(p, 0.0f);

    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    buttons.setMargin(false);

    Dialogs.makeDialog(main, main.dialogWidth(), main.dialogHeight(0.8), "Hallinnoi strategiakarttaa", "Sulje",
            content, buttons);

}

From source file:fi.semantum.strategia.Utils.java

License:Open Source License

public static void saveCurrentState(final Main main) {

    VerticalLayout content = new VerticalLayout();
    content.setSizeFull();//from  w w  w  .j  a va 2s.c om

    HorizontalLayout hl1 = new HorizontalLayout();
    hl1.setSpacing(true);
    hl1.setWidth("100%");

    final Map<String, UIState> stateMap = new HashMap<String, UIState>();
    for (UIState s : main.account.uiStates)
        stateMap.put(s.name, s);

    final TextField tf = new TextField();

    final Button save = new Button("Tallenna nkym", new Button.ClickListener() {

        private static final long serialVersionUID = 2449606920686729881L;

        public void buttonClick(ClickEvent event) {

            if (!tf.isValid())
                return;

            String name = tf.getValue();

            Page.getCurrent().getJavaScript().execute("doSaveBrowserState('" + name + "');");

        }

    });

    tf.setWidth("100%");
    tf.addStyleName(ValoTheme.TEXTFIELD_SMALL);
    tf.setCaption("Tallenna nykyinen nkym nimell:");
    tf.setValue("Uusi nkym");
    tf.setCursorPosition(tf.getValue().length());
    tf.setValidationVisible(true);
    tf.setInvalidCommitted(true);
    tf.setImmediate(true);
    tf.addTextChangeListener(new TextChangeListener() {

        private static final long serialVersionUID = -8274588731607481635L;

        @Override
        public void textChange(TextChangeEvent event) {
            tf.setValue(event.getText());
            try {
                tf.validate();
            } catch (InvalidValueException e) {
                save.setEnabled(false);
                return;
            }
            save.setEnabled(true);
        }

    });
    tf.addValidator(new Validator() {

        private static final long serialVersionUID = -4779239111120669168L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            String s = (String) value;
            if (s.isEmpty())
                throw new InvalidValueException("Nimi ei saa olla tyhj");
            if (stateMap.containsKey(s))
                throw new InvalidValueException("Nimi on jo kytss");
        }

    });
    if (!tf.isValid())
        save.setEnabled(false);

    hl1.addComponent(tf);
    hl1.setExpandRatio(tf, 1.0f);

    hl1.addComponent(save);
    hl1.setExpandRatio(save, 0.0f);
    hl1.setComponentAlignment(save, Alignment.BOTTOM_CENTER);

    content.addComponent(hl1);
    content.setExpandRatio(hl1, 0.0f);

    final ListSelect table = new ListSelect();
    table.setWidth("100%");
    table.setHeight("100%");
    table.setNullSelectionAllowed(true);
    table.setMultiSelect(true);
    table.setCaption("Tallennetut nkymt");
    for (UIState state : main.account.uiStates) {
        table.addItem(state.name);
    }
    content.addComponent(table);
    content.setExpandRatio(table, 1.0f);

    final Button remove = new Button("Poista valitut nkymt");

    table.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 6439090862804667322L;

        @Override
        public void valueChange(ValueChangeEvent event) {

            Object selection = table.getValue();
            Collection<?> selected = (Collection<?>) selection;
            if (!selected.isEmpty()) {
                remove.setEnabled(true);
            } else {
                remove.setEnabled(false);
            }

        }

    });

    remove.setEnabled(false);

    content.addComponent(remove);
    content.setComponentAlignment(remove, Alignment.MIDDLE_LEFT);
    content.setExpandRatio(remove, 0.0f);

    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    buttons.setMargin(false);

    final Window dialog = Dialogs.makeDialog(main, "Nkymien hallinta", "Sulje", content, buttons);

    remove.addClickListener(new Button.ClickListener() {

        private static final long serialVersionUID = -4680588998085550908L;

        public void buttonClick(ClickEvent event) {

            Object selection = table.getValue();
            Collection<?> selected = (Collection<?>) selection;

            if (!selected.isEmpty()) {

                for (Object o : selected) {
                    UIState state = stateMap.get(o);
                    if (state != null)
                        main.account.uiStates.remove(state);
                }
                Updates.update(main, true);
            }

            main.removeWindow(dialog);

        }

    });

}

From source file:fi.semantum.strategia.widget.Datatype.java

License:Open Source License

public AbstractField<?> getEditor(final Main main, final Base base, final Indicator indicator,
        final boolean forecast, final CommentCallback callback) {

    Object value = forecast ? indicator.getForecast() : indicator.getValue();
    final String formatted = indicator.getDatatype(main.getDatabase()).format(value);

    final TextField tf = new TextField();
    tf.setValue(formatted);/*from  w  w  w . j a v  a2 s.  co m*/

    if (main.canWrite(base)) {

        tf.addValidator(new Validator() {

            private static final long serialVersionUID = 9043601075831736114L;

            @Override
            public void validate(Object value) throws InvalidValueException {

                try {
                    BigDecimal.valueOf(Double.parseDouble((String) value));
                } catch (NumberFormatException e) {
                    throw new InvalidValueException("Arvon tulee olla numero");
                }

            }

        });
        tf.addValueChangeListener(new ValueChangeListener() {

            private static final long serialVersionUID = 3547126051252580446L;

            @Override
            public void valueChange(ValueChangeEvent event) {

                try {
                    final BigDecimal number = BigDecimal.valueOf(Double.parseDouble(tf.getValue()));
                    indicator.updateWithComment(main, base, number, forecast, new AbstractCommentCallback() {

                        public void canceled() {
                            tf.setValue(formatted);
                            if (callback != null)
                                callback.canceled();
                        }

                        public void runWithComment(String shortComment, String comment) {
                            if (callback != null)
                                callback.runWithComment(shortComment, comment);
                        }

                    });
                } catch (NumberFormatException e) {
                    tf.setComponentError(new UserError("Arvon tulee olla numero"));
                }

            }

        });

    } else {

        tf.setReadOnly(true);

    }

    return tf;

}

From source file:fi.semantum.strategia.widget.NumberTrafficValuation.java

License:Open Source License

private void applyValues(Main main, Meter meter, ComboBox combo, VerticalLayout vl1, Label l1,
        VerticalLayout vl2, Label l2, VerticalLayout vl3, Label l3, TextField tf1, TextField tf2) {

    inApply = true;//from  ww w  .ja v  a2  s . co m

    Indicator indicator = meter.getPossibleIndicator(main.getDatabase());
    String unit = indicator.getUnit();

    combo.select(getState());

    final double highDouble = getHighLimit().doubleValue();
    final double lowDouble = getLowLimit().doubleValue();

    String highLimit = df.format(highDouble);
    String lowLimit = df.format(lowDouble);

    tf1.setValue(highLimit);
    l1.setValue(highLimit + " < arvo " + unit);

    if (isIncrease()) {
        vl1.setStyleName("greenBlock");
    } else {
        vl1.setStyleName("redBlock");
    }

    tf1.removeAllValidators();
    tf1.addValidator(new Validator() {

        private static final long serialVersionUID = 5929569929930454714L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            try {
                double d = Double.parseDouble((String) value);
                if (d < lowDouble)
                    throw new InvalidValueException("Value is too small");
            } catch (NumberFormatException e) {
                throw new InvalidValueException(e.getMessage());
            }
        }

    });

    if (isTwo()) {

        l3.setVisible(false);
        vl3.setVisible(false);
        tf2.setVisible(false);
        tf1.setCaption("Raja-arvo");

        if (isIncrease()) {
            vl2.setStyleName("redBlock");
        } else {
            vl2.setStyleName("greenBlock");
        }

        l2.setValue("arvo < " + lowLimit + " " + unit);

    } else {

        vl3.setVisible(true);
        l3.setVisible(true);
        tf2.setVisible(true);
        tf1.setCaption("Suurempi raja-arvo");
        tf2.setCaption("Pienempi raja-arvo");
        vl2.setStyleName("yellowBlock");

        l2.setValue(lowLimit + " < arvo < " + highLimit + " " + unit);
        l3.setValue("arvo < " + lowLimit + " " + unit);
        tf2.setValue(lowLimit);

        if (isIncrease()) {
            vl3.setStyleName("redBlock");
        } else {
            vl3.setStyleName("greenBlock");
        }

        tf2.removeAllValidators();
        tf2.addValidator(new Validator() {

            private static final long serialVersionUID = 5929569929930454714L;

            @Override
            public void validate(Object value) throws InvalidValueException {
                try {
                    double d = Double.parseDouble((String) value);
                    if (d > highDouble)
                        throw new InvalidValueException("Value is too large");
                } catch (NumberFormatException e) {
                    throw new InvalidValueException(e.getMessage());
                }
            }

        });

    }

    inApply = false;

}

From source file:jp.primecloud.auto.ui.WinServerAddSimple.java

License:Open Source License

private void initValidation() {

    String message = ViewMessages.getMessage("IUI-000025");
    prefixField.setRequired(true);/*from  w ww  .  j av a 2 s .  com*/
    prefixField.setRequiredError(message);
    prefixField.addValidator(new StringLengthValidator(message, 1, 10, false));
    prefixField.addValidator(new RegexpValidator("^[a-z]|[a-z][0-9a-z-]*[0-9a-z]$", true, message));

    Validator serverNumberValidator = new Validator() {
        public boolean isValid(Object value) {
            if (value == null || !(value instanceof Integer)) {
                return false;
            } else {
                return ((Integer) value >= 1 && (Integer) value <= MAX_ADD_SERVER);
            }
        }

        public void validate(Object value) throws InvalidValueException {
            String message = ViewMessages.getMessage("IUI-000026");
            if (!isValid(value)) {
                throw new InvalidValueException(message);
            }
        }
    };

    //        message = ViewMessages.getMessage("IUI-000026");
    serverNumber.setRequired(true);
    //        serverNumber.setRequiredError(message);
    //        serverNumber.addValidator(new RegexpValidator("^[1-9]|10$", true, message));
    serverNumber.addValidator(serverNumberValidator);
}

From source file:jp.primecloud.auto.ui.WinUserAuthAddEdit.java

License:Open Source License

private void initValidation() {
    //??//from   w w w  .  j  a  v  a2  s.c  o m
    String message = ViewMessages.getMessage("IUI-000117");
    userNameField.setRequired(true);
    userNameField.setRequiredError(message);
    userNameField.addValidator(new StringLengthValidator(message, -1, 15, false));
    userNameField.addValidator(new RegexpValidator("^[a-z]|[a-z][0-9a-z-]*[0-9a-z]$", true, message));

    //
    message = ViewMessages.getMessage("IUI-000118");
    passwordField.setRequired(true);
    passwordField.setRequiredError(message);
    passwordField.addValidator(new StringLengthValidator(message, 1, 15, false));
    passwordField.addValidator(new Validator() {
        @Override
        public void validate(Object value) throws InvalidValueException {
            if (isValid(value)) {
                throw new InvalidValueException(ViewMessages.getMessage("IUI-000118"));
            }
        }

        @Override
        public boolean isValid(Object value) {
            if (value == null || !(value instanceof String)) {
                return false;
            }
            return ((String) value).matches(".*&.*|.*\".*|.*'.*|.*<.*|.*>.*");
        }
    });
}

From source file:org.activiti.explorer.ui.management.identity.NewGroupPopupWindow.java

License:Apache License

protected void initInputFields() {
    // Input fields
    form.addField("id", new TextField(i18nManager.getMessage(Messages.GROUP_ID)));

    // Set id field to required
    form.getField("id").setRequired(true);
    form.getField("id").setRequiredError(i18nManager.getMessage(Messages.GROUP_ID_REQUIRED));
    form.getField("id").focus();

    // Set id field to be unique
    form.getField("id").addValidator(new Validator() {
        public void validate(Object value) throws InvalidValueException {
            if (!isValid(value)) {
                throw new InvalidValueException(i18nManager.getMessage(Messages.GROUP_ID_UNIQUE));
            }//from   ww  w .  j  av  a2 s . c om
        }

        public boolean isValid(Object value) {
            if (value != null) {
                return identityService.createGroupQuery().groupId(value.toString()).singleResult() == null;
            }
            return false;
        }
    });

    form.addField("name", new TextField(i18nManager.getMessage(Messages.GROUP_NAME)));
    ComboBox typeComboBox = new ComboBox(i18nManager.getMessage(Messages.GROUP_TYPE),
            Arrays.asList("assignment", "security-role"));
    typeComboBox.select("assignment");
    form.addField("type", typeComboBox);
}

From source file:org.activiti.explorer.ui.management.identity.NewUserPopupWindow.java

License:Apache License

protected void initInputFields() {
    // Input fields
    form.addField("id", new TextField(i18nManager.getMessage(Messages.USER_ID)));

    // Set id field to required
    form.getField("id").setRequired(true);
    form.getField("id").setRequiredError(i18nManager.getMessage(Messages.USER_ID_REQUIRED));
    form.getField("id").focus();

    // Set id field to be unique
    form.getField("id").addValidator(new Validator() {
        public void validate(Object value) throws InvalidValueException {
            if (!isValid(value)) {
                throw new InvalidValueException(i18nManager.getMessage(Messages.USER_ID_UNIQUE));
            }//  w  ww  . j  av a  2  s .  co  m
        }

        public boolean isValid(Object value) {
            if (value != null) {
                return identityService.createUserQuery().userId(value.toString()).singleResult() == null;
            }
            return false;
        }
    });

    // Password is required
    form.addField("password", new PasswordField(i18nManager.getMessage(Messages.USER_PASSWORD)));
    form.getField("password").setRequired(true);
    form.getField("password").setRequiredError(i18nManager.getMessage(Messages.USER_PASSWORD_REQUIRED));

    // Password must be at least 5 characters
    StringLengthValidator passwordLengthValidator = new StringLengthValidator(
            i18nManager.getMessage(Messages.USER_PASSWORD_MIN_LENGTH, 5), 5, -1, false);
    form.getField("password").addValidator(passwordLengthValidator);

    form.addField("firstName", new TextField(i18nManager.getMessage(Messages.USER_FIRSTNAME)));
    form.addField("lastName", new TextField(i18nManager.getMessage(Messages.USER_LASTNAME)));
    form.addField("email", new TextField(i18nManager.getMessage(Messages.USER_EMAIL)));
}

From source file:org.apache.openaz.xacml.admin.view.windows.EditPDPGroupWindow.java

License:Apache License

protected void initializeText() {
    this.textName.setNullRepresentation("");
    this.textDescription.setNullRepresentation("");
    if (this.group != null) {
        this.textName.setValue(this.group.getName());
        this.textDescription.setValue(this.group.getDescription());
    }//from  w w w. j av a2 s .  c  o  m
    //
    // Validation
    //
    this.textName.addValidator(new Validator() {
        private static final long serialVersionUID = 1L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            assert value instanceof String;
            if (value == null) {
                throw new InvalidValueException("The name cannot be blank.");
            }
            // Group names must be unique so that user can distinguish between them (and we can create unique IDs from them)
            for (PDPGroup g : self.groups) {
                if (group != null && g.getId().equals(group.getId())) {
                    // ignore this group - we may or may not be changing the name
                    continue;
                }
                if (g.getName().equals(value.toString())) {
                    throw new InvalidValueException("Name must be unique");
                }
            }
        }
    });
    this.textName.addTextChangeListener(new TextChangeListener() {
        private static final long serialVersionUID = 1L;

        @Override
        public void textChange(TextChangeEvent event) {
            if (event.getText() == null || event.getText().isEmpty()) {
                self.buttonSave.setEnabled(false);
            } else {
                self.buttonSave.setEnabled(true);
            }
        }
    });
}