Search in sources :

Example 11 with SuccessCallback

use of com.codename1.util.SuccessCallback 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 12 with SuccessCallback

use of com.codename1.util.SuccessCallback 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 13 with SuccessCallback

use of com.codename1.util.SuccessCallback in project codenameone-google-maps by codenameone.

the class MapContainer method initBrowserComponent.

private void initBrowserComponent(String htmlApiKey) {
    if (debug) {
        Log.e(new RuntimeException("Initializing Browser Component.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
    }
    // System.out.println("About to check location");
    Location loc = LocationManager.getLocationManager().getLastKnownLocation();
    try {
        // if (true)return;
        // System.out.println("About to load map text");
        String str = Util.readToString(Display.getInstance().getResourceAsStream(null, "/com_codename1_googlemaps_MapContainer.html"));
        // System.out.println("Map text: "+str);
        str = StringUtil.replaceAll(str, "YOUR_API_KEY", htmlApiKey);
        // System.out.println("Finished setting API key");
        str = StringUtil.replaceAll(str, "//origin = MAPCONTAINER_ORIGIN", "origin = {lat: " + loc.getLatitude() + ", lng: " + loc.getLongitude() + "};");
        // System.out.println("Finished setting origin");
        internalBrowser.setPage(str, "/");
        if (debug) {
            Log.e(new RuntimeException("Adding onLoad Listener.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
        }
        internalBrowser.addWebEventListener("onLoad", new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (debug) {
                    Log.e(new RuntimeException("Inside onLoad Listener.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
                }
                // JavascriptContext ctx = new JavascriptContext(internalBrowser);
                // browserBridge.ctx = ctx;
                internalBrowser.execute("com_codename1_googlemaps_MapContainer = {}");
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireTapEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int x = new Double(Double.parseDouble(parts[0])).intValue();
                        int y = new Double(Double.parseDouble(parts[1])).intValue();
                        fireTapEvent(x, y);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireLongPressEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int x = new Double(Double.parseDouble(parts[0])).intValue();
                        int y = new Double(Double.parseDouble(parts[1])).intValue();
                        fireLongPressEvent(x, y);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMapChangeEvent = function(zoom,lat,lon){callback.onSuccess(zoom+','+lat+','+lon)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int zoom = Integer.parseInt(parts[0]);
                        double lat = Double.parseDouble(parts[1]);
                        double lon = Double.parseDouble(parts[2]);
                        fireMapListenerEvent(zoom, lat, lon);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMarkerEvent = function(id){callback.onSuccess(id)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        fireMarkerEvent(value.getInt());
                    }
                });
                internalBrowser.execute("callback.onSuccess(com_codename1_googlemaps_MapContainer_bridge)", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        if ("null".equals(value.getValue()) || value.getJSType() == BrowserComponent.JSType.UNDEFINED) {
                            internalBrowser.execute("com_codename1_googlemaps_MapContainer_onReady=function(bridge){callback.onSuccess(bridge)};", new SuccessCallback<BrowserComponent.JSRef>() {

                                public void onSucess(BrowserComponent.JSRef value) {
                                    browserBridge.ready = true;
                                }
                            });
                        } else {
                            browserBridge.ready = true;
                        }
                    }
                });
                // /System.out.println("Bridge is ready");
                if (debug) {
                    Log.p("About to fire browserBridge.ready(null) event to kick things off");
                }
                browserBridge.ready(null);
            }
        });
        return;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : SuccessCallback(com.codename1.util.SuccessCallback) ActionEvent(com.codename1.ui.events.ActionEvent) JSRef(com.codename1.ui.BrowserComponent.JSRef) IOException(java.io.IOException) Point(com.codename1.ui.geom.Point) JSRef(com.codename1.ui.BrowserComponent.JSRef) ActionListener(com.codename1.ui.events.ActionListener) BrowserComponent(com.codename1.ui.BrowserComponent) Location(com.codename1.location.Location)

Aggregations

SuccessCallback (com.codename1.util.SuccessCallback)4 ArrayList (java.util.ArrayList)4 ConnectionRequest (com.codename1.io.ConnectionRequest)3 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)3 IOException (java.io.IOException)3 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 FileSystemStorage (com.codename1.io.FileSystemStorage)1 NetworkEvent (com.codename1.io.NetworkEvent)1 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)1 ParseException (com.codename1.l10n.ParseException)1 Location (com.codename1.location.Location)1 Result (com.codename1.processing.Result)1 BrowserComponent (com.codename1.ui.BrowserComponent)1 JSRef (com.codename1.ui.BrowserComponent.JSRef)1 EncodedImage (com.codename1.ui.EncodedImage)1 BrowserNavigationCallback (com.codename1.ui.events.BrowserNavigationCallback)1 Point (com.codename1.ui.geom.Point)1 Style (com.codename1.ui.plaf.Style)1 Callback (com.codename1.util.Callback)1