001: /*
002: * $Id: Image.java 460112 2006-04-02 21:09:16Z jcompagner $
003: * $Revision: 460112 $ $Date: 2006-04-02 23:09:16 +0200 (Sun, 02 Apr 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.markup.html.image;
019:
020: import wicket.IResourceListener;
021: import wicket.Resource;
022: import wicket.ResourceReference;
023: import wicket.markup.ComponentTag;
024: import wicket.markup.MarkupStream;
025: import wicket.markup.html.WebComponent;
026: import wicket.markup.html.image.resource.LocalizedImageResource;
027: import wicket.model.IModel;
028: import wicket.model.Model;
029: import wicket.util.value.ValueMap;
030:
031: /**
032: * An Image component displays a localizable image resource.
033: * <p>
034: * For details of how Images load, generate and manage images, see
035: * {@link LocalizedImageResource}.
036: *
037: * @author Jonathan Locke
038: */
039: public class Image extends WebComponent implements IResourceListener {
040: private static final long serialVersionUID = 1L;
041:
042: /** The image resource this image component references */
043: private final LocalizedImageResource localizedImageResource = new LocalizedImageResource(
044: this );
045:
046: /**
047: * This constructor can be used if you have a img tag that has a src that
048: * points to a PackageResource (which will be created and bind to the shared
049: * resources) Or if you have a value attribute in your tag for which the
050: * image factory can make an image.
051: *
052: * @see wicket.Component#Component(String)
053: */
054: public Image(final String id) {
055: super (id);
056: }
057:
058: /**
059: * Constructs an image from an image resourcereference. That resource
060: * reference will bind its resource to the current SharedResources.
061: *
062: * If you are using non sticky session clustering and the resource reference
063: * is pointing to a Resource that isn't guaranteed to be on every server,
064: * for example a dynamic image or resources that aren't added with a
065: * IInitializer at application startup. Then if only that resource is
066: * requested from another server, without the rendering of the page, the
067: * image won't be there and will result in a broken link.
068: *
069: * @param id
070: * See Component
071: * @param resourceReference
072: * The shared image resource
073: */
074: public Image(final String id,
075: final ResourceReference resourceReference) {
076: this (id, resourceReference, null);
077: }
078:
079: /**
080: * Constructs an image from an image resourcereference. That resource
081: * reference will bind its resource to the current SharedResources.
082: *
083: * If you are using non sticky session clustering and the resource reference
084: * is pointing to a Resource that isn't guaranteed to be on every server,
085: * for example a dynamic image or resources that aren't added with a
086: * IInitializer at application startup. Then if only that resource is
087: * requested from another server, without the rendering of the page, the
088: * image won't be there and will result in a broken link.
089: *
090: * @param id
091: * See Component
092: * @param resourceReference
093: * The shared image resource
094: * @param resourceParameters
095: * The resource parameters
096: */
097: public Image(final String id,
098: final ResourceReference resourceReference,
099: ValueMap resourceParameters) {
100: super (id);
101: setImageResourceReference(resourceReference, resourceParameters);
102: }
103:
104: /**
105: * Constructs an image directly from an image resource.
106: *
107: * This one doesn't have the 'non sticky session clustering' problem that
108: * the ResourceReference constructor has. But this will result in a non
109: * 'stable' url and the url will have request parameters.
110: *
111: * @param id
112: * See Component
113: *
114: * @param imageResource
115: * The image resource
116: */
117: public Image(final String id, final Resource imageResource) {
118: super (id);
119: setImageResource(imageResource);
120: }
121:
122: /**
123: * @see wicket.Component#Component(String, IModel)
124: */
125: public Image(final String id, final IModel model) {
126: super (id, model);
127: }
128:
129: /**
130: * @param id
131: * See Component
132: * @param string
133: * Name of image
134: * @see wicket.Component#Component(String, IModel)
135: */
136: public Image(final String id, final String string) {
137: this (id, new Model(string));
138: }
139:
140: /**
141: * @see wicket.IResourceListener#onResourceRequested()
142: */
143: public void onResourceRequested() {
144: localizedImageResource.onResourceRequested();
145: }
146:
147: /**
148: * @param imageResource
149: * The new ImageResource to set.
150: */
151: public void setImageResource(final Resource imageResource) {
152: this .localizedImageResource.setResource(imageResource);
153: }
154:
155: /**
156: * @param resourceReference
157: * The shared ImageResource to set.
158: */
159: public void setImageResourceReference(
160: final ResourceReference resourceReference) {
161: this .localizedImageResource
162: .setResourceReference(resourceReference);
163: }
164:
165: /**
166: * @param resourceReference
167: * The shared ImageResource to set.
168: * @param parameters
169: * Set the resource parameters for the resource.
170: */
171: public void setImageResourceReference(
172: final ResourceReference resourceReference,
173: final ValueMap parameters) {
174: this .localizedImageResource.setResourceReference(
175: resourceReference, parameters);
176: }
177:
178: /**
179: * @return Resource returned from subclass
180: */
181: protected Resource getImageResource() {
182: return null;
183: }
184:
185: /**
186: * @return ResourceReference returned from subclass
187: */
188: protected ResourceReference getImageResourceReference() {
189: return null;
190: }
191:
192: /**
193: * @see wicket.Component#initModel()
194: */
195: protected IModel initModel() {
196: // Images don't support Compound models. They either have a simple
197: // model, explicitly set, or they use their tag's src or value
198: // attribute to determine the image.
199: return null;
200: }
201:
202: /**
203: * @see wicket.Component#onComponentTag(ComponentTag)
204: */
205: protected void onComponentTag(final ComponentTag tag) {
206: checkComponentTag(tag, "img");
207: super .onComponentTag(tag);
208: final Resource resource = getImageResource();
209: if (resource != null) {
210: localizedImageResource.setResource(resource);
211: }
212: final ResourceReference resourceReference = getImageResourceReference();
213: if (resourceReference != null) {
214: localizedImageResource
215: .setResourceReference(resourceReference);
216: }
217: localizedImageResource.setSrcAttribute(tag);
218: }
219:
220: /**
221: * @see wicket.Component#onComponentTagBody(MarkupStream, ComponentTag)
222: */
223: protected void onComponentTagBody(final MarkupStream markupStream,
224: final ComponentTag openTag) {
225: }
226: }
|