Search in sources :

Example 6 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class ImageDownloadService method postResponse.

/**
 * {@inheritDoc}
 */
protected void postResponse() {
    // trigger an exception in case of an invalid image
    result.getWidth();
    Image image = result;
    if (toScale != null && toScale.getWidth() != image.getWidth() && toScale.getHeight() != image.getHeight()) {
        image = scaleImage(image, toScale, maintainAspectRatio);
    }
    final Image i = image;
    if (parentLabel != null) {
        final Dimension pref = parentLabel.getPreferredSize();
        if (parentLabel.getComponentForm() != null) {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (isDownloadToStyles()) {
                        parentLabel.getUnselectedStyle().setBgImage(i);
                        parentLabel.getSelectedStyle().setBgImage(i);
                        parentLabel.getPressedStyle().setBgImage(i);
                    } else {
                        parentLabel.setIcon(i);
                    }
                    Dimension newPref = parentLabel.getPreferredSize();
                    // sized image in place or has a hardcoded preferred size.
                    if (pref.getWidth() != newPref.getWidth() || pref.getHeight() != newPref.getHeight()) {
                        parentLabel.getComponentForm().revalidate();
                    }
                }
            });
        } else {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (isDownloadToStyles()) {
                        parentLabel.getUnselectedStyle().setBgImage(i);
                        parentLabel.getSelectedStyle().setBgImage(i);
                        parentLabel.getPressedStyle().setBgImage(i);
                    } else {
                        parentLabel.setIcon(i);
                    }
                }
            });
        }
        parentLabel.repaint();
        return;
    } else {
        if (targetList != null) {
            setEntryInListModel(targetOffset, image);
            // revalidate only once to avoid multiple revalidate refreshes during scroll
            if (targetList.getParent() != null) {
                if (alwaysRevalidate) {
                    targetList.getParent().revalidate();
                } else {
                    if (targetList.getClientProperty("$imgDSReval") == null) {
                        targetList.putClientProperty("$imgDSReval", Boolean.TRUE);
                        targetList.getParent().revalidate();
                    } else {
                        targetList.repaint();
                    }
                }
            }
        }
    }
    // if this is a list cell renderer component
    fireResponseListener(new NetworkEvent(this, result));
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) Dimension(com.codename1.ui.geom.Dimension) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 7 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class FaceBookAccess method getUser.

/**
 * Gets a User from a user id
 * This is a sync method it will block until a response it returned
 * @param userId the user id or null to get details on the authenticated user
 * @return the User requested
 */
public User getUser(String userId) throws IOException {
    String id = userId;
    if (id == null) {
        id = "me";
    }
    final User user = new User();
    final Vector err = new Vector();
    addResponseCodeListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            NetworkEvent ne = (NetworkEvent) evt;
            err.addElement(ne);
            removeResponseCodeListener(this);
        }
    });
    getFaceBookObject(id, new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Vector v = (Vector) ((NetworkEvent) evt).getMetaData();
            Hashtable t = (Hashtable) v.elementAt(0);
            user.copy(t);
        }
    }, true, false);
    if (err.size() > 0) {
        throw new IOException(((NetworkEvent) err.elementAt(0)).getResponseCode() + ": " + ((NetworkEvent) err.elementAt(0)).getMessage());
    }
    return user;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) NetworkEvent(com.codename1.io.NetworkEvent) IOException(java.io.IOException) Vector(java.util.Vector)

Example 8 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class FaceBookAccess method getUser.

/**
 * Gets a user from a user id
 *
 * @param userId the user id or null to get detaild on the authenticated user
 * @param user an object to fill with the user details
 * @param callback the callback that should be updated when the data arrives
 */
public void getUser(String userId, final User user, final ActionListener callback) throws IOException {
    String id = userId;
    if (id == null) {
        id = "me";
    }
    getFaceBookObject(id, new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Vector v = (Vector) ((NetworkEvent) evt).getMetaData();
            Hashtable t = (Hashtable) v.elementAt(0);
            if (user != null) {
                user.copy(t);
            }
            if (callback != null) {
                callback.actionPerformed(evt);
            }
        }
    });
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) NetworkEvent(com.codename1.io.NetworkEvent) Vector(java.util.Vector)

Example 9 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class FaceBookAccess method getPost.

/**
 * Gets a Post from a postId
 * This is a sync method it will block until a response it returned
 * @param postId the post id
 * @param needAuth if this object is public needAuth can be false and no
 * authentication will be performed
 * @return the Post requested
 */
public Post getPost(String postId, boolean needAuth) throws IOException {
    final Post post = new Post();
    final Vector err = new Vector();
    addResponseCodeListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            NetworkEvent ne = (NetworkEvent) evt;
            err.addElement(ne);
            removeResponseCodeListener(this);
        }
    });
    getFaceBookObject(postId, new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Vector v = (Vector) ((NetworkEvent) evt).getMetaData();
            Hashtable t = (Hashtable) v.elementAt(0);
            post.copy(t);
        }
    }, needAuth, false);
    if (err.size() > 0) {
        throw new IOException(((NetworkEvent) err.elementAt(0)).getResponseCode() + ": " + ((NetworkEvent) err.elementAt(0)).getMessage());
    }
    return post;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) NetworkEvent(com.codename1.io.NetworkEvent) IOException(java.io.IOException) Vector(java.util.Vector)

Example 10 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class FaceBookAccess method getAlbum.

/**
 * Gets a Album from a albumId
 * This is a sync method it will block until a response it returned
 * @param the albumId
 * @param needAuth if this object is public needAuth can be false and no
 * authentication will be performed
 *
 * @return the Album requested
 */
public Album getAlbum(String albumId, boolean needAuth) throws IOException {
    final Album album = new Album();
    final Vector err = new Vector();
    addResponseCodeListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            NetworkEvent ne = (NetworkEvent) evt;
            err.addElement(ne);
            removeResponseCodeListener(this);
        }
    });
    getFaceBookObject(albumId, new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Vector v = (Vector) ((NetworkEvent) evt).getMetaData();
            Hashtable t = (Hashtable) v.elementAt(0);
            album.copy(t);
        }
    }, needAuth, false);
    if (err.size() > 0) {
        throw new IOException(((NetworkEvent) err.elementAt(0)).getResponseCode() + ": " + ((NetworkEvent) err.elementAt(0)).getMessage());
    }
    return album;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) NetworkEvent(com.codename1.io.NetworkEvent) IOException(java.io.IOException) Vector(java.util.Vector)

Aggregations

NetworkEvent (com.codename1.io.NetworkEvent)21 ActionEvent (com.codename1.ui.events.ActionEvent)10 ActionListener (com.codename1.ui.events.ActionListener)10 Vector (java.util.Vector)8 IOException (java.io.IOException)7 Hashtable (java.util.Hashtable)7 EventDispatcher (com.codename1.ui.util.EventDispatcher)6 EncodedImage (com.codename1.ui.EncodedImage)4 FileEncodedImage (com.codename1.components.FileEncodedImage)3 StorageImage (com.codename1.components.StorageImage)3 ConnectionRequest (com.codename1.io.ConnectionRequest)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)3 Image (com.codename1.ui.Image)3 InputStreamReader (java.io.InputStreamReader)3 InfiniteProgress (com.codename1.components.InfiniteProgress)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 CharArrayReader (com.codename1.io.CharArrayReader)1 JSONParser (com.codename1.io.JSONParser)1 MultipartRequest (com.codename1.io.MultipartRequest)1 ParseException (com.codename1.l10n.ParseException)1