You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pivot.apache.org by Noel Grandin <no...@gmail.com> on 2009/10/14 16:47:15 UTC

Re: svn commit: r825153 - in /incubator/pivot/trunk: tutorials/src/org/apache/pivot/tutorials/splitters.wtkx wtk/src/org/apache/pivot/wtk/ImageView.java wtk/src/org/apache/pivot/wtk/ImageViewListener.java wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java

Hi

The piece of code below is going to suffer from the classic problem that
if you reference the same image multiple times, you will end up loading
it multiple times.
Which won't cause any problems, because it doesn't really matter that
the image in the cache will get overwritten a couple of times.

But it's probably not going to help startup times.

The correct approach would be to modify the ResourceCache to have an
interface that looked like

public ResourceCache {
   public void asynchPut(Object key, IFetcher fetcher)
}

Where asynchPut() maintains a table of keys that are in-progress
loading, and makes sure that only the first IFetcher for each key gets
executed.

Regards, Noel.


gbrown@apache.org wrote:
> Author: gbrown
> Date: Wed Oct 14 14:27:32 2009
> New Revision: 825153
>
> URL: http://svn.apache.org/viewvc?rev=825153&view=rev
> Log:
> Resolve issue PIVOT-327.
>
> Modified:
>     incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/splitters.wtkx
>     incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageView.java
>     incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageViewListener.java
>     incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java
>
> Modified: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/splitters.wtkx
> URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/splitters.wtkx?rev=825153&r1=825152&r2=825153&view=diff
> ==============================================================================
> Binary files - no diff available.
>
> Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageView.java
> URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageView.java?rev=825153&r1=825152&r2=825153&view=diff
> ==============================================================================
> --- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageView.java (original)
> +++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageView.java Wed Oct 14 14:27:32 2009
> @@ -22,7 +22,9 @@
>  import org.apache.pivot.serialization.JSONSerializer;
>  import org.apache.pivot.util.ListenerList;
>  import org.apache.pivot.util.ThreadUtilities;
> +import org.apache.pivot.util.concurrent.Task;
>  import org.apache.pivot.util.concurrent.TaskExecutionException;
> +import org.apache.pivot.util.concurrent.TaskListener;
>  import org.apache.pivot.wtk.media.Image;
>  
>  
> @@ -40,6 +42,13 @@
>          }
>  
>          @Override
> +        public void asynchronousChanged(ImageView imageView) {
> +            for (ImageViewListener listener : this) {
> +                listener.asynchronousChanged(imageView);
> +            }
> +        }
> +
> +        @Override
>          public void imageKeyChanged(ImageView imageView, String previousImageKey) {
>              for (ImageViewListener listener : this) {
>                  listener.imageKeyChanged(imageView, previousImageKey);
> @@ -48,6 +57,7 @@
>      }
>  
>      private Image image = null;
> +    private boolean asynchronous = false;
>      private String imageKey = null;
>  
>      private ImageViewListenerList imageViewListeners = new ImageViewListenerList();
> @@ -106,7 +116,7 @@
>       * @param imageURL
>       * The location of the image to set.
>       */
> -    public final void setImage(URL imageURL) {
> +    public final void setImage(final URL imageURL) {
>          if (imageURL == null) {
>              throw new IllegalArgumentException("imageURL is null.");
>          }
> @@ -114,13 +124,30 @@
>          Image image = (Image)ApplicationContext.getResourceCache().get(imageURL);
>  
>          if (image == null) {
> -            try {
> -                image = Image.load(imageURL);
> -            } catch (TaskExecutionException exception) {
> -                throw new IllegalArgumentException(exception);
> -            }
> +            if (asynchronous) {
> +                Image.load(imageURL, new TaskAdapter<Image>(new TaskListener<Image>() {
> +                    @Override
> +                    public void taskExecuted(Task<Image> task) {
> +                        Image image = task.getResult();
> +
> +                        setImage(image);
> +                        ApplicationContext.getResourceCache().put(imageURL, image);
> +                    }
> +
> +                    @Override
> +                    public void executeFailed(Task<Image> task) {
> +                        // No-op
> +                    }
> +                }));
> +            } else {
> +                try {
> +                    image = Image.load(imageURL);
> +                } catch (TaskExecutionException exception) {
> +                    throw new IllegalArgumentException(exception);
> +                }
>  
> -            ApplicationContext.getResourceCache().put(imageURL, image);
> +                ApplicationContext.getResourceCache().put(imageURL, image);
> +            }
>          }
>  
>          setImage(image);
> @@ -147,6 +174,31 @@
>      }
>  
>      /**
> +     * Returns the image view's asynchronous flag.
> +     *
> +     * @return
> +     * <tt>true</tt> if images specified via URL will be loaded in the background;
> +     * <tt>false</tt> if they will be loaded synchronously.
> +     */
> +    public boolean isAsynchronous() {
> +        return asynchronous;
> +    }
> +
> +    /**
> +     * Sets the image view's asynchronous flag.
> +     *
> +     * @param asynchronous
> +     * <tt>true</tt> if images specified via URL will be loaded in the background;
> +     * <tt>false</tt> if they will be loaded synchronously.
> +     */
> +    public void setAsynchronous(boolean asynchronous) {
> +        if (this.asynchronous != asynchronous) {
> +            this.asynchronous = asynchronous;
> +            imageViewListeners.asynchronousChanged(this);
> +        }
> +    }
> +
> +    /**
>       * Returns the image view's image key.
>       *
>       * @return
>
> Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageViewListener.java
> URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageViewListener.java?rev=825153&r1=825152&r2=825153&view=diff
> ==============================================================================
> --- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageViewListener.java (original)
> +++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ImageViewListener.java Wed Oct 14 14:27:32 2009
> @@ -31,6 +31,10 @@
>          }
>  
>          @Override
> +        public void asynchronousChanged(ImageView imageView) {
> +        }
> +
> +        @Override
>          public void imageKeyChanged(ImageView imageView, String previousImageKey) {
>          }
>      }
> @@ -44,6 +48,13 @@
>      public void imageChanged(ImageView imageView, Image previousImage);
>  
>      /**
> +     * Called when an image view's asynchronous flag has changed.
> +     *
> +     * @param imageView
> +     */
> +    public void asynchronousChanged(ImageView imageView);
> +
> +    /**
>       * Called when an image view's image key has changed.
>       *
>       * @param imageView
>
> Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java
> URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java?rev=825153&r1=825152&r2=825153&view=diff
> ==============================================================================
> --- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java (original)
> +++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ImageViewSkin.java Wed Oct 14 14:27:32 2009
> @@ -336,6 +336,11 @@
>      }
>  
>      @Override
> +    public void asynchronousChanged(ImageView imageView) {
> +        // No-op
> +    }
> +
> +    @Override
>      public void imageKeyChanged(ImageView imageView, String previousImageKey) {
>          // No-op
>      }
>
>
>