Search in sources :

Example 41 with ActionEvent

use of com.codename1.ui.events.ActionEvent 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 42 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class Toolbar method bindScrollListener.

private void bindScrollListener(boolean bind) {
    final Form f = getComponentForm();
    if (f != null) {
        final Container actualPane = f.getActualPane();
        final Container contentPane = f.getContentPane();
        if (bind) {
            initVars(actualPane);
            scrollListener = new ScrollListener() {

                public void scrollChanged(int scrollX, int scrollY, int oldscrollX, int oldscrollY) {
                    int diff = scrollY - oldscrollY;
                    int toolbarNewY = getY() - diff;
                    if (scrollY < 0 || Math.abs(toolbarNewY) < 2) {
                        return;
                    }
                    toolbarNewY = Math.max(toolbarNewY, -getHeight());
                    toolbarNewY = Math.min(toolbarNewY, initialY);
                    if (toolbarNewY != getY()) {
                        setY(toolbarNewY);
                        if (!layered) {
                            actualPane.setY(actualPaneInitialY + toolbarNewY);
                            actualPane.setHeight(actualPaneInitialH + getHeight() - toolbarNewY);
                            actualPane.doLayout();
                        }
                        f.repaint();
                    }
                }
            };
            contentPane.addScrollListener(scrollListener);
            releasedListener = new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    if (getY() + getHeight() / 2 > 0) {
                        showToolbar();
                    } else {
                        hideToolbar();
                    }
                    f.repaint();
                }
            };
            contentPane.addPointerReleasedListener(releasedListener);
        } else {
            if (scrollListener != null) {
                contentPane.removeScrollListener(scrollListener);
                contentPane.removePointerReleasedListener(releasedListener);
            }
        }
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) ScrollListener(com.codename1.ui.events.ScrollListener)

Example 43 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class SideMenuBar method createMenu.

private Form createMenu(final String placement) {
    final Form m = new Form() {

        private boolean pressedInRightPanel;

        private boolean manualMotionLock;

        protected boolean shouldSendPointerReleaseToOtherForm() {
            return true;
        }

        void actionCommandImpl(Command cmd, ActionEvent ev) {
            if (cmd instanceof SideMenuBar.CommandWrapper) {
                cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
                ev = new ActionEvent(cmd, ActionEvent.Type.Command);
            }
            final Command c = cmd;
            final ActionEvent e = ev;
            Display.getInstance().scheduleBackgroundTask(new Runnable() {

                public void run() {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (Display.getInstance().getCurrent() != parent) {
                                try {
                                    Thread.sleep(40);
                                } catch (Exception ex) {
                                }
                            }
                        }
                    });
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            parent.actionCommandImpl(c, e);
                        }
                    });
                }
            });
        }

        protected void sizeChanged(int w, int h) {
            Style formStyle = getStyle();
            int width = w - (formStyle.getHorizontalMargins());
            parent.sizeChangedInternal(w, h);
            // close the menu
            if (getWidth() != width) {
                closeMenu();
            }
            super.sizeChanged(w, h);
        }

        public void pointerPressed(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerPressed(x, y);
            if (rightPanel.contains(x, y)) {
                pressedInRightPanel = true;
            }
        }

        public void pointerDragged(int[] x, int[] y) {
            if (manualMotionLock) {
                return;
            }
            if (!transitionRunning && pressedInRightPanel) {
                dragActivated = true;
                pressedInRightPanel = false;
            }
            if (dragActivated) {
                setMenuGlassPane(menu, placement);
                draggedX = x[0];
                repaint();
                return;
            }
            super.pointerDragged(x, y);
        }

        public void pointerReleased(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerReleased(x, y);
            boolean isRTLValue = isRTL();
            if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                isRTLValue = !isRTLValue;
            }
            int displayWidth = Display.getInstance().getDisplayWidth();
            if (isRTLValue) {
                if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            } else {
                if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            }
            if (dragActivated || rightPanel.contains(x, y)) {
                setMenuGlassPane(menu, placement);
                draggedX = x;
                int start = x;
                int end = 0;
                if (isRTLValue) {
                    end = getWidth();
                }
                final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
                motion.start();
                manualMotionLock = true;
                sideSwipePotential = false;
                rightSideSwipePotential = false;
                topSwipePotential = false;
                registerAnimated(new Animation() {

                    public boolean animate() {
                        draggedX = motion.getValue();
                        if (motion.isFinished()) {
                            dragActivated = false;
                        }
                        return true;
                    }

                    public void paint(Graphics g) {
                        repaint();
                        if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
                            parent.setTransitionInAnimator(CommonTransitions.createEmpty());
                            parent.show();
                            deregisterAnimated(this);
                            Display.getInstance().callSerially(new Runnable() {

                                public void run() {
                                    clean();
                                }
                            });
                        }
                    }
                });
            }
        }

        public void keyReleased(int keyCode) {
            if (keyCode == leftSK) {
                if (transitionRunning) {
                    return;
                }
                closeMenu();
                return;
            }
            super.keyReleased(keyCode);
        }
    };
    m.setScrollable(false);
    m.removeComponentFromForm(m.getTitleArea());
    m.putClientProperty("Menu", "true");
    m.setTransitionInAnimator(CommonTransitions.createEmpty());
    m.setTransitionOutAnimator(CommonTransitions.createEmpty());
    m.setBackCommand(new Command("") {

        public void actionPerformed(ActionEvent evt) {
            if (transitionRunning) {
                return;
            }
            closeMenu();
        }
    });
    m.setLayout(new BorderLayout());
    if (Display.getInstance().areMutableImagesFast()) {
        rightPanel = new Container(new BorderLayout());
    } else {
        rightPanel = new Container(new BorderLayout()) {

            public void paintBackground(Graphics g) {
            }

            public void paintBackgrounds(Graphics g) {
            }

            public void paint(Graphics g) {
                Component c = (Component) rightPanel.getClientProperty("$parent");
                // not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
                if (c != null) {
                    boolean b = c.isVisible();
                    c.setVisible(true);
                    int x = getAbsoluteX();
                    g.translate(x, 0);
                    Container.sidemenuBarTranslation = x;
                    c.paintComponent(g, true);
                    Container.sidemenuBarTranslation = 0;
                    g.translate(-x, 0);
                    c.setVisible(b);
                }
            }
        };
    }
    if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
        int v = 0;
        if (Display.getInstance().isPortrait()) {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 2 / 3;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
                if (v < 0) {
                    v = openButton.getHeight();
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        } else {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 4 / 10;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        }
        rightPanel.setPreferredH(v);
    } else {
        if (Display.getInstance().isPortrait()) {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 2 / 3;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
                if (v < 0) {
                    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                        if (isRTL()) {
                            v = openButton.getWidth();
                        } else {
                            v = rightSideButton.getWidth();
                        }
                    } else {
                        v = openButton.getWidth();
                    }
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        } else {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 4 / 10;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        }
    }
    if (sidePanel != null) {
        sidePanel.removeAll();
        sidePanel = null;
    }
    sidePanel = createSideNavigationComponent(getCommands(), placement);
    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
        m.addComponent(BorderLayout.WEST, rightPanel);
        m.addComponent(BorderLayout.CENTER, sidePanel);
    } else {
        if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
            m.addComponent(BorderLayout.NORTH, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
            Button button = new Button(" ");
            button.setUIID("Container");
            button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
            m.addComponent(BorderLayout.SOUTH, button);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    closeMenu();
                }
            });
        } else {
            m.addComponent(BorderLayout.EAST, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
        }
    }
    m.putClientProperty("cn1$sideMenuParent", this);
    return m;
}
Also used : Motion(com.codename1.ui.animations.Motion) ActionEvent(com.codename1.ui.events.ActionEvent) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Style(com.codename1.ui.plaf.Style) Animation(com.codename1.ui.animations.Animation)

Example 44 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project codenameone-google-maps by codenameone.

the class MapContainer method addMarker.

/**
 * Adds a marker to the map with the given attributes
 * @param opts The marker options.
 * @return marker reference object that should be used when removing the marker
 */
public MapObject addMarker(MarkerOptions opts) {
    // public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
    EncodedImage icon = opts.getIcon();
    Coord location = opts.getLocation();
    String text = opts.getText();
    String longText = opts.getLongText();
    ActionListener onClick = opts.getOnClick();
    if (internalNative != null) {
        byte[] iconData = null;
        if (icon != null) {
            iconData = icon.getImageData();
            internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
        }
        long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
        MapObject o = new MapObject();
        o.mapKey = key;
        o.callback = onClick;
        markers.add(o);
        return o;
    } else {
        if (internalLightweightCmp != null) {
            PointLayer pl = new PointLayer(location, text, icon);
            if (points == null) {
                points = new PointsLayer();
                internalLightweightCmp.addLayer(points);
            }
            points.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    PointLayer point = (PointLayer) evt.getSource();
                    for (MapObject o : markers) {
                        if (o.point == point) {
                            if (o.callback != null) {
                                o.callback.actionPerformed(new ActionEvent(o));
                            }
                            evt.consume();
                            return;
                        }
                    }
                }
            });
            points.addPoint(pl);
            MapObject o = new MapObject();
            o.point = pl;
            o.callback = onClick;
            markers.add(o);
            internalLightweightCmp.revalidate();
            return o;
        } else {
            String uri = null;
            int iconWidth = 0;
            int iconHeight = 0;
            int anchorU = 0;
            int anchorV = 0;
            if (icon != null) {
                uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
                iconWidth = icon.getWidth();
                iconHeight = icon.getHeight();
                anchorU = (int) (icon.getWidth() * opts.anchorU);
                anchorV = (int) (icon.getHeight() * opts.anchorV);
            }
            // browserBridge.waitForReady();
            MapObject o = new MapObject();
            o.callback = onClick;
            o.pending = true;
            final String fUri = uri;
            final int fIconWidth = iconWidth;
            final int fIconHeight = iconHeight;
            final float fAnchorU = anchorU;
            final float fAnchorV = anchorV;
            browserBridge.ready(() -> {
                internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
                fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
                    o.mapKey = jsres.getInt();
                    o.pending = false;
                });
            });
            // MapObject o = new MapObject();
            // o.mapKey = res.getInt();
            // o.callback = onClick;
            markers.add(o);
            // System.out.println("MapKey added "+o.mapKey);
            return o;
        }
    }
}
Also used : Coord(com.codename1.maps.Coord) PointsLayer(com.codename1.maps.layers.PointsLayer) ActionListener(com.codename1.ui.events.ActionListener) PointLayer(com.codename1.maps.layers.PointLayer) ActionEvent(com.codename1.ui.events.ActionEvent) EncodedImage(com.codename1.ui.EncodedImage) Point(com.codename1.ui.geom.Point)

Example 45 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class VServAds method getPendingAd.

/**
 * {@inheritDoc}
 */
protected Component getPendingAd() {
    if (imageURL == null) {
        return null;
    }
    if (renderNotify != null && renderNotify.length() > 0) {
        ConnectionRequest c = new ConnectionRequest();
        c.setFailSilently(true);
        c.setUrl(renderNotify);
        c.setPost(false);
        NetworkManager.getInstance().addToQueue(c);
    }
    if ("image".equalsIgnoreCase(contentType)) {
        Button adComponent = new Button() {

            public void setIcon(Image icon) {
                if (icon != null && isScaleMode()) {
                    icon = icon.scaledWidth(Display.getInstance().getDisplayWidth());
                }
                super.setIcon(icon);
            }
        };
        adComponent.setUIID("Container");
        adComponent.getStyle().setBgColor(backgroundColor);
        adComponent.getStyle().setOpacity(0xff);
        ImageDownloadService imd = new ImageDownloadService(imageURL, adComponent);
        NetworkManager.getInstance().addToQueueAndWait(imd);
        /*adComponent.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    Display.getInstance().execute(getAdDestination());
                }
            });*/
        return adComponent;
    } else {
        WebBrowser wb = new WebBrowser();
        if (wb.getInternal() instanceof BrowserComponent) {
            BrowserComponent bc = (BrowserComponent) wb.getInternal();
            bc.setBrowserNavigationCallback(new BrowserNavigationCallback() {

                public boolean shouldNavigate(final String url) {
                    unlock(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            Display.getInstance().execute(url);
                        }
                    });
                    return false;
                }
            });
        }
        wb.setURL(imageURL);
        return wb;
    }
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService) ConnectionRequest(com.codename1.io.ConnectionRequest) WebBrowser(com.codename1.components.WebBrowser) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)98 ActionListener (com.codename1.ui.events.ActionListener)55 IOException (java.io.IOException)30 BorderLayout (com.codename1.ui.layouts.BorderLayout)28 Form (com.codename1.ui.Form)18 Hashtable (java.util.Hashtable)14 Vector (java.util.Vector)11 NetworkEvent (com.codename1.io.NetworkEvent)10 Container (com.codename1.ui.Container)9 ActionEvent (java.awt.event.ActionEvent)9 Command (com.codename1.ui.Command)8 ActionListener (java.awt.event.ActionListener)8 Button (com.codename1.ui.Button)7 Style (com.codename1.ui.plaf.Style)7 Rectangle (com.codename1.ui.geom.Rectangle)6 File (java.io.File)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6 BufferedOutputStream (com.codename1.io.BufferedOutputStream)5