Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFE: do you want my TextComponentPassword class? #2654

Closed
jsfan3 opened this issue Dec 29, 2018 · 1 comment

Comments

@jsfan3
Copy link
Contributor

commented Dec 29, 2018

First of all, thank you for the answer you gave me here: https://stackoverflow.com/questions/53964814/add-an-icon-inside-a-codename-one-textmodelayout/53967035#53967035

The workaround you suggested doesn't work because a NullPointerException. However, as you suggested, I extended the TextComponent class and it works!

I tested the following class in Javascript port, native Android app and native iPhone app. It seems ok, at least in my simple test cases. Can you please evaluate if do you want to add it to Codename One? In that case, I can submit a pull request.

package com.codename1.ui;

import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;

/**
 * TextComponent extended to automatically add mask/unmask password button near
 * the TextField; it acts like a normal TextComponent if the Constraint is not
 * TextArea.PASSWORD
 *
 * @author Francesco Galgani
 */
public class TextComponentPassword extends TextComponent {

    private Image unmask = FontImage.createMaterial(FontImage.MATERIAL_VISIBILITY, UIManager.getInstance().getComponentStyle("Label"));
    private Image mask = FontImage.createMaterial(FontImage.MATERIAL_VISIBILITY_OFF, UIManager.getInstance().getComponentStyle("Label"));
    private Button showPassword = new Button(unmask, "Label");
    private final TextField field = super.getField();

    public TextComponentPassword() {
        super();
        showPassword.addActionListener(e -> {
            if (field.getConstraint() == TextArea.PASSWORD) {
                field.setConstraint(TextField.ANY);
                showPassword.setIcon(mask);
            } else {
                field.setConstraint(TextField.PASSWORD);
                showPassword.setIcon(unmask);
            }
            if (field.isEditing()) {
                field.stopEditing();
                field.startEditingAsync();
            } else {
                field.getParent().revalidate();
            }
        });
    }

    @Override
    void constructUI() {
        super.constructUI();
        if (field.getConstraint() == TextArea.PASSWORD) {
            Container tfContainer = field.getParent();
            showPassword.remove(); // to solve a "java.lang.IllegalArgumentException - Component is already contained in Container" after a Form.showBack
            tfContainer.add(BorderLayout.EAST, showPassword);
        }
    }
}
@codenameone

This comment has been minimized.

Copy link
Owner

commented Dec 29, 2018

I'm not sure if this is the right way to add this to the API. I'll have to give this some thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.