Search in sources :

Example 71 with ActionListener

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

the class NetworkManager method addErrorListener.

/**
 * Adds a generic listener to a network error that is invoked before the exception is propagated.
 * Notice that this doesn't apply to server error codes!
 * Consume the event in order to prevent it from propagating further.
 *
 * @param e callback will be invoked with the Exception as the source object
 */
public void addErrorListener(ActionListener<NetworkEvent> e) {
    if (errorListeners == null) {
        errorListeners = new EventDispatcher();
        errorListeners.setBlocking(true);
    }
    errorListeners.addListener(e);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 72 with ActionListener

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

the class Oauth2 method handleURL.

private void handleURL(String url, WebBrowser web, final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
    if ((url.startsWith(redirectURI))) {
        if (Display.getInstance().getCurrent() == progress) {
            progress.dispose();
        }
        web.stop();
        // remove the browser component.
        if (login != null) {
            login.removeAll();
            login.revalidate();
        }
        if (url.indexOf("code=") > -1) {
            Hashtable params = getParamsFromURL(url);
            ConnectionRequest req = new ConnectionRequest() {

                protected void readResponse(InputStream input) throws IOException {
                    byte[] tok = Util.readInputStream(input);
                    String t = new String(tok);
                    if (t.startsWith("{")) {
                        JSONParser p = new JSONParser();
                        Map map = p.parseJSON(new StringReader(t));
                        token = (String) map.get("access_token");
                        Object ex = map.get("expires_in");
                        if (ex == null) {
                            ex = map.get("expires");
                        }
                        if (ex != null) {
                            expires = ex.toString();
                        }
                    } else {
                        token = t.substring(t.indexOf("=") + 1, t.indexOf("&"));
                        int off = t.indexOf("expires=");
                        int start = 8;
                        if (off == -1) {
                            off = t.indexOf("expires_in=");
                            start = 11;
                        }
                        if (off > -1) {
                            int end = t.indexOf('&', off);
                            if (end < 0 || end < off) {
                                end = t.length();
                            }
                            expires = t.substring(off + start, end);
                        }
                    }
                    if (login != null) {
                        login.dispose();
                    }
                }

                protected void handleException(Exception err) {
                    if (backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(err, ActionEvent.Type.Exception));
                    }
                }

                protected void postResponse() {
                    if (backToParent && backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                    }
                }
            };
            req.setUrl(tokenRequestURL);
            req.setPost(true);
            req.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.addArgument("client_id", clientId);
            req.addArgument("redirect_uri", redirectURI);
            req.addArgument("client_secret", clientSecret);
            req.addArgument("code", (String) params.get("code"));
            req.addArgument("grant_type", "authorization_code");
            NetworkManager.getInstance().addToQueue(req);
        } else if (url.indexOf("error_reason=") > -1) {
            Hashtable table = getParamsFromURL(url);
            String error = (String) table.get("error_reason");
            if (login != null) {
                login.dispose();
            }
            if (backToForm != null) {
                backToForm.showBack();
            }
            if (al != null) {
                al.actionPerformed(new ActionEvent(new IOException(error), ActionEvent.Type.Exception));
            }
        } else {
            boolean success = url.indexOf("#") > -1;
            if (success) {
                String accessToken = url.substring(url.indexOf("#") + 1);
                if (accessToken.indexOf("&") > 0) {
                    token = accessToken.substring(accessToken.indexOf("=") + 1, accessToken.indexOf("&"));
                } else {
                    token = accessToken.substring(accessToken.indexOf("=") + 1);
                }
                if (login != null) {
                    login.dispose();
                }
                if (backToParent && backToForm != null) {
                    backToForm.showBack();
                }
                if (al != null) {
                    al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                }
            }
        }
    } else {
        if (frm != null && Display.getInstance().getCurrent() != frm) {
            progress.dispose();
            frm.show();
        }
    }
}
Also used : Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(com.codename1.util.regex.StringReader) Map(java.util.Map)

Example 73 with ActionListener

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

the class LayerWithZoomLevels method getTiles.

private void getTiles() throws RuntimeException {
    _tiles = new Vector();
    Dimension tileSize = _map.tileSize();
    int posY = 0;
    _delta = null;
    while (posY - tileSize.getHeight() < getHeight()) {
        int posX = 0;
        while (posX - tileSize.getWidth() < getWidth()) {
            Tile tile;
            Coord cur = _map.translate(_center, _zoom, posX - getWidth() / 2, getHeight() / 2 - posY);
            if (_map.projection().extent().contains(cur)) {
                tile = _map.tileFor(_map.bboxFor(cur, _zoom));
                if (_delta == null) {
                    _delta = tile.pointPosition(cur);
                }
                tile.setsTileReadyListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        refreshLayers = true;
                        repaint();
                    }
                });
                _tiles.addElement(new PositionedTile(new Point(posX, posY), tile));
            }
            posX += tileSize.getWidth();
        }
        posY += tileSize.getHeight();
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Dimension(com.codename1.ui.geom.Dimension) Point(com.codename1.ui.geom.Point) Vector(java.util.Vector) Point(com.codename1.ui.geom.Point)

Example 74 with ActionListener

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

the class Util method downloadUrlTo.

private static boolean downloadUrlTo(String url, String fileName, boolean showProgress, boolean background, boolean storage, ActionListener callback) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    if (callback != null) {
        cr.addResponseListener(callback);
    }
    if (storage) {
        cr.setDestinationStorage(fileName);
    } else {
        cr.setDestinationFile(fileName);
    }
    if (background) {
        NetworkManager.getInstance().addToQueue(cr);
        return true;
    }
    if (showProgress) {
        InfiniteProgress ip = new InfiniteProgress();
        Dialog d = ip.showInifiniteBlocking();
        NetworkManager.getInstance().addToQueueAndWait(cr);
        d.dispose();
    } else {
        NetworkManager.getInstance().addToQueueAndWait(cr);
    }
    int rc = cr.getResponseCode();
    return rc == 200 || rc == 201;
}
Also used : InfiniteProgress(com.codename1.components.InfiniteProgress) Dialog(com.codename1.ui.Dialog)

Example 75 with ActionListener

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

the class Toolbar method constructOnTopSideMenu.

private void constructOnTopSideMenu() {
    if (sidemenuDialog == null) {
        permanentSideMenuContainer = constructSideNavigationComponent();
        final Form parent = getComponentForm();
        parent.addPointerPressedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                if (sidemenuDialog.isShowing()) {
                    if (evt.getX() > sidemenuDialog.getWidth()) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        closeSideMenu();
                        evt.consume();
                    } else {
                        if (evt.getX() + Display.getInstance().convertToPixels(8) > sidemenuDialog.getWidth()) {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        } else {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        }
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                    }
                } else {
                    int displayWidth = Display.getInstance().getDisplayWidth();
                    final int sensitiveSection = displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                    if (evt.getX() < sensitiveSection) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        evt.consume();
                    } else {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        permanentSideMenuContainer.pointerPressed(evt.getX(), evt.getY());
                    }
                }
            }
        });
        parent.addPointerDraggedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuCharged");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", Boolean.TRUE);
                    showOnTopSidemenu(evt.getX(), false);
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        permanentSideMenuContainer.pointerDragged(evt.getX(), evt.getY());
                        evt.consume();
                    }
                }
            }
        });
        parent.addPointerReleasedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuActivated");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", null);
                    if (evt.getX() < parent.getWidth() / 4) {
                        closeSideMenu();
                    } else {
                        showOnTopSidemenu(-1, true);
                    }
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                        permanentSideMenuContainer.pointerReleased(evt.getX(), evt.getY());
                    }
                }
            }
        });
        sidemenuDialog = new InteractionDialog(new BorderLayout());
        sidemenuDialog.setFormMode(true);
        sidemenuDialog.setUIID("Container");
        sidemenuDialog.setDialogUIID("Container");
        sidemenuDialog.getTitleComponent().remove();
        sidemenuDialog.add(BorderLayout.CENTER, permanentSideMenuContainer);
        if (sidemenuSouthComponent != null) {
            sidemenuDialog.add(BorderLayout.SOUTH, sidemenuSouthComponent);
        }
        float size = 4.5f;
        try {
            size = Float.parseFloat(getUIManager().getThemeConstant("menuImageSize", "4.5"));
        } catch (Throwable t) {
            Log.e(t);
        }
        if (!parent.getUIManager().isThemeConstant("hideLeftSideMenuBool", false)) {
            Image i = (Image) parent.getUIManager().getThemeImageConstant("sideMenuImage");
            if (i != null) {
                Command cmd = addCommandToLeftBar("", i, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
                Image p = (Image) parent.getUIManager().getThemeImageConstant("sideMenuPressImage");
                if (p != null) {
                    findCommandComponent(cmd).setPressedIcon(p);
                }
            } else {
                addMaterialCommandToLeftBar("", FontImage.MATERIAL_MENU, size, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
            }
        }
    }
}
Also used : InteractionDialog(com.codename1.components.InteractionDialog) ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)61 ActionListener (com.codename1.ui.events.ActionListener)61 IOException (java.io.IOException)25 BorderLayout (com.codename1.ui.layouts.BorderLayout)20 EventDispatcher (com.codename1.ui.util.EventDispatcher)16 Hashtable (java.util.Hashtable)14 NetworkEvent (com.codename1.io.NetworkEvent)13 Form (com.codename1.ui.Form)13 Vector (java.util.Vector)11 Container (com.codename1.ui.Container)10 Button (com.codename1.ui.Button)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 Dialog (com.codename1.ui.Dialog)7 EncodedImage (com.codename1.ui.EncodedImage)6 Image (com.codename1.ui.Image)6 Label (com.codename1.ui.Label)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6