Search in sources :

Example 71 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class SignIn method showGoogleUser.

private void showGoogleUser(String token) {
    ConnectionRequest req = new ConnectionRequest();
    req.addRequestHeader("Authorization", "Bearer " + token);
    req.setUrl("https://www.googleapis.com/plus/v1/people/me");
    req.setPost(false);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    d.dispose();
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("displayName");
    Map im = (Map) map.get("image");
    String url = (String) im.get("url");
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), url);
    userForm.show();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 72 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class SignIn method start.

public void start() {
    // new SignInFormGB(theme).show();
    // if (true) return;
    loginForm = new Form("Sign in Demo");
    loginForm.setLayout(new BorderLayout());
    Container center = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    center.setUIID("ContainerWithPadding");
    Image logo = theme.getImage("CodenameOne.png");
    Label l = new Label(logo);
    Container flow = new Container(new FlowLayout(Component.CENTER));
    flow.addComponent(l);
    center.addComponent(flow);
    final TextField username = new TextField();
    username.setHint("Username");
    final TextField pass = new TextField();
    pass.setHint("Password");
    pass.setConstraint(TextField.PASSWORD);
    center.addComponent(username);
    center.addComponent(pass);
    Button signIn = new Button("Sign In");
    signIn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (username.getText().length() == 0 || pass.getText().length() == 0) {
                return;
            }
            UserForm userForm = new UserForm(username.getText(), (EncodedImage) theme.getImage("user.png"), null);
            userForm.show();
        }
    });
    center.addComponent(signIn);
    loginForm.addComponent(BorderLayout.CENTER, center);
    Container bottom = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    Button loginWFace = new Button(theme.getImage("signin_facebook.png"));
    loginWFace.setUIID("LoginButton");
    loginWFace.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            // create your own app identity on facebook follow the guide here:
            // facebook-login.html
            String clientId = "1171134366245722";
            String redirectURI = "http://www.codenameone.com/";
            String clientSecret = "";
            if (clientSecret.length() == 0) {
                System.err.println("create your own facebook app follow the guide here:");
                System.err.println("http://www.codenameone.com/facebook-login.html");
                return;
            }
            Login fb = FacebookConnect.getInstance();
            fb.setClientId(clientId);
            fb.setRedirectURI(redirectURI);
            fb.setClientSecret(clientSecret);
            login = fb;
            fb.setCallback(new LoginListener(LoginListener.FACEBOOK));
            if (!fb.isUserLoggedIn()) {
                fb.doLogin();
            } else {
                showFacebookUser(fb.getAccessToken().getToken());
            }
        }
    });
    Button loginWG = new Button(theme.getImage("signin_google.png"));
    loginWG.setUIID("LoginButton");
    loginWG.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            SignIn.this.doFirebase();
            // create your own google project follow the guide here:
            // http://www.codenameone.com/google-login.html
            String clientId = "555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com";
            String redirectURI = "https://www.codenameone.com/oauth2callback";
            String clientSecret = "650YqplrnAI0KXb9LMUnVNnx";
            if (clientSecret.length() == 0) {
                System.err.println("create your own google project follow the guide here:");
                System.err.println("http://www.codenameone.com/google-login.html");
                return;
            }
            Login gc = GoogleConnect.getInstance();
            gc.setClientId(clientId);
            gc.setRedirectURI(redirectURI);
            gc.setClientSecret(clientSecret);
            gc.setScope("https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file");
            login = gc;
            gc.setCallback(new LoginListener(LoginListener.GOOGLE));
            if (!gc.isUserLoggedIn()) {
                gc.doLogin();
            } else {
                showGoogleUser(gc.getAccessToken().getToken());
            }
        }
    });
    bottom.addComponent(loginWFace);
    bottom.addComponent(loginWG);
    loginForm.addComponent(BorderLayout.SOUTH, bottom);
    loginForm.show();
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) BoxLayout(com.codename1.ui.layouts.BoxLayout) Label(com.codename1.ui.Label) Login(com.codename1.social.Login) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) TextField(com.codename1.ui.TextField)

Example 73 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class SignIn method showFacebookUser.

private void showFacebookUser(String token) {
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/v2.3/me");
    req.addArgumentNoEncoding("access_token", token);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("name");
    d.dispose();
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), "https://graph.facebook.com/v2.3/me/picture?access_token=" + token);
    userForm.show();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 74 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class HTMLComponent method handleInput.

/**
 * Handles the INPUT tag
 *
 * @param element The input element
 * @param align The current aligment
 */
private void handleInput(HTMLElement element, int align) {
    String type = element.getAttributeById(HTMLElement.ATTR_TYPE);
    if (type == null) {
        return;
    }
    int typeID = INPUT_TYPES.indexOf(type.toLowerCase());
    if (typeID == -1) {
        if (htmlCallback != null) {
            if (!htmlCallback.parsingError(HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, element.getTagName(), element.getAttributeName(new Integer(HTMLElement.ATTR_TYPE)), type, "Unsupported input type '" + type + "'. Supported types: text, password, checkbox, radio, submit, reset, hidden, image")) {
                cancel();
            }
        }
        return;
    }
    String name = element.getAttributeById(HTMLElement.ATTR_NAME);
    String id = element.getAttributeById(HTMLElement.ATTR_ID);
    String value = element.getAttributeById(HTMLElement.ATTR_VALUE);
    if (value == null) {
        value = "";
    }
    Component cmp = null;
    switch(typeID) {
        case INPUT_CHECKBOX:
            CheckBox cb = new CheckBox();
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                cb.setSelected(true);
            }
            cmp = cb;
            if (curForm != null) {
                curForm.addCheckBox(name, cb, value);
            }
            break;
        case INPUT_HIDDEN:
            if (curForm != null) {
                curForm.addInput(name, value, null);
            }
            break;
        case INPUT_EMAIL:
        case INPUT_TEXT:
        case INPUT_PASSWORD:
            TextField tf = new TextField(value);
            tf.setLeftAndRightEditingTrigger(false);
            if (typeID == INPUT_PASSWORD) {
                tf.setConstraint(TextField.PASSWORD);
            }
            if (typeID == INPUT_EMAIL) {
                tf.setConstraint(TextField.EMAILADDR);
            }
            if (SUPPORT_INPUT_FORMAT) {
                HTMLInputFormat inputFormat = HTMLInputFormat.getInputFormat(element.getAttributeById(HTMLElement.ATTR_FORMAT));
                if (inputFormat != null) {
                    tf = (TextField) inputFormat.applyConstraints(tf);
                    if (curForm != null) {
                        curForm.setInputFormat(tf, inputFormat);
                    }
                }
                String emptyOk = element.getAttributeById(HTMLElement.ATTR_EMPTYOK);
                if ((emptyOk != null) && (curForm != null)) {
                    if (emptyOk.equalsIgnoreCase("true")) {
                        curForm.setEmptyOK(tf, true);
                    } else if (emptyOk.equalsIgnoreCase("false")) {
                        curForm.setEmptyOK(tf, false);
                    }
                }
            }
            int size = getInt(element.getAttributeById(HTMLElement.ATTR_SIZE));
            int maxlen = getInt(element.getAttributeById(HTMLElement.ATTR_MAXLENGTH));
            if (size == 0) {
                size = DEFAULT_TEXTFIELD_SIZE;
            }
            if (maxlen != 0) {
                tf.setMaxSize(maxlen);
                if (size > maxlen) {
                    size = maxlen;
                }
            }
            tf.setPreferredW(tf.getStyle().getFont().stringWidth("W") * size);
            tf.getSelectedStyle().setFont(font.getFont());
            tf.getUnselectedStyle().setFont(font.getFont());
            if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_READONLY) != null)) {
                tf.setEditable(false);
            }
            cmp = tf;
            if (curForm != null) {
                curForm.addInput(name, cmp, value);
                textfieldsToForms.put(tf, curForm);
            }
            break;
        case INPUT_RADIO:
            RadioButton rb = new RadioButton(" ");
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                rb.setSelected(true);
            }
            cmp = rb;
            if (curForm != null) {
                curForm.addRadioButton(name, rb, value);
            }
            break;
        case INPUT_RESET:
            Command resetCmd = null;
            if (curForm != null) {
                resetCmd = curForm.createResetCommand(value);
            }
            if (resetCmd == null) {
                // dummy command - no form so it won't do anything
                resetCmd = new Command(getUIManager().localize("html.reset", HTMLForm.DEFAULT_RESET_TEXT));
            }
            Button resetButton = new Button(resetCmd);
            cmp = resetButton;
            break;
        case INPUT_BUTTON:
        case INPUT_SUBMIT:
            Command submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            if (submitCmd == null) {
                // dummy command - no form so it won't do anything
                submitCmd = new Command(value.equals("") ? value = getUIManager().localize("html.submit", HTMLForm.DEFAULT_SUBMIT_TEXT) : value);
            }
            Button submitButton = new Button(submitCmd);
            cmp = submitButton;
            break;
        case // Image submit is not officially supported in XHTML-MP 1.0 but was added anyway, but pixel data submission is not supported (i.e. name.x=xx&name.y=yy)
        INPUT_IMAGE:
            submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            handleImage(element, align, submitCmd);
            break;
    }
    if (cmp != null) {
        if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_DISABLED) != null)) {
            cmp.setEnabled(false);
        }
        String aKey = element.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
        if ((aKey != null) && (aKey.length() == 1)) {
            // accessKeys.put(new Integer(aKey.charAt(0)), cmp);
            addAccessKey(aKey.charAt(0), cmp, false);
        }
        if (eventsListener != null) {
            eventsListener.registerComponent(cmp, element);
        }
        // Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
        element.setAssociatedComponents(cmp);
        if ((curForm != null) && (curForm.action == null)) {
            // Form that submits to a forbidden link
            cmp.setEnabled(false);
        } else if (firstFocusable == null) {
            firstFocusable = cmp;
        }
        if (id != null) {
            inputFields.put(id, cmp);
        }
    }
    addCmp(cmp, align);
}
Also used : Command(com.codename1.ui.Command) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) CheckBox(com.codename1.ui.CheckBox) TextField(com.codename1.ui.TextField) RadioButton(com.codename1.ui.RadioButton) Component(com.codename1.ui.Component)

Example 75 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class HTMLComponent method addAccessKey.

/**
 * Adds the given access key to make it focus on the given component
 *
 * @param accessKey The accesskey key code
 * @param cmp The component that should be focused when the access key is pressed
 * @param override If true, cancel any previous accesskey associated with this component (Relevant for CSS -wap-accesskey)
 */
void addAccessKey(int accessKey, Component cmp, boolean override) {
    if ((override) && (accessKeys.contains(cmp))) {
        Hashtable newAccessKeys = new Hashtable();
        for (Enumeration e = accessKeys.keys(); e.hasMoreElements(); ) {
            Object key = e.nextElement();
            Component c = (Component) accessKeys.get(key);
            if (c != cmp) {
                newAccessKeys.put(key, c);
            }
        }
        accessKeys = newAccessKeys;
    }
    accessKeys.put(new Integer(accessKey), cmp);
    Form form = getComponentForm();
    if (form != null) {
        form.addKeyListener(accessKey, this);
    }
}
Also used : Enumeration(java.util.Enumeration) Form(com.codename1.ui.Form) Hashtable(java.util.Hashtable) Component(com.codename1.ui.Component)

Aggregations

Form (com.codename1.ui.Form)90 ActionEvent (com.codename1.ui.events.ActionEvent)41 Component (com.codename1.ui.Component)38 BorderLayout (com.codename1.ui.layouts.BorderLayout)38 Container (com.codename1.ui.Container)26 ActionListener (com.codename1.ui.events.ActionListener)25 Dialog (com.codename1.ui.Dialog)21 Command (com.codename1.ui.Command)19 Hashtable (java.util.Hashtable)17 Label (com.codename1.ui.Label)14 Style (com.codename1.ui.plaf.Style)14 IOException (java.io.IOException)14 TextArea (com.codename1.ui.TextArea)13 Vector (java.util.Vector)12 Button (com.codename1.ui.Button)11 Graphics (com.codename1.ui.Graphics)9 RadioButton (com.codename1.ui.RadioButton)9 Animation (com.codename1.ui.animations.Animation)9 Image (com.codename1.ui.Image)8 UIManager (com.codename1.ui.plaf.UIManager)8