Search in sources :

Example 51 with ActionListener

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

the class FaceBookAccess method getFaceBookObject.

/**
 * This method returns immediately and will call the callback when it returns with
 * the FaceBook Object data.
 *
 * @param faceBookId the object id that we would like to query
 * @param callback the callback that should be updated when the data arrives
 */
public void getFaceBookObject(String faceBookId, final ActionListener callback) throws IOException {
    checkAuthentication();
    final FacebookRESTService con = new FacebookRESTService(token, faceBookId, "", false);
    con.addResponseListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (!con.isAlive()) {
                return;
            }
            if (callback != null) {
                callback.actionPerformed(evt);
            }
        }
    });
    if (slider != null) {
        SliderBridge.bindProgress(con, slider);
    }
    for (int i = 0; i < responseCodeListeners.size(); i++) {
        con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
    }
    current = con;
    NetworkManager.getInstance().addToQueue(con);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 52 with ActionListener

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

the class FaceBookAccess method getAlbumPhotos.

/**
 * This method returns a list model of photos that automatically fetches additional images as necessary
 * @param targetList required for the image download code
 * @param albumId the id of the album
 * @param photoCount the number of photos within the album
 * @param placeholder a placeholder image that will determine the size of the images requested
 * @return the list of the images
 */
private ListModel getAlbumPhotos(final Component targetList, final String albumId, final int photoCount, final Image placeholder) {
    if (!isAuthenticated()) {
        return null;
    }
    Hashtable[] h = new Hashtable[photoCount];
    int hlen = h.length;
    for (int iter = 0; iter < hlen; iter++) {
        h[iter] = new Hashtable();
        h[iter].put("photo", placeholder);
        if (iter < 30) {
            h[iter].put("fetching", Boolean.TRUE);
        }
    }
    DefaultListModel dl = new DefaultListModel((Object[]) h) {

        public Object getItem(int offset) {
            Hashtable hash = (Hashtable) super.getItemAt(offset);
            if (!hash.containsKey("fetching")) {
                int limit = Math.min(30, photoCount - offset);
                for (int iter = 0; iter < limit; iter++) {
                    Hashtable current = (Hashtable) super.getItemAt(iter + offset);
                    if (current.containsKey("fetching")) {
                        break;
                    }
                    current.put("fetching", Boolean.TRUE);
                }
                FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
                con.setResponseDestination(this);
                con.setResponseOffset(0);
                con.addArgument("limit", "" + limit);
                con.addArgument("offset", "" + offset);
                for (int i = 0; i < responseCodeListeners.size(); i++) {
                    con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
                }
                NetworkManager.getInstance().addToQueueAndWait(con);
                for (int iter = 0; iter < limit; iter++) {
                    Hashtable current = (Hashtable) getItemAt(iter + offset);
                    ImageDownloadService.createImageToStorage((String) current.get("photo"), targetList, iter + offset, "photo", ((String) current.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
                }
            }
            return hash;
        }
    };
    FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
    con.setResponseDestination(dl);
    con.setResponseOffset(0);
    con.addArgument("limit", "30");
    con.addArgument("offset", "0");
    for (int i = 0; i < responseCodeListeners.size(); i++) {
        con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
    }
    NetworkManager.getInstance().addToQueueAndWait(con);
    for (int iter = 0; iter < Math.min(30, photoCount); iter++) {
        Hashtable hash = (Hashtable) dl.getItemAt(iter);
        ImageDownloadService.createImageToStorage((String) hash.get("photo"), targetList, iter, "photo", ((String) hash.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
    }
    return dl;
}
Also used : Hashtable(java.util.Hashtable) DefaultListModel(com.codename1.ui.list.DefaultListModel)

Example 53 with ActionListener

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

the class ConnectionRequest method downloadImage.

private void downloadImage(final SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail, boolean useCache) {
    setReadResponseForErrors(false);
    if (useCache) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                if (getDestinationFile() != null) {
                    String file = getDestinationFile();
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(file)) {
                        try {
                            EncodedImage img = EncodedImage.create(fs.openInputStream(file), (int) fs.getLength(file));
                            if (img == null) {
                                throw new IOException("Failed to load image at " + file);
                            }
                            CallbackDispatcher.dispatchSuccess(onSuccess, img);
                        } catch (Exception ex) {
                            CallbackDispatcher.dispatchError(onFail, ex);
                        }
                    } else {
                        downloadImage(onSuccess, onFail, false);
                    }
                } else if (getDestinationStorage() != null) {
                    String file = getDestinationStorage();
                    Storage fs = Storage.getInstance();
                    if (fs.exists(file)) {
                        try {
                            EncodedImage img = EncodedImage.create(fs.createInputStream(file), fs.entrySize(file));
                            if (img == null) {
                                throw new IOException("Failed to load image at " + file);
                            }
                            CallbackDispatcher.dispatchSuccess(onSuccess, img);
                        } catch (Exception ex) {
                            CallbackDispatcher.dispatchError(onFail, ex);
                        }
                    } else {
                        downloadImage(onSuccess, onFail, false);
                    }
                }
            }
        });
    } else {
        final ActionListener onDownload = new ActionListener<NetworkEvent>() {

            public void actionPerformed(NetworkEvent nevt) {
                int rc = nevt.getResponseCode();
                if (rc == 200 || rc == 201) {
                    downloadImage(onSuccess, onFail, true);
                } else {
                    if (nevt.getError() == null) {
                        nevt.setError(new IOException("Failed to get image:  Code was " + nevt.getResponseCode()));
                    }
                    CallbackDispatcher.dispatchError(onFail, nevt.getError());
                }
                removeResponseListener(this);
            }
        };
        addResponseListener(onDownload);
        NetworkManager.getInstance().addToQueue(this);
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) IOException(java.io.IOException) ParseException(com.codename1.l10n.ParseException)

Example 54 with ActionListener

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

the class ConnectionRequest method addResponseCodeListener.

/**
 * Adds a listener that would be notified on the CodenameOne thread of a response code that
 * is not a 200 (OK) or 301/2 (redirect) response code.
 *
 * @param a listener
 */
public void addResponseCodeListener(ActionListener<NetworkEvent> a) {
    if (responseCodeListeners == null) {
        responseCodeListeners = new EventDispatcher();
        responseCodeListeners.setBlocking(false);
    }
    responseCodeListeners.addListener(a);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 55 with ActionListener

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

the class ClearableTextField method wrap.

/**
 * Wraps the given text field with a UI that will allow us to clear it
 * @param tf the text field
 * @param iconSize size in millimeters for the clear icon, -1 for default size
 * @return a Container that should be added to the UI instead of the actual text field
 */
public static ClearableTextField wrap(final TextArea tf, float iconSize) {
    ClearableTextField cf = new ClearableTextField();
    Button b = new Button("", tf.getUIID());
    if (iconSize > 0) {
        FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR, iconSize);
    } else {
        FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR);
    }
    removeCmpBackground(tf);
    removeCmpBackground(b);
    cf.setUIID(tf.getUIID());
    cf.add(BorderLayout.CENTER, tf);
    cf.add(BorderLayout.EAST, b);
    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            tf.stopEditing();
            tf.setText("");
            tf.startEditingAsync();
        }
    });
    return cf;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) 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