Search in sources :

Example 51 with Callback

use of com.codename1.util.Callback in project CodenameOne by codenameone.

the class Component method animate.

/**
 * {@inheritDoc}
 */
public boolean animate() {
    if (!visible) {
        return false;
    }
    Image bgImage = getStyle().getBgImage();
    boolean animateBackground = bgImage != null && bgImage.isAnimation() && bgImage.animate();
    Motion m = getAnimationMotion();
    // perform regular scrolling
    if (m != null && destScrollY != -1 && destScrollY != getScrollY()) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        setScrollY(m.getValue());
        if (destScrollY == scrollY) {
            destScrollY = -1;
            deregisterAnimatedInternal();
            updateTensileHighlightIntensity(0, 0, m != null);
        }
        return true;
    }
    boolean animateY = false;
    boolean animateX = false;
    // perform the dragging motion if exists
    if (draggedMotionY != null) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        int dragVal = draggedMotionY.getValue();
        // this can't be a part of the parent if since we need the last value to arrive
        if (draggedMotionY.isFinished()) {
            if (dragVal < 0) {
                startTensile(dragVal, 0, true);
            } else {
                int iv = getInvisibleAreaUnderVKB();
                int edge = (getScrollDimension().getHeight() - getHeight() + iv);
                if (dragVal > edge && edge > 0) {
                    startTensile(dragVal, getScrollDimension().getHeight() - getHeight() + iv, true);
                } else {
                    if (snapToGrid && getScrollY() < edge && getScrollY() > 0) {
                        boolean tVal = tensileDragEnabled;
                        tensileDragEnabled = true;
                        int dest = getGridPosY();
                        int scroll = getScrollY();
                        if (dest != scroll) {
                            startTensile(scroll, dest, true);
                        } else {
                            draggedMotionY = null;
                        }
                        tensileDragEnabled = tVal;
                    } else {
                        draggedMotionY = null;
                    }
                }
            }
            // special callback to scroll Y to allow developers to override the setScrollY method effectively
            setScrollY(dragVal);
            updateTensileHighlightIntensity(dragVal, getScrollDimension().getHeight() - getHeight() + getInvisibleAreaUnderVKB(), false);
        }
        if (scrollListeners != null) {
            scrollListeners.fireScrollEvent(this.scrollX, dragVal, this.scrollX, this.scrollY);
        }
        scrollY = dragVal;
        onScrollY(scrollY);
        updateTensileHighlightIntensity(0, 0, false);
        animateY = true;
    }
    if (draggedMotionX != null) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        int dragVal = draggedMotionX.getValue();
        // this can't be a part of the parent if since we need the last value to arrive
        if (draggedMotionX.isFinished()) {
            if (dragVal < 0) {
                startTensile(dragVal, 0, false);
            } else {
                int edge = (getScrollDimension().getWidth() - getWidth());
                if (dragVal > edge && edge > 0) {
                    startTensile(dragVal, getScrollDimension().getWidth() - getWidth(), false);
                } else {
                    if (snapToGrid && getScrollX() < edge && getScrollX() > 0) {
                        boolean tVal = tensileDragEnabled;
                        tensileDragEnabled = true;
                        int dest = getGridPosX();
                        int scroll = getScrollX();
                        if (dest != scroll) {
                            startTensile(scroll, dest, false);
                        } else {
                            draggedMotionX = null;
                        }
                        tensileDragEnabled = tVal;
                    } else {
                        draggedMotionX = null;
                    }
                }
            }
            // special callback to scroll X to allow developers to override the setScrollY method effectively
            setScrollX(dragVal);
        }
        if (scrollListeners != null) {
            scrollListeners.fireScrollEvent(dragVal, this.scrollY, this.scrollX, this.scrollY);
        }
        scrollX = dragVal;
        onScrollX(scrollX);
        animateX = true;
    }
    if (animateY || animateX) {
        return true;
    }
    if (getClientProperty("$pullToRelease") != null) {
        return true;
    }
    Painter bgp = getStyle().getBgPainter();
    boolean animateBackgroundB = bgp != null && bgp.getClass() != BGPainter.class && bgp instanceof Animation && (bgp != this) && ((Animation) bgp).animate();
    animateBackground = animateBackgroundB || animateBackground;
    if (getUIManager().getLookAndFeel().isFadeScrollBar()) {
        if (tensileHighlightIntensity > 0) {
            tensileHighlightIntensity = Math.max(0, tensileHighlightIntensity - (scrollOpacityChangeSpeed * 2));
        }
        if (scrollOpacity > 0 && !dragActivated) {
            scrollOpacity = Math.max(0, scrollOpacity - scrollOpacityChangeSpeed);
            return true;
        }
    }
    if (!animateBackground && (destScrollY == -1 || destScrollY == scrollY) && !animateBackground && m == null && draggedMotionY == null && draggedMotionX == null && !dragActivated) {
        tryDeregisterAnimated();
    }
    return animateBackground;
}
Also used : Motion(com.codename1.ui.animations.Motion) Animation(com.codename1.ui.animations.Animation) ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Point(com.codename1.ui.geom.Point)

Example 52 with Callback

use of com.codename1.util.Callback in project CodenameOne by codenameone.

the class ComponentSelector method fadeIn.

/**
 * Fade in this set of components.  Prior to calling this, the component visibility should
 * be set to "false".
 * @param duration The duration of the fade in.
 * @param callback Callback to run when animation completes.
 * @return
 */
public ComponentSelector fadeIn(final int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations1 = new ArrayList<ComponentAnimation>();
    final ArrayList<ComponentAnimation> animations2 = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.getStyle().setBgColor(0xff0000);
                // placeholder.getStyle().setBgTransparency(255);
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                // System.out.println("Placeholder height "+c.getHeight());
                // parent.replace(c, placeholder, false);
                // c.setHidden(false);
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createEmpty());
                animations1.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations1.toArray(new ComponentAnimation[animations1.size()])), new Runnable() {

            public void run() {
                AnimationManager mgr = null;
                for (final Component c : animatingComponents) {
                    Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    if (placeholder != null) {
                        // System.out.println("Placeholder height after replace "+(c.getHeight() + c.getStyle().getMarginBottom() + c.getStyle().getMarginTop()));
                        // System.out.println("Placeholder not null");
                        c.putClientProperty(placeholderProperty, null);
                        AnimationManager cmgr = placeholder.getAnimationManager();
                        if (cmgr != null) {
                            // System.out.println("Animation manager not null");
                            mgr = cmgr;
                            c.setVisible(true);
                            Container parent = placeholder.getParent();
                            if (parent != null) {
                                // System.out.println("Parent not null");
                                ComponentAnimation a = parent.createReplaceTransition(placeholder, c, CommonTransitions.createFade(duration));
                                animations2.add(a);
                            }
                        }
                    }
                }
                if (mgr != null) {
                    final AnimationManager fmgr = mgr;
                    $(new Runnable() {

                        public void run() {
                            fmgr.addAnimation(ComponentAnimation.compoundAnimation(animations2.toArray(new ComponentAnimation[animations2.size()])), new Runnable() {

                                public void run() {
                                    if (callback != null) {
                                        callback.onSucess(ComponentSelector.this);
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 53 with Callback

use of com.codename1.util.Callback in project CodenameOne by codenameone.

the class ComponentSelector method animateStyle.

/**
 * Animates this set of components, replacing any modified style properties of the
 * destination style to the components.
 * @param destStyle The style to apply to the components via animation.
 * @param duration The duration of the animation (ms)
 * @param callback Callback to call after animation is complete.
 * @return Self for chaining
 * @see Component#createStyleAnimation(java.lang.String, int)
 */
public ComponentSelector animateStyle(Style destStyle, int duration, final SuccessCallback<ComponentSelector> callback) {
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    AnimationManager mgr = null;
    for (Component c : this) {
        AnimationManager cmgr = c.getAnimationManager();
        if (cmgr != null) {
            mgr = cmgr;
            Style sourceStyle = c.getUnselectedStyle();
            destStyle.merge(sourceStyle);
            animations.add(c.createStyleAnimation(sourceStyle, destStyle, duration, null));
        }
    }
    if (mgr != null) {
        ComponentAnimation anim = ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()]));
        mgr.addAnimation(anim, new Runnable() {

            public void run() {
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList) Style(com.codename1.ui.plaf.Style)

Example 54 with Callback

use of com.codename1.util.Callback in project CodenameOne by codenameone.

the class BrowserComponent method fireBrowserNavigationCallbacks.

/**
 * Fires all of the registered browser navigation callbacks against the provided URL.
 * @param url The URL to fire the navigation callbacks against.
 * @return True if all of the callbacks say that they can browse.  False otherwise.
 */
public boolean fireBrowserNavigationCallbacks(String url) {
    boolean shouldNavigate = true;
    if (browserNavigationCallback != null && !browserNavigationCallback.shouldNavigate(url)) {
        shouldNavigate = false;
    }
    if (browserNavigationCallbacks != null) {
        for (BrowserNavigationCallback cb : browserNavigationCallbacks) {
            if (!cb.shouldNavigate(url)) {
                shouldNavigate = false;
            }
        }
    }
    if (!url.startsWith("javascript:") && url.indexOf(RETURN_URL_PREFIX) != -1) {
        // System.out.println("Received browser navigation callback "+url);
        String result = decodeURL(url.substring(url.indexOf(RETURN_URL_PREFIX) + RETURN_URL_PREFIX.length()), "UTF-8");
        // System.out.println("After decode "+result);
        Result structResult = Result.fromContent(result, Result.JSON);
        int callbackId = structResult.getAsInteger("callbackId");
        final String value = structResult.getAsString("value");
        final String type = structResult.getAsString("type");
        final String errorMessage = structResult.getAsString("errorMessage");
        final SuccessCallback<JSRef> callback = popReturnValueCallback(callbackId);
        if (jsCallbacks != null && jsCallbacks.contains(callback)) {
            // If this is a registered callback, then we treat it more like
            // an event listener, and we retain it for future callbacks.
            returnValueCallbacks.put(callbackId, callback);
        }
        if (callback != null) {
            if (errorMessage != null) {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        if (callback instanceof Callback) {
                            ((Callback) callback).onError(this, new RuntimeException(errorMessage), 0, errorMessage);
                        }
                    }
                });
            } else {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        callback.onSucess(new JSRef(value, type));
                    }
                });
            }
        } else {
            Log.e(new RuntimeException("Received return value from javascript, but no callback could be found for that ID"));
        }
        shouldNavigate = false;
    }
    return shouldNavigate;
}
Also used : BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback) Callback(com.codename1.util.Callback) SuccessCallback(com.codename1.util.SuccessCallback) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback) Result(com.codename1.processing.Result)

Example 55 with Callback

use of com.codename1.util.Callback in project CodenameOne by codenameone.

the class Login method doLogin.

/**
 * Logs in the user.
 * If the service has a native login it will try to use that, otherwise an
 * Oauth2 web login will be used.
 */
public void doLogin() {
    if (isNativeLoginSupported()) {
        nativelogin();
    } else {
        if (oauth2URL == null) {
            System.out.println("No oauth2URL found Use setOauth2URL");
            return;
        }
        if (clientId == null) {
            System.out.println("No ClientId found Use setClientId");
            return;
        }
        if (redirectURI == null) {
            System.out.println("No redirectURI found Use setRedirectURI");
            return;
        }
        if (clientSecret == null) {
            System.out.println("No clientSecret found Use setClientSecret");
            return;
        }
        Oauth2 auth = createOauth2();
        auth.showAuthentication(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // success
                if (evt.getSource() instanceof AccessToken) {
                    AccessToken t = (AccessToken) evt.getSource();
                    setAccessToken(t);
                    if (callback != null) {
                        callback.loginSuccessful();
                    }
                    return;
                }
                if (evt.getSource() instanceof String) {
                    String t = (String) evt.getSource();
                    setAccessToken(new AccessToken(t, null));
                    if (callback != null) {
                        callback.loginSuccessful();
                    }
                    return;
                }
                if (evt.getSource() instanceof Exception) {
                    if (callback != null) {
                        Exception e = (Exception) evt.getSource();
                        Log.e(e);
                        callback.loginFailed(e.getMessage());
                    }
                }
            }
        });
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) AccessToken(com.codename1.io.AccessToken) Oauth2(com.codename1.io.Oauth2) IOException(java.io.IOException)

Aggregations

ActionListener (com.codename1.ui.events.ActionListener)14 ActionEvent (com.codename1.ui.events.ActionEvent)12 IOException (java.io.IOException)11 ConnectionRequest (com.codename1.io.ConnectionRequest)10 EventDispatcher (com.codename1.ui.util.EventDispatcher)8 NetworkEvent (com.codename1.io.NetworkEvent)6 InputStream (java.io.InputStream)5 Intent (android.content.Intent)4 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)4 ArrayList (java.util.ArrayList)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)3 Uri (android.net.Uri)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 AccessToken (com.codename1.io.AccessToken)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)3 EncodedImage (com.codename1.ui.EncodedImage)3 Image (com.codename1.ui.Image)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3