Search in sources :

Example 76 with Image

use of com.codename1.ui.Image 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 77 with Image

use of com.codename1.ui.Image 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 78 with Image

use of com.codename1.ui.Image 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 79 with Image

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

the class CSSBgPainter method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g, Rectangle rect) {
    Style s = parent.getStyle();
    int x = rect.getX();
    int y = rect.getY();
    int width = rect.getSize().getWidth();
    int height = rect.getSize().getHeight();
    if (width <= 0 || height <= 0) {
        return;
    }
    g.setColor(s.getBgColor());
    g.fillRect(x, y, width, height, s.getBgTransparency());
    Image bgImage = s.getBgImage();
    if (bgImage == null) {
        return;
    }
    if (fixedX) {
        if (scrollableParent == null) {
            scrollableParent = getScrollableParent(parent);
        }
        if (scrollableParent != null) {
            x += scrollableParent.getScrollX();
            y += scrollableParent.getScrollY();
            width = scrollableParent.getWidth();
            height = scrollableParent.getHeight();
        }
    }
    int iW = bgImage.getWidth();
    int iH = bgImage.getHeight();
    int offsetX = horizPos;
    int offsetY = vertPos;
    if (horizIsPercentage) {
        offsetX = (width - iW) * offsetX / 100;
    }
    if (vertIsPercentage) {
        offsetY = (height - iH) * offsetY / 100;
    }
    switch(s.getBackgroundType()) {
        case 0:
            g.drawImage(s.getBgImage(), x + offsetX, y + offsetY);
            return;
        case Style.BACKGROUND_IMAGE_TILE_BOTH:
            for (int xPos = getTiledPosition(offsetX, iW); xPos <= width; xPos += iW) {
                for (int yPos = getTiledPosition(offsetY, iH); yPos <= height; yPos += iH) {
                    g.drawImage(s.getBgImage(), x + xPos, y + yPos);
                }
            }
            return;
        case Style.BACKGROUND_IMAGE_TILE_HORIZONTAL:
            for (int xPos = getTiledPosition(offsetX, iW); xPos <= width; xPos += iW) {
                g.drawImage(s.getBgImage(), x + xPos, y + offsetY);
            }
            return;
        case Style.BACKGROUND_IMAGE_TILE_VERTICAL:
            for (int yPos = getTiledPosition(offsetY, iH); yPos <= height; yPos += iH) {
                g.drawImage(s.getBgImage(), x + offsetX, y + yPos);
            }
            return;
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Image(com.codename1.ui.Image)

Example 80 with Image

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

the class CSSEngine method evalContentExpression.

/**
 * Evaluates a CSS content property expression and returns the matching label component
 *
 * @param htmlC The HTMLComponent
 * @param exp The expression to evaluate
 * @param element The element this content property
 * @param selector The CSS selector that includes the content property (mainly for error messages)
 * @return A label representing the evaluated expression or null if not found
 */
private Label evalContentExpression(HTMLComponent htmlC, String exp, HTMLElement element, CSSElement selector) {
    if (exp.length() != 0) {
        if (exp.startsWith("counter(")) {
            exp = exp.substring(8);
            int index = exp.indexOf(")");
            if (index != -1) {
                return new Label("" + htmlC.getCounterValue(exp.substring(0, index)));
            }
        } else if (exp.startsWith("attr(")) {
            exp = exp.substring(5);
            int index = exp.indexOf(")");
            if (index != -1) {
                String attr = exp.substring(0, index);
                String attrValue = element.getAttribute(attr);
                return new Label(attrValue == null ? "" : attrValue);
            }
        } else if (exp.equals("open-quote")) {
            return getQuote(true);
        } else if (exp.equals("close-quote")) {
            return getQuote(false);
        } else if (exp.startsWith("url(")) {
            String url = getCSSUrl(exp);
            Label imgLabel = new Label();
            if (htmlC.showImages) {
                if (htmlC.getDocumentInfo() != null) {
                    htmlC.getThreadQueue().add(imgLabel, htmlC.convertURL(url));
                } else {
                    if (DocumentInfo.isAbsoluteURL(url)) {
                        htmlC.getThreadQueue().add(imgLabel, url);
                    } else {
                        if (htmlC.getHTMLCallback() != null) {
                            htmlC.getHTMLCallback().parsingError(HTMLCallback.ERROR_NO_BASE_URL, selector.getTagName(), selector.getAttributeName(new Integer(CSSElement.CSS_CONTENT)), url, "Ignoring image file referred in a CSS file/segment (" + url + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                        }
                    }
                }
            }
            return imgLabel;
        }
    }
    return null;
}
Also used : Label(com.codename1.ui.Label)

Aggregations

Image (com.codename1.ui.Image)82 EncodedImage (com.codename1.ui.EncodedImage)46 IOException (java.io.IOException)29 Hashtable (java.util.Hashtable)19 AnimationObject (com.codename1.ui.animations.AnimationObject)18 BufferedImage (java.awt.image.BufferedImage)18 Style (com.codename1.ui.plaf.Style)16 Form (com.codename1.ui.Form)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 Dimension (com.codename1.ui.geom.Dimension)14 FontImage (com.codename1.ui.FontImage)13 BorderLayout (com.codename1.ui.layouts.BorderLayout)13 Timeline (com.codename1.ui.animations.Timeline)12 ActionListener (com.codename1.ui.events.ActionListener)12 Border (com.codename1.ui.plaf.Border)12 Container (com.codename1.ui.Container)11 InputStream (java.io.InputStream)11 Graphics (com.codename1.ui.Graphics)10 Label (com.codename1.ui.Label)10 Button (com.codename1.ui.Button)9