001 /*
002 * Copyright 2006 Google Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package com.google.gwt.user.client.ui;
017
018 import com.google.gwt.user.client.DOM;
019 import com.google.gwt.user.client.Element;
020 import com.google.gwt.user.client.Event;
021
022 import java.util.HashMap;
023
024 /**
025 * A widget that displays the image at a given URL.
026 *
027 * <h3>CSS Style Rules</h3>
028 * <ul class="css">
029 * <li>.gwt-Image { }</li>
030 * </ul>
031 *
032 * <h3>Example</h3>
033 * {@link com.google.gwt.examples.ImageExample code}
034 */
035 public class Image extends Widget implements SourcesClickEvents,
036 SourcesMouseEvents, SourcesLoadEvents {
037
038 /**
039 * This map is used to store prefetched images. If a reference is not kept to
040 * the prefetched image objects, they can get garbage collected, which
041 * sometimes keeps them from getting fully fetched.
042 */
043 private static HashMap prefetchImages = new HashMap();
044
045 /**
046 * Causes the browser to pre-fetch the image at a given URL.
047 *
048 * @param url the URL of the image to be prefetched
049 */
050 public static void prefetch(String url) {
051 Element img = DOM.createImg();
052 DOM.setAttribute(img, "src", url);
053 prefetchImages.put(url, img);
054 }
055
056 private ClickListenerCollection clickListeners;
057 private LoadListenerCollection loadListeners;
058 private MouseListenerCollection mouseListeners;
059
060 /**
061 * Creates an empty image.
062 */
063 public Image() {
064 setElement(DOM.createImg());
065 sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.ONLOAD | Event.ONERROR);
066 }
067
068 /**
069 * Creates an image with a specified URL.
070 *
071 * @param url the URL of the image to be displayed
072 */
073 public Image(String url) {
074 this();
075 setUrl(url);
076 }
077
078 public void addClickListener(ClickListener listener) {
079 if (clickListeners == null)
080 clickListeners = new ClickListenerCollection();
081 clickListeners.add(listener);
082 }
083
084 public void addLoadListener(LoadListener listener) {
085 if (loadListeners == null)
086 loadListeners = new LoadListenerCollection();
087 loadListeners.add(listener);
088 }
089
090 public void addMouseListener(MouseListener listener) {
091 if (mouseListeners == null)
092 mouseListeners = new MouseListenerCollection();
093 mouseListeners.add(listener);
094 }
095
096 /**
097 * Gets the URL of the image.
098 *
099 * @return the image URL
100 */
101 public String getUrl() {
102 return DOM.getAttribute(getElement(), "src");
103 }
104
105 public void onBrowserEvent(Event event) {
106 switch (DOM.eventGetType(event)) {
107 case Event.ONCLICK: {
108 if (clickListeners != null)
109 clickListeners.fireClick(this);
110 break;
111 }
112 case Event.ONMOUSEDOWN:
113 case Event.ONMOUSEUP:
114 case Event.ONMOUSEMOVE:
115 case Event.ONMOUSEOVER:
116 case Event.ONMOUSEOUT: {
117 if (mouseListeners != null)
118 mouseListeners.fireMouseEvent(this, event);
119 break;
120 }
121 case Event.ONLOAD: {
122 if (loadListeners != null)
123 loadListeners.fireLoad(this);
124 break;
125 }
126 case Event.ONERROR: {
127 if (loadListeners != null)
128 loadListeners.fireError(this);
129 break;
130 }
131 }
132 }
133
134 public void removeClickListener(ClickListener listener) {
135 if (clickListeners != null)
136 clickListeners.remove(listener);
137 }
138
139 public void removeLoadListener(LoadListener listener) {
140 if (loadListeners != null)
141 loadListeners.remove(listener);
142 }
143
144 public void removeMouseListener(MouseListener listener) {
145 if (mouseListeners != null)
146 mouseListeners.remove(listener);
147 }
148
149 /**
150 * Sets the URL of the image to be displayed.
151 *
152 * @param url the image URL
153 */
154 public void setUrl(String url) {
155 DOM.setAttribute(getElement(), "src", url);
156 }
157 }