/** * Instantiates a new collect field. * * @param caption the caption * @param groups the available groups */ public CollectField(String caption, List<String> groups) { setCaption(caption); selectField.setRows(10); selectField.setLeftColumnCaption("Available"); selectField.setRightColumnCaption("Selected"); for (String group : groups) { selectField.addItem(group); } }
void css(VerticalLayout layout) { // BEGIN-EXAMPLE: component.select.twincolselect.css // BOOK: components.selecting#twincolselect final TwinColSelect select = new TwinColSelect("Select Targets to Destroy"); select.addStyleName("twincolselectexample"); // Set the column captions (optional) select.setLeftColumnCaption("These are left"); select.setRightColumnCaption("These are done for"); // Put some data in the select String planets[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" }; for (int pl = 0; pl < planets.length; pl++) select.addItem(planets[pl]); // Set the number of visible items select.setHeight("200px"); select.setRows(6); // Set width by the number of character columns select.setColumns(10); // Preselect a few items HashSet<String> preselected = new HashSet<String>(); Collections.addAll(preselected, "Venus", "Earth", "Mars"); select.setValue(preselected); select.addListener( new Property.ValueChangeListener() { private static final long serialVersionUID = 6081009810084203857L; public void valueChange(ValueChangeEvent event) { System.out.println(event.getProperty().getType().getName()); if (event.getProperty().getValue() != null) System.out.println(event.getProperty().getValue().getClass().getName()); } }); select.setImmediate(true); layout.addComponent(select); // END-EXAMPLE: component.select.twincolselect.css }
/** * Builds main layout * * @return mainLayout VerticalLayout with all dialog components */ @AutoGenerated private VerticalLayout buildMainLayout(final boolean newUser) { mainLayout = new VerticalLayout(); mainLayout.setImmediate(false); mainLayout.setMargin(true); mainLayout.setSpacing(true); mainLayout.setWidth("380px"); users = userFacade.getAllUsers(); userDetailsLayout = new GridLayout(2, 5); userDetailsLayout.setImmediate(false); userDetailsLayout.setSpacing(true); userFullName = new TextField(); userFullName.setImmediate(true); userFullName.setWidth("250px"); userFullName.addValidator( new Validator() { private static final long serialVersionUID = 1L; @Override public void validate(Object value) throws InvalidValueException { if (value != null && (value.getClass() == String.class && !((String) value).isEmpty())) { String inputFullName = (String) value; Pattern namePattern1 = Pattern.compile( "([A-Za-zŽžÝýŮůÚúŤťŠšŘřÓóŇňÍíĚěÉéĎďČčÁáÄäÖößÜüÀàÈèÙùÂâÊêÎîÔôÛûÇçËëÏïÜüŸÿ]+\\s*)+", Pattern.UNICODE_CASE); Matcher m = namePattern1.matcher(inputFullName); if (!m.matches()) { ex = new InvalidValueException( "Full user name must start with a letter and can only consists of letters and spaces."); throw ex; } if ((m.end() - m.start()) < 2) { ex = new InvalidValueException("Full user name must contains more than one symbol"); throw ex; } } } }); userDetailsLayout.addComponent(new Label("Full user name:"), 0, 0); userDetailsLayout.addComponent(userFullName, 1, 0); userName = new TextField(); userName.setImmediate(true); userName.setWidth("250px"); userName.addValidator( new Validator() { private static final long serialVersionUID = 1L; @Override public void validate(Object value) throws InvalidValueException { if (value.getClass() == String.class && !((String) value).isEmpty()) { String inputName = (String) value; for (User user : users) { if (user.getUsername().equals(inputName)) { if (selectUser == null || (selectUser.getId() != user.getId())) { ex = new InvalidValueException("user with this user name is already exist"); throw ex; } } } return; } ex = new InvalidValueException("user name field must be filled"); throw ex; } }); userDetailsLayout.addComponent(new Label("User name:"), 0, 1); userDetailsLayout.addComponent(userName, 1, 1); password = new PasswordField(); password.setImmediate(true); password.setWidth("250px"); password.addFocusListener( new FocusListener() { private static final long serialVersionUID = 1L; @Override public void focus(FocusEvent event) { password.setValue(""); passwordConfim.setValue(""); passChanged = true; } }); Label passLabel = new Label("Password:"******"250px"); Label confirmLabel = new Label("Password<br>confirmation:"); passwordConfim.addFocusListener( new FocusListener() { private static final long serialVersionUID = 1L; @Override public void focus(FocusEvent event) { passwordConfim.setValue(""); passChanged = true; } }); confirmLabel.setContentMode(ContentMode.HTML); userDetailsLayout.addComponent(confirmLabel, 0, 3); userDetailsLayout.addComponent(passwordConfim, 1, 3); userEmail = new TextField(); userEmail.setImmediate(true); userEmail.setWidth("250px"); userEmail.addValidator( new Validator() { private static final long serialVersionUID = 1L; @Override public void validate(Object value) throws InvalidValueException { if (value.getClass() == String.class && !((String) value).isEmpty()) { String inputEmail = (String) value; if (!inputEmail.matches( "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})")) { ex = new InvalidValueException("wrong mail format"); throw ex; } return; } ex = new InvalidValueException("e-mail field must be filled"); throw ex; } }); userDetailsLayout.addComponent(new Label("E-mail:"), 0, 4); userDetailsLayout.addComponent(userEmail, 1, 4); userDetailsLayout.setColumnExpandRatio(0, 0.3f); userDetailsLayout.setColumnExpandRatio(1, 0.7f); roleSelector = new TwinColSelect(); roleSelector.addItem(Role.ROLE_ADMIN); roleSelector.addItem(Role.ROLE_USER); roleSelector.setNullSelectionAllowed(true); roleSelector.setMultiSelect(true); roleSelector.setImmediate(true); roleSelector.setWidth("335px"); roleSelector.setHeight("200px"); roleSelector.setLeftColumnCaption("Defined Roles:"); roleSelector.setRightColumnCaption("Set Roles:"); roleSelector.addValueChangeListener( new ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { Set<Object> selectedRoles = (Set<Object>) roleSelector.getValue(); Iterator<Object> it = selectedRoles.iterator(); Set<Role> role = new HashSet<>(); while (it.hasNext()) { Object selectRole = it.next(); if (selectRole.equals(Role.ROLE_ADMIN)) { role.add(Role.ROLE_ADMIN); role.add(Role.ROLE_USER); roleSelector.setValue(role); } } } }); // roleSelector is mandatory component roleSelector.addValidator( new Validator() { private static final long serialVersionUID = 1L; @Override public void validate(Object value) throws InvalidValueException { if (!"[]".equals(value.toString())) { return; } ex = new InvalidValueException("at least one role must be set"); throw ex; } }); // Layout with buttons Save and Cancel HorizontalLayout buttonBar = new HorizontalLayout(); // buttonBar.setMargin(true); // Save button Button createUser = new Button(); createUser.setCaption("Save"); createUser.setWidth("90px"); createUser.setImmediate(true); createUser.addClickListener( new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { String errorText = ""; // validation // User name, password,e-mail and roles should be filled // email should be in correct format try { userFullName.validate(); } catch (Validator.InvalidValueException e) { errorText = errorText + e.getMessage(); } try { userName.validate(); } catch (Validator.InvalidValueException e) { if (!errorText.equals("")) { errorText = errorText + ", " + e.getMessage(); } else { errorText = errorText + e.getMessage(); } } try { userEmail.validate(); } catch (Validator.InvalidValueException e) { if (!errorText.equals("")) { errorText = errorText + ", " + e.getMessage(); } else { errorText = errorText + e.getMessage(); } } try { roleSelector.validate(); } catch (Validator.InvalidValueException e) { if (!errorText.equals("")) { errorText = errorText + ", " + e.getMessage(); } else { errorText = errorText + e.getMessage(); } } if (!errorText.equals("")) { errorText = errorText + "."; Notification.show( "Failed to save settings. Reason:", errorText, Notification.Type.ERROR_MESSAGE); return; } // checking if the dialog was open from the User table // if no, create new user record if (newUser) { String userPassword; if (passwordConfim.getValue().equals(password.getValue())) { if (!passwordConfim.getValue().isEmpty()) { userPassword = password.getValue(); } else { userPassword = createPassword(); } } else { Notification.show( "Password confirmation is wrong", "The typed pasword is different than the retyped password", Notification.Type.ERROR_MESSAGE); return; } EmailAddress email = new EmailAddress(userEmail.getValue().trim()); user = userFacade.createUser(userName.getValue().trim(), userPassword, email); user.setFullName(userFullName.getValue().trim()); } else { user = selectUser; user.setFullName(userFullName.getValue().trim()); user.setUsername(userName.getValue().trim()); if (passChanged) { if (passwordConfim.getValue().equals(password.getValue())) { if (!passwordConfim.getValue().isEmpty()) { user.setPassword(password.getValue()); } else { user.setPassword(createPassword()); } } else { Notification.show( "Password confirmation is wrong", "The typed pasword is different than the retyped password", Notification.Type.ERROR_MESSAGE); return; } } EmailAddress email = new EmailAddress(userEmail.getValue().trim()); user.setEmail(email); } @SuppressWarnings("unchecked") Set<Object> selectedRoles = (Set<Object>) roleSelector.getValue(); Iterator<Object> it = selectedRoles.iterator(); roles = new HashSet<>(); while (it.hasNext()) { Object selectRole = it.next(); if (selectRole.equals(Role.ROLE_ADMIN)) { roles.add(Role.ROLE_ADMIN); } else { roles.add(Role.ROLE_USER); } } user.setRoles(roles); // store user record to DB userFacade.save(user); close(); } }); buttonBar.addComponent(createUser); Button cancelButton = new Button( "Cancel", new Button.ClickListener() { /** Closes Scheduling pipeline window */ private static final long serialVersionUID = 1L; @Override public void buttonClick(Button.ClickEvent event) { close(); } }); cancelButton.setWidth("90px"); buttonBar.addComponent(cancelButton); mainLayout.addComponent(userDetailsLayout); mainLayout.addComponent(roleSelector); mainLayout.addComponent(buttonBar); // mainLayout.setComponentAlignment(buttonBar, Alignment.MIDDLE_RIGHT); return mainLayout; }
// TODO: учеть версионность! void createGroupEditor(final long id, final Set<String> names) { final Organization org = AdminServiceProvider.get().findOrganizationById(id); final Set<String> all = AdminServiceProvider.get().getOrgGroupNames(); final VerticalLayout layout = new VerticalLayout(); layout.setSpacing(true); final TwinColSelect twin = new TwinColSelect(); twin.setSizeFull(); twin.setNullSelectionAllowed(true); twin.setLeftColumnCaption("Доступные"); twin.setRightColumnCaption("Отобранные для " + org.getName()); twin.setImmediate(true); for (final String name : all) { twin.addItem(name); if (names.contains(name)) { twin.select(name); } } layout.addComponent(twin); final HorizontalLayout h = new HorizontalLayout(); h.setSpacing(true); Button cancel = new Button( "Отменить", new Button.ClickListener() { private static final long serialVersionUID = -2885182304929510066L; @Override public void buttonClick(Button.ClickEvent event) { showOrganization(id); } }); cancel.setClickShortcut(KeyCode.ESCAPE, 0); Button ok = new Button( "Применить", new Button.ClickListener() { private static final long serialVersionUID = -3182280627040233669L; @Override public void buttonClick(Button.ClickEvent event) { AdminServiceProvider.get() .setOrgGroupNames( id, new TreeSet<String>((Collection<String>) twin.getValue())); showOrganization(id); } }); ok.setClickShortcut(KeyCode.O, ModifierKey.CTRL); h.addComponent(ok); h.addComponent(cancel); layout.addComponent(h); layout.setSizeFull(); panel.removeAllComponents(); panel.addComponent(layout); twin.focus(); }