Example usage for com.vaadin.ui TextField addFocusListener

List of usage examples for com.vaadin.ui TextField addFocusListener

Introduction

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

Prototype

@Override
public Registration addFocusListener(FocusListener listener) 

Source Link

Document

Adds a FocusListener to this component, which gets fired when this component receives keyboard focus.

Usage

From source file:dhbw.clippinggorilla.userinterface.windows.GroupProfileWindow.java

private Component getAddTagGroup(GroupInterestProfile profile, boolean included) {
    CssLayout newTagGroup = new CssLayout();
    newTagGroup.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);

    TextField textFieldTag = new TextField();
    textFieldTag.setWidth("325px");
    textFieldTag.setMaxLength(255);/*from w  ww  .j  a  va 2s .  co  m*/
    Language.setCustom(Word.NEW_TAG, s -> textFieldTag.setPlaceholder(s));

    Button buttonAddTag = new Button(VaadinIcons.PLUS);
    buttonAddTag.addStyleName(ValoTheme.BUTTON_PRIMARY);
    buttonAddTag.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);
    buttonAddTag.addClickListener(event -> {
        addRow(profile, included, textFieldTag.getValue(), true);
        profile.getTags().put(textFieldTag.getValue(), included);
        textFieldTag.clear();
    });
    textFieldTag.addFocusListener(f -> buttonAddTag.setClickShortcut(ShortcutAction.KeyCode.ENTER, null));
    textFieldTag.addBlurListener(f -> buttonAddTag.removeClickShortcut());

    newTagGroup.addComponents(textFieldTag, buttonAddTag);
    newTagGroup.setWidth("100%");
    return newTagGroup;
}

From source file:fr.amapj.view.views.advanced.devtools.DevToolsSelectionTable.java

License:Open Source License

private void addTest2() {
    // Titre/*w  ww . j a  va2s.  c  om*/
    setStepTitle("UNIQUEMENT DES TEXTFIELDS EDITABLES");

    final Table table = new Table("La table");

    // 
    table.addContainerProperty("col1", TextField.class, null);
    table.addContainerProperty("col2", TextField.class, null);

    // Ligne 1
    TextField tf1 = new TextField();
    tf1.setValue("Canopus");
    tf1.addFocusListener(e -> table.select(1)); // IMPORTANT !! 

    TextField tf2 = new TextField();
    tf2.setValue("0.75");
    tf2.addFocusListener(e -> table.select(1)); // IMPORTANT !!

    table.addItem(new Object[] { tf1, tf2 }, 1);

    // Ligne 2
    tf1 = new TextField();
    tf1.setValue("Actarus");
    tf1.addFocusListener(e -> table.select(2)); // IMPORTANT !!

    tf2 = new TextField();
    tf2.setValue("0.85");
    tf2.addFocusListener(e -> table.select(2)); // IMPORTANT !!

    table.addItem(new Object[] { tf1, tf2 }, 2);

    // Ligne 3
    tf1 = new TextField();
    tf1.setValue("Venus");
    tf1.addFocusListener(e -> table.select(3)); // IMPORTANT !!

    tf2 = new TextField();
    tf2.setValue("1.45");
    tf2.addFocusListener(e -> table.select(3)); // IMPORTANT !!

    table.addItem(new Object[] { tf1, tf2 }, 3);

    // Show exactly the currently contained rows (items)
    table.setPageLength(table.size());

    // Allow selecting items from the table.
    table.setSelectable(true);
    table.setSortEnabled(false);

    // Send changes in selection immediately to server.
    table.setImmediate(true);

    // Shows feedback from selection.
    final Label current = new Label("Selected: -");

    // Handle selection change.
    table.addValueChangeListener(new Property.ValueChangeListener() {
        public void valueChange(ValueChangeEvent event) {
            current.setValue("Selected: " + table.getValue());
        }
    });

    form.addComponent(table);
    form.addComponent(current);

    String content = "Dans ce cas, tout fonctionne bien, on peut bien selectionner la ligne comme on veut, mais il faut avoir ajouter un listener sur chaque textfield ";
    addLabel(content, ContentMode.HTML);

}

From source file:fr.amapj.view.views.advanced.devtools.DevToolsSelectionTable.java

License:Open Source License

private void addTest3() {
    // Titre//  w  w w. j ava  2 s .  co  m
    setStepTitle("UNIQUEMENT DES TEXTFIELDS ENABLE = FALSE");

    final Table table = new Table("La table");

    // 
    table.addContainerProperty("col1", TextField.class, null);
    table.addContainerProperty("col2", TextField.class, null);

    // Ligne 1
    TextField tf1 = new TextField();
    tf1.setValue("Canopus");
    tf1.addFocusListener(e -> table.select(1)); // IMPORTANT !! 
    tf1.setEnabled(false);

    TextField tf2 = new TextField();
    tf2.setValue("0.75");
    tf2.addFocusListener(e -> table.select(1)); // IMPORTANT !!
    tf2.setEnabled(false);

    table.addItem(new Object[] { tf1, tf2 }, 1);

    // Ligne 2
    tf1 = new TextField();
    tf1.setValue("Actarus");
    tf1.addFocusListener(e -> table.select(2)); // IMPORTANT !!
    tf1.setEnabled(false);

    tf2 = new TextField();
    tf2.setValue("0.85");
    tf2.addFocusListener(e -> table.select(2)); // IMPORTANT !!
    tf2.setEnabled(false);

    table.addItem(new Object[] { tf1, tf2 }, 2);

    // Ligne 3
    tf1 = new TextField();
    tf1.setValue("Venus");
    tf1.addFocusListener(e -> table.select(3)); // IMPORTANT !!
    tf1.setEnabled(false);

    tf2 = new TextField();
    tf2.setValue("1.45");
    tf2.addFocusListener(e -> table.select(3)); // IMPORTANT !!
    tf2.setEnabled(false);

    table.addItem(new Object[] { tf1, tf2 }, 3);

    // Show exactly the currently contained rows (items)
    table.setPageLength(table.size());

    // Allow selecting items from the table.
    table.setSelectable(true);
    table.setSortEnabled(false);

    // Send changes in selection immediately to server.
    table.setImmediate(true);

    // Shows feedback from selection.
    final Label current = new Label("Selected: -");

    // Handle selection change.
    table.addValueChangeListener(new Property.ValueChangeListener() {
        public void valueChange(ValueChangeEvent event) {
            current.setValue("Selected: " + table.getValue());
        }
    });

    form.addComponent(table);
    form.addComponent(current);

    String content = "Dans ce cas, impossible de le faire fonctionner. On n'arrive pas  selctionner les lignes. Le seul moyen est de cliquer juste au milieu entre les 2 colonnes, mais pas pratique du tout  ";
    addLabel(content, ContentMode.HTML);

}

From source file:info.magnolia.configuration.app.problem.toolbar.ProblemToolbarViewImpl.java

License:Open Source License

private TextField buildSearchField() {
    final TextField field = new TextField();
    ShortcutProtector.extend(field, Arrays.asList(ShortcutAction.KeyCode.ENTER));
    final String inputPrompt = i18n.translate("toolbar.search.prompt");

    field.setInputPrompt(inputPrompt);/*  w w w . j  a  v a2 s  .co m*/
    field.setSizeUndefined();
    field.addStyleName("searchfield");

    // TextField has to be immediate to fire value changes when pressing Enter, avoiding ShortcutListener overkill.
    field.setImmediate(true);
    field.addValueChangeListener(searchFieldListener);

    field.addFocusListener(new FieldEvents.FocusListener() {
        @Override
        public void focus(FieldEvents.FocusEvent event) {
            // put the cursor at the end of the field
            TextField tf = (TextField) event.getSource();
            tf.setCursorPosition(tf.getValue().length());
        }
    });

    // No blur handler.
    return field;
}

From source file:org.lucidj.vaadinui.Login.java

License:Apache License

@Override // LoginForm
protected Component createContent(TextField userNameField, PasswordField passwordField, Button loginButton) {
    // Save the predefined components
    this.userNameField = userNameField;
    this.passwordField = passwordField;
    this.loginButton = loginButton;

    // Make LoginForm container full-screen
    setSizeFull();//  w ww  . ja v a2  s . c om

    VerticalLayout layout = new VerticalLayout();
    layout.setSizeFull();
    layout.addStyleName("login-wallpaper");

    final VerticalLayout loginPanel = new VerticalLayout();
    loginPanel.setSizeUndefined();
    loginPanel.setMargin(true);
    loginPanel.setSpacing(true);
    Responsive.makeResponsive(loginPanel);
    loginPanel.addStyleName("card");

    //--------
    // HEADER
    //--------

    final HorizontalLayout labels = new HorizontalLayout();
    labels.setWidth("100%");

    final Label title = new Label("<h3><strong>LucidJ</strong> Console</h3>", ContentMode.HTML);
    labels.addComponent(title);
    labels.setExpandRatio(title, 1);

    loginPanel.addComponent(labels);

    //--------
    // FIELDS
    //--------

    HorizontalLayout fields = new HorizontalLayout();
    fields.setSpacing(true);
    fields.addStyleName("fields");

    userNameField.setImmediate(true);
    userNameField.setTextChangeEventMode(AbstractTextField.TextChangeEventMode.EAGER);
    final ShortcutListener username_enter_listener = new ShortcutListener("Next field (Tab)",
            ShortcutAction.KeyCode.ENTER, null) {
        @Override
        public void handleAction(Object o, Object o1) {
            passwordField.setValue("");
            passwordField.focus();
        }
    };
    userNameField.addTextChangeListener(new FieldEvents.TextChangeListener() {
        @Override
        public void textChange(FieldEvents.TextChangeEvent textChangeEvent) {
            show_default_message();

            int new_username_length = textChangeEvent.getText().length();

            // Check for autofill
            if (userNameField.isEmpty() && new_username_length > 1 && !userNameField_filling) {
                // This is autofill
                userNameField.removeShortcutListener(username_enter_listener);
                userNameField.setCursorPosition(new_username_length);
                userNameField.setSelectionRange(0, new_username_length);
            } else {
                userNameField_filling = true;
                passwordField.setValue("");
                userNameField.addShortcutListener(username_enter_listener);
            }
        }
    });
    userNameField.addFocusListener(new FieldEvents.FocusListener() {
        @Override
        public void focus(FieldEvents.FocusEvent focusEvent) {
            // Cursor on username, Enter jump to password
            loginButton.removeClickShortcut();
            userNameField.addShortcutListener(username_enter_listener);
        }
    });
    userNameField.addBlurListener(new FieldEvents.BlurListener() {
        @Override
        public void blur(FieldEvents.BlurEvent blurEvent) {
            // Cursor on password or elsewhere, enter submits
            userNameField.removeShortcutListener(username_enter_listener);
            loginButton.setClickShortcut(ShortcutAction.KeyCode.ENTER);
        }
    });

    passwordField.setImmediate(true);
    passwordField.setTextChangeEventMode(AbstractTextField.TextChangeEventMode.EAGER);
    passwordField.addTextChangeListener(new FieldEvents.TextChangeListener() {
        @Override
        public void textChange(FieldEvents.TextChangeEvent textChangeEvent) {
            show_default_message();
        }
    });

    loginButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
    loginButton.setDisableOnClick(true);

    fields.addComponents(userNameField, passwordField, loginButton);
    fields.setComponentAlignment(loginButton, Alignment.BOTTOM_LEFT);

    loginPanel.addComponent(fields);

    //--------
    // FOOTER
    //--------

    loginPanel.addComponent(new CheckBox("Remember me", true));

    loginPanel.addComponent(message_label);
    show_default_message();

    layout.addComponent(loginPanel);
    layout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
    return (layout);
}

From source file:org.qa82.analyzer.ui.OnEnterKeyHandler.java

License:Open Source License

public void installOn(final TextField component) {
    component.addFocusListener(new FieldEvents.FocusListener() {

        /**/*from www .  j a v  a 2  s  .c om*/
        * 
        */
        private static final long serialVersionUID = 1L;

        @Override
        public void focus(FieldEvents.FocusEvent event) {
            component.addShortcutListener(enterShortCut);
        }

    });

    component.addBlurListener(new FieldEvents.BlurListener() {

        /**
        * 
        */
        private static final long serialVersionUID = 1L;

        @Override
        public void blur(FieldEvents.BlurEvent event) {
            component.removeShortcutListener(enterShortCut);
        }

    });
}

From source file:org.vaadin.tori.view.edit.EditViewImpl.java

License:Apache License

private Component buildReplacements() {
    VerticalLayout result = new VerticalLayout();
    result.addComponent(getSubTitle("Define post body regex-patterns/replacements to be "
            + "applied whenever posts are being displayed/previewed."));

    replacementsTable = new Table();
    replacementsTable.setWidth("100%");
    replacementsTable.setHeight("10em");
    replacementsTable.setSelectable(true);
    replacementsTable.setImmediate(true);
    replacementsTable.setEditable(true);
    replacementsTable.setTableFieldFactory(new TableFieldFactory() {

        @Override//from w  w  w.  ja  v  a2s .  c  om
        public Field<?> createField(final Container container, final Object itemId, final Object propertyId,
                final Component uiContext) {
            final TextField textField = new TextField();
            textField.setWidth(100.0f, Unit.PERCENTAGE);
            textField.addFocusListener(new FocusListener() {
                @Override
                public void focus(final FocusEvent event) {
                    replacementsTable.setValue(itemId);
                }
            });
            return textField;
        }
    });

    final Button removeButton = ComponentUtil.getSecondaryButton("Remove", new Button.ClickListener() {

        @Override
        public void buttonClick(final ClickEvent event) {
            replacementsTable.removeItem(replacementsTable.getValue());
        }
    });

    replacementsTable.addValueChangeListener(new Property.ValueChangeListener() {
        @Override
        public void valueChange(final ValueChangeEvent event) {
            removeButton.setEnabled(event.getProperty().getValue() != null);
        }
    });

    replacementsTable.addContainerProperty("regex", String.class, "", "Regex", null, null);
    replacementsTable.setSortContainerPropertyId("regex");
    replacementsTable.addContainerProperty("replacement", String.class, "", "Replacement", null, null);
    result.addComponent(replacementsTable);

    removeButton.setEnabled(false);

    Button newButton = ComponentUtil.getSecondaryButton("New", new Button.ClickListener() {
        @Override
        public void buttonClick(final ClickEvent event) {
            final Object itemId = replacementsTable.addItem();
            replacementsTable.setValue(itemId);
        }
    });

    final HorizontalLayout buttonsLayout = new HorizontalLayout();
    buttonsLayout.setSpacing(true);
    buttonsLayout.addComponent(removeButton);
    buttonsLayout.addComponent(newButton);
    result.addComponent(buttonsLayout);

    return result;
}

From source file:sph.vaadin.ui.ComponentFactory.java

License:Apache License

/**
 * Creates a new TextField component./*  www.j  a v a2 s  . c  o  m*/
 * 
 * @param  caption component's caption String. Caption is the visible name of the component.
 * @param  prompt component's prompt String.
 * @param  enabled a boolean value specifying if the component should be enabled or not.
 * @return the created component.
 */
public static TextField createTextField(String caption, String prompt, boolean enabled) {
    TextField tf = new TextField();
    if (caption != null) {
        tf.setCaption(caption);
    }
    if (prompt != null) {
        tf.setInputPrompt(prompt);
    }
    tf.setEnabled(enabled);
    tf.addStyleName(SPH_Theme.TEXTFIELD_SMALL);
    tf.addValueChangeListener(new TextFieldValidator());
    tf.addFocusListener(new TextSelector());
    tf.setImmediate(true);
    return tf;
}

From source file:zm.hashcode.mshengu.app.util.validation.OnSubmitValidationHelper.java

public void doValidation() {
    TextField textField = new TextField();
    TextArea textArea = new TextArea();
    DateField dateField = new DateField();
    ComboBox comboBox = new ComboBox();
    for (Field o : fields) {

        String currentMessage = "";
        try {//from w  ww . j  a  v  a  2  s  .  c o  m
            if (o instanceof TextField) {
                textField = (TextField) o;
                textField.validate();
            } else if (o instanceof TextArea) {
                textArea = (TextArea) o;
                textArea.validate();
            } else if (o instanceof DateField) {
                dateField = (DateField) o;
                dateField.validate();
            } else if (o instanceof ComboBox) {
                comboBox = (ComboBox) o;
                comboBox.validate();
            }

        } catch (Validator.InvalidValueException x) { //works with vaadin required
            currentMessage = x.getMessage();
            if (o instanceof TextField) {
                textField.setStyleName("invalid");
            } else if (o instanceof TextArea) {
                textArea.setStyleName("invalid");
            } else if (o instanceof DateField) {
                dateField.setStyleName("invalid");
            } else if (o instanceof ComboBox) {
                comboBox.setStyleName("invalid");
            }

        } finally {
            if (o instanceof TextField) {
                textField.addFocusListener(
                        new LabelErrorMessageManipulator(textField, errorLabel, currentMessage)); //custom focus handler for displaying error message on a labe when you focus on an errored Textfield                       
            } else if (o instanceof TextArea) {
                textArea.addFocusListener(
                        new LabelErrorMessageManipulator(textArea, errorLabel, currentMessage)); //custom focus handler for displaying error message on a labe when you focus on an errored Textfield
            } else if (o instanceof DateField) {
                dateField.addFocusListener(
                        new LabelErrorMessageManipulator(dateField, errorLabel, currentMessage)); //custom focus handler for displaying error message on a labe when you focus on an errored Textfield                
            } else if (o instanceof ComboBox) {
                comboBox.addFocusListener(
                        new LabelErrorMessageManipulator(comboBox, errorLabel, currentMessage)); //custom focus handler for displaying error message on a labe when you focus on an errored Textfield
            }
        }

        if (o instanceof TextField) {
            textField.addBlurListener(new LabelErrorMessageManipulator(textField, errorLabel, currentMessage)); //custom blur handler for displaying error message on a labe when you blur on an errored Textfield                       
        } else if (o instanceof TextArea) {
            textArea.addBlurListener(new LabelErrorMessageManipulator(textArea, errorLabel, currentMessage)); //custom blur handler for displaying error message on a labe when you blur on an errored Textfield
        } else if (o instanceof DateField) {
            dateField.addBlurListener(new LabelErrorMessageManipulator(dateField, errorLabel, currentMessage)); //custom blur handler for displaying error message on a labe when you blur on an errored Textfield
        } else if (o instanceof ComboBox) {
            comboBox.addBlurListener(new LabelErrorMessageManipulator(comboBox, errorLabel, currentMessage)); //custom blur handler for displaying error message on a labe when you blur on an errored Textfield
        }
    }

}