Search in sources :

Example 6 with ActionListener

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

the class NetworkManager method addProgressListener.

/**
 * Adds a listener to be notified when progress updates
 *
 * @param al action listener
 */
public void addProgressListener(ActionListener<NetworkEvent> al) {
    if (progressListeners == null) {
        progressListeners = new EventDispatcher();
        progressListeners.setBlocking(false);
    }
    progressListeners.addListener(al);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 7 with ActionListener

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

the class Oauth2 method showAuthentication.

/**
 * This method shows an authentication for login form
 *
 * @param al a listener that will receive at its source either a token for
 * the service or an exception in case of a failure
 * @return a component that should be displayed to the user in order to
 * perform the authentication
 */
public void showAuthentication(ActionListener al) {
    final Form old = Display.getInstance().getCurrent();
    InfiniteProgress inf = new InfiniteProgress();
    final Dialog progress = inf.showInifiniteBlocking();
    Form authenticationForm = new Form("Login");
    authenticationForm.setScrollable(false);
    if (old != null) {
        Command cancel = new Command("Cancel") {

            public void actionPerformed(ActionEvent ev) {
                if (Display.getInstance().getCurrent() == progress) {
                    progress.dispose();
                }
                old.showBack();
            }
        };
        if (authenticationForm.getToolbar() != null) {
            authenticationForm.getToolbar().addCommandToLeftBar(cancel);
        } else {
            authenticationForm.addCommand(cancel);
        }
        authenticationForm.setBackCommand(cancel);
    }
    authenticationForm.setLayout(new BorderLayout());
    authenticationForm.addComponent(BorderLayout.CENTER, createLoginComponent(al, authenticationForm, old, progress));
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) InfiniteProgress(com.codename1.components.InfiniteProgress) Command(com.codename1.ui.Command) Dialog(com.codename1.ui.Dialog) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 8 with ActionListener

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

the class Oauth2 method createLoginComponent.

private Component createLoginComponent(final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
    String URL = oauth2URL + "?client_id=" + clientId + "&redirect_uri=" + Util.encodeUrl(redirectURI);
    if (scope != null) {
        URL += "&scope=" + scope;
    }
    if (clientSecret != null) {
        URL += "&response_type=code";
    } else {
        URL += "&response_type=token";
    }
    if (additionalParams != null) {
        Enumeration e = additionalParams.keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String val = additionalParams.get(key).toString();
            URL += "&" + key + "=" + val;
        }
    }
    DocumentInfo.setDefaultEncoding(DocumentInfo.ENCODING_UTF8);
    final WebBrowser[] web = new WebBrowser[1];
    web[0] = new WebBrowser() {

        @Override
        public void onLoad(String url) {
            handleURL(url, this, al, frm, backToForm, progress);
        }

        public void onStart(String url) {
        }
    };
    web[0].setURL(URL);
    return web[0];
}
Also used : Enumeration(java.util.Enumeration) WebBrowser(com.codename1.components.WebBrowser)

Example 9 with ActionListener

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

the class RequestBuilder method getAsBytesAsync.

/**
 * Executes the request asynchronously and writes the response to the provided
 * Callback
 * @param callback writes the response to this callback
 */
public void getAsBytesAsync(final Callback<Response<byte[]>> callback) {
    ConnectionRequest request = createRequest(false);
    request.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            Response res = null;
            res = new Response(evt.getResponseCode(), evt.getConnectionRequest().getResponseData(), evt.getMessage());
            callback.onSucess(res);
        }
    });
    CN.addToQueue(request);
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent)

Example 10 with ActionListener

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

the class RequestBuilder method getAsJsonMap.

/**
 * Executes the request asynchronously and writes the response to the provided
 * Callback
 * @param callback writes the response to this callback
 * @param onError the error callback
 * @return returns the Connection Request object so it can be killed if necessary
 */
public ConnectionRequest getAsJsonMap(final SuccessCallback<Response<Map>> callback, final FailureCallback<? extends Object> onError) {
    ConnectionRequest request = createRequest(true);
    request.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            if (onError != null) {
                // this is an error response code and should be handled as an error
                if (evt.getResponseCode() > 310) {
                    return;
                }
            }
            Response res = null;
            Map response = (Map) evt.getMetaData();
            res = new Response(evt.getResponseCode(), response, evt.getMessage());
            callback.onSucess(res);
        }
    });
    bindOnError(request, onError);
    CN.addToQueue(request);
    return request;
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent) HashMap(java.util.HashMap) Map(java.util.Map)

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