Search in sources :

Example 46 with Dialog

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

the class Log method bindCrashProtection.

/**
 * Binds pro based crash protection logic that will send out an email in case of an exception thrown on the EDT
 *
 * @param consumeError true will hide the error from the user, false will leave the builtin logic that defaults to
 * showing an error dialog to the user
 */
public static void bindCrashProtection(final boolean consumeError) {
    if (Display.getInstance().isSimulator()) {
        return;
    }
    Display.getInstance().addEdtErrorHandler(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (consumeError) {
                evt.consume();
            }
            p("Exception in " + Display.getInstance().getProperty("AppName", "app") + " version " + Display.getInstance().getProperty("AppVersion", "Unknown"));
            p("OS " + Display.getInstance().getPlatformName());
            p("Error " + evt.getSource());
            if (Display.getInstance().getCurrent() != null) {
                p("Current Form " + Display.getInstance().getCurrent().getName());
            } else {
                p("Before the first form!");
            }
            e((Throwable) evt.getSource());
            sendLog();
        }
    });
    crashBound = true;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 47 with Dialog

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

the class Oauth2 method authenticate.

/**
 * This method preforms the actual authentication, this method is a blocking
 * method that will display the user the html authentication pages.
 *
 * @return the method if passes authentication will return the access token
 * or null if authentication failed.
 *
 * @throws IOException the method will throw an IOException if something
 * went wrong in the communication.
 * @deprecated use createAuthComponent or showAuthentication which work
 * asynchronously and adapt better to different platforms
 */
public String authenticate() {
    if (token == null) {
        login = new Dialog();
        boolean i = Dialog.isAutoAdjustDialogSize();
        Dialog.setAutoAdjustDialogSize(false);
        login.setLayout(new BorderLayout());
        login.setScrollable(false);
        Component html = createLoginComponent(null, null, null, null);
        login.addComponent(BorderLayout.CENTER, html);
        login.setScrollable(false);
        login.setDialogUIID("Container");
        login.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 300));
        login.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300));
        login.show(0, 0, 0, 0, false, true);
        Dialog.setAutoAdjustDialogSize(i);
    }
    return token;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Example 48 with Dialog

use of com.codename1.ui.Dialog 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 49 with Dialog

use of com.codename1.ui.Dialog 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 50 with Dialog

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

the class Dialog method growOrShrinkImpl.

private void growOrShrinkImpl(int w, int h) {
    Component contentPane = super.getContentPane();
    Component title = super.getTitleComponent();
    int prefHeight = contentPane.getPreferredH();
    int prefWidth = contentPane.getPreferredW();
    Style contentPaneStyle = contentPane.getStyle();
    Style titleStyle = title.getStyle();
    // if the dialog is packed we can scale it far more accurately based on intention
    if (position != null) {
        int menuHeight = 0;
        if (getSoftButtonCount() > 1) {
            Component menuBar = getSoftButton(0).getParent();
            Style menuStyle = menuBar.getStyle();
            menuHeight = menuBar.getPreferredH() + menuStyle.getVerticalMargins();
        }
        prefWidth = Math.min(prefWidth, w);
        // - titleStyle.getMargin(false, TOP) - titleStyle.getMargin(false, BOTTOM);
        h = h - menuHeight - title.getPreferredH();
        int topBottom = Math.max(0, (h - prefHeight) / 2);
        int leftRight = Math.max(0, (w - prefWidth) / 2);
        int top = topBottom, bottom = topBottom;
        int left = leftRight, right = leftRight;
        if (position.equals(BorderLayout.EAST)) {
            left = Math.max(0, w - prefWidth);
            right = 0;
        } else {
            if (position.equals(BorderLayout.WEST)) {
                right = 0;
                left = Math.max(0, w - prefWidth);
            } else {
                if (position.equals(BorderLayout.NORTH)) {
                    top = 0;
                    bottom = Math.max(0, h - prefHeight);
                } else {
                    if (position.equals(BorderLayout.SOUTH)) {
                        top = Math.max(0, h - prefHeight);
                        bottom = 0;
                    }
                }
            }
        }
        titleStyle.setMargin(Component.TOP, 0, true);
        titleStyle.setMargin(Component.BOTTOM, 0, true);
        titleStyle.setMargin(Component.LEFT, 0, true);
        titleStyle.setMargin(Component.RIGHT, 0, true);
        contentPaneStyle.setMargin(Component.TOP, top, true);
        contentPaneStyle.setMargin(Component.BOTTOM, bottom, true);
        contentPaneStyle.setMargin(Component.LEFT, left, true);
        contentPaneStyle.setMargin(Component.RIGHT, right, true);
        return;
    } else {
        int oldW = getWidth();
        int oldH = getHeight();
        if (oldW != w || oldH != h) {
            // try to preserve the old size of the dialog if we still have room for it...
            if (prefWidth <= w && prefHeight <= h) {
                float oldLeftRightDistRatio = 1;
                if (left + right != 0) {
                    oldLeftRightDistRatio = ((float) left) / ((float) left + right);
                }
                float oldTopBottomDistRatio = 1;
                if (left + right != 0) {
                    oldTopBottomDistRatio = ((float) top) / ((float) top + bottom);
                }
                top = Math.max(0, (int) ((h - prefHeight) * oldTopBottomDistRatio));
                left = Math.max(0, (int) ((w - prefWidth) * oldLeftRightDistRatio));
                bottom = Math.max(0, (h - prefHeight) - top);
                right = Math.max(0, (w - prefWidth) - left);
                titleStyle.setMargin(Component.TOP, 0, true);
                titleStyle.setMargin(Component.BOTTOM, 0, true);
                titleStyle.setMargin(Component.LEFT, 0, true);
                titleStyle.setMargin(Component.RIGHT, 0, true);
                contentPaneStyle.setMargin(Component.TOP, top, true);
                contentPaneStyle.setMargin(Component.BOTTOM, bottom, true);
                contentPaneStyle.setMargin(Component.LEFT, left, true);
                contentPaneStyle.setMargin(Component.RIGHT, right, true);
                return;
            } else {
                float ratioW = ((float) w) / ((float) oldW);
                float ratioH = ((float) h) / ((float) oldH);
                Style s = getDialogStyle();
                s.setMargin(TOP, (int) (s.getMarginTop() * ratioH));
                s.setMargin(BOTTOM, (int) (s.getMarginBottom() * ratioH));
                s.setMargin(LEFT, (int) (s.getMarginLeft(isRTL()) * ratioW));
                s.setMargin(RIGHT, (int) (s.getMarginRight(isRTL()) * ratioW));
                titleStyle.setMargin(TOP, (int) (titleStyle.getMarginTop() * ratioH));
                titleStyle.setMargin(LEFT, (int) (titleStyle.getMarginLeft(isRTL()) * ratioW));
                titleStyle.setMargin(RIGHT, (int) (titleStyle.getMarginRight(isRTL()) * ratioW));
                return;
            }
        }
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Dialog (com.codename1.ui.Dialog)28 Form (com.codename1.ui.Form)23 BorderLayout (com.codename1.ui.layouts.BorderLayout)17 Component (com.codename1.ui.Component)16 ActionEvent (com.codename1.ui.events.ActionEvent)14 Container (com.codename1.ui.Container)13 Style (com.codename1.ui.plaf.Style)12 ActionListener (com.codename1.ui.events.ActionListener)10 GridLayout (com.codename1.ui.layouts.GridLayout)8 UIManager (com.codename1.ui.plaf.UIManager)7 IOException (java.io.IOException)7 InfiniteProgress (com.codename1.components.InfiniteProgress)6 Command (com.codename1.ui.Command)6 Label (com.codename1.ui.Label)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 ConnectionRequest (com.codename1.io.ConnectionRequest)4 Button (com.codename1.ui.Button)4 CheckBox (com.codename1.ui.CheckBox)4 Painter (com.codename1.ui.Painter)4 TextArea (com.codename1.ui.TextArea)4