View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.utils;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.vectomatic.svg.edit.client.SvgrealApp;
26  
27  import com.google.gwt.event.dom.client.LoadEvent;
28  import com.google.gwt.event.dom.client.LoadHandler;
29  import com.google.gwt.event.logical.shared.HasInitializeHandlers;
30  import com.google.gwt.event.logical.shared.InitializeEvent;
31  import com.google.gwt.event.logical.shared.InitializeHandler;
32  import com.google.gwt.event.shared.GwtEvent;
33  import com.google.gwt.event.shared.HandlerRegistration;
34  import com.google.gwt.resources.client.ImageResource;
35  
36  /**
37   * Class to preload a list of images
38   * @author laaglu
39   */
40  public class ImageLoader implements HasInitializeHandlers {
41  	protected List<SimpleImage> images;
42  	protected ImageResource[] resources;
43  	
44  	/**
45  	 * Constructor
46  	 */
47  	public ImageLoader() {			
48  		images = new ArrayList<SimpleImage>();
49  		resources = new ImageResource[0];
50  	}
51  	
52  	/**
53  	 * Preloads the specified images, asynchronously. Once loaded, a
54  	 * {@link com.google.gwt.event.dom.client.LoadEvent} is fired.
55  	 * @param resources
56  	 * The images to preload.
57  	 */
58  	public void loadImages(ImageResource[] resources) {
59  		images.clear();
60  		this.resources = resources;
61  		readNext();
62  	}
63  	
64  	private void readNext() {
65  		if (images.size() < resources.length) {
66  			final SimpleImage image = new SimpleImage();
67  			images.add(image);
68  			image.addLoadHandler(new LoadHandler() {
69  				@Override
70  				public void onLoad(LoadEvent event) {
71  					readNext();
72  				}
73  			});
74  			image.setSrc(resources[images.size() - 1].getSafeUri().asString());
75  		} else {
76  			InitializeEvent.fire(this);
77  		}
78  	}
79  
80  	/**
81  	 * Returns the preloaded images.
82  	 * @return the preloaded images.
83  	 * @throws IllegalStateException if this method is
84  	 * invoked synchronously before the 
85  	 * {@link com.google.gwt.event.dom.client.LoadEvent} has been fired.
86  	 */
87  	public Map<ImageResource, SimpleImage> getImages() {
88  		if (resources.length != images.size()) {
89  			throw new IllegalStateException();
90  		}
91  		Map<ImageResource, SimpleImage> resourceToImage = new HashMap<ImageResource, SimpleImage>();
92  		for (int i = 0; i < resources.length; i++) {
93  			resourceToImage.put(resources[i], images.get(i));
94  		}
95  		return resourceToImage;
96  	}
97  	
98  	@Override
99  	public void fireEvent(GwtEvent<?> event) {
100 		SvgrealApp.getApp().getEventBus().fireEventFromSource(event, this);
101 		
102 	}
103 
104 	@Override
105 	public HandlerRegistration addInitializeHandler(InitializeHandler handler) {
106 		return SvgrealApp.getApp().getEventBus().addHandlerToSource(InitializeEvent.getType(), this, handler);
107 	}	
108 }