001: /*
002: * $Id: PackageResourceReference.java 5151 2006-03-28 13:50:28 +0200 (di, 28 mrt
003: * 2006) joco01 $ $Revision: 462287 $ $Date: 2006-03-28 13:50:28 +0200 (di, 28 mrt
004: * 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.markup.html;
020:
021: import java.util.Locale;
022:
023: import wicket.Application;
024: import wicket.Resource;
025: import wicket.ResourceReference;
026:
027: /**
028: * A convenience class for creating resource references to static resources.
029: *
030: * @author Jonathan Locke
031: * @author Eelco Hillenius
032: *
033: * @deprecated Use {@link ResourceReference} instead. This class will be removed in Wicket 2.0
034: */
035: public class PackageResourceReference extends ResourceReference {
036: private static final long serialVersionUID = 1L;
037:
038: /** pre-calculated has code for this immutable object. */
039: private int hash;
040:
041: /**
042: * Constuctor to get a resource reference to a packaged resource. It will
043: * bind itself directly to the given application object, so that the
044: * resource will be created if it did not exist and added to the application
045: * shared resources.
046: *
047: * Package resources should be added by a IInitializer implementation So
048: * that all needed packaged resources are there on startup of the
049: * application.
050: *
051: * @param application
052: * The application to bind to
053: * @param scope
054: * The scope of the binding
055: * @param name
056: * The name of the resource
057: * @param locale
058: * The Locale from which the search for the PackageResource must
059: * start
060: * @param style
061: * The Style of the PackageResource
062: * @throws IllegalArgumentException
063: * when no corresponding resource is found
064: *
065: * @see ResourceReference#ResourceReference(Class, String)
066: */
067: public PackageResourceReference(Application application,
068: Class scope, String name, Locale locale, String style) {
069: super (scope, name);
070: checkExists(scope, name, locale, style);
071: setHash(scope, name, locale, style);
072: setLocale(locale);
073: setStyle(style);
074: bind(application);
075: if (getResource() instanceof PackageResource) {
076: setLocale(((PackageResource) getResource()).getLocale());
077: }
078: }
079:
080: /**
081: * Constuctor to get a resource reference to a packaged resource. It will
082: * bind itself directly to the given application object, so that the
083: * resource will be created if it did not exist and added to the application
084: * shared resources.
085: *
086: * Package resources should be added by a IInitializer implementation So
087: * that all needed packaged resources are there on startup of the
088: * application.
089: *
090: * @param application
091: * The application to bind to
092: * @param scope
093: * The scope of the binding
094: * @param name
095: * The name of the resource
096: * @throws IllegalArgumentException
097: * when no corresponding resource is found
098: * @see ResourceReference#ResourceReference(Class, String)
099: */
100: public PackageResourceReference(Application application,
101: Class scope, String name) {
102: this (application, scope, name, null, null);
103: }
104:
105: /**
106: * Constuctor to get a resource reference to a packaged resource. It will
107: * bind itself directly to the given application object, so that the
108: * resource will be created if it did not exist and added to the application
109: * shared resources.
110: *
111: * Package resources should be added by a IInitializer implementation So
112: * that all needed packaged resources are there on startup of the
113: * application.
114: *
115: * The scope of this constructor will be the wicket.Application.class
116: * itself. so the shared resources key wil be "wicket.Application/name"
117: *
118: * @param application
119: * The application to bind to
120: * @param name
121: * The name of the resource
122: * @throws IllegalArgumentException
123: * when no corresponding resource is found
124: * @see ResourceReference#ResourceReference(Class, String)
125: */
126: public PackageResourceReference(Application application, String name) {
127: this (application, Application.class, name);
128: }
129:
130: /**
131: * Constuctor to get a resource reference to a packaged resource that is
132: * already bindend to the current applicaiton.
133: *
134: * It will not bind a resource to the current application object, so the
135: * resource must be created by a IInitializer implementation. So that it is
136: * already binded at startup.
137: *
138: * @param scope
139: * The scope of the binding
140: * @param name
141: * The name of the resource
142: * @throws IllegalArgumentException
143: * when no corresponding resource is found
144: * @see ResourceReference#ResourceReference(Class, String)
145: */
146: public PackageResourceReference(Class scope, String name) {
147: super (scope, name);
148: // This can't be done in the constructor because ResourceReferences can
149: // be declared static in a class that is stored in the session.
150: // checkExists(scope, name, null, null);
151: setHash(scope, name, null, null);
152: }
153:
154: /**
155: * @see java.lang.Object#hashCode()
156: */
157: public int hashCode() {
158: return hash;
159: }
160:
161: /**
162: * @see java.lang.Object#equals(java.lang.Object)
163: */
164: public boolean equals(Object obj) {
165: if (obj instanceof PackageResourceReference) {
166: PackageResourceReference that = (PackageResourceReference) obj;
167: return checkEquals(this .getScope(), that.getScope())
168: && checkEquals(this .getName(), that.getName())
169: && checkEquals(this .getLocale(), that.getLocale())
170: && checkEquals(this .getStyle(), that.getStyle());
171: }
172: return false;
173: }
174:
175: /**
176: * @see wicket.ResourceReference#newResource()
177: */
178: protected Resource newResource() {
179: PackageResource packageResource = PackageResource.get(
180: getScope(), getName(), getLocale(), getStyle());
181: if (packageResource != null) {
182: locale = packageResource.getLocale();
183: } else {
184: throw new IllegalArgumentException(
185: "package resource [scope=" + getScope() + ",name="
186: + getName() + ",locale=" + getLocale()
187: + "style=" + getStyle() + "] not found");
188: }
189: return packageResource;
190: }
191:
192: /**
193: * Checks whether the packaged resource can be found. If it can't be found,
194: * an {@link IllegalArgumentException} will be thrown. If it was found, this
195: * method just returns.
196: *
197: * @param scope
198: * the scope of the resource
199: * @param name
200: * the name of the resource
201: * @param locale
202: * the optional locale for the resource
203: * @param style
204: * the optional style for the resource
205: */
206: private void checkExists(Class scope, String name, Locale locale,
207: String style) {
208: if (!PackageResource.exists(scope, name, locale, style)) {
209: throw new IllegalArgumentException(
210: "package resource [scope=" + scope + ",name="
211: + name + ",locale=" + locale + "style="
212: + style + "] not found");
213: }
214: }
215:
216: private final boolean checkEquals(Object o1, Object o2) {
217: if (o1 == null) {
218: return o2 == null;
219: } else if (o2 == null) {
220: return false;
221: } else {
222: return o1.equals(o2);
223: }
224: }
225:
226: private final void setHash(Class scope, String name, Locale locale,
227: String style) {
228: int result = 17;
229: result = 37 * result + (scope != null ? scope.hashCode() : 0);
230: result = 37 * result + (name != null ? name.hashCode() : 0);
231: result = 37 * result + (locale != null ? locale.hashCode() : 0);
232: result = 37 * result + (style != null ? style.hashCode() : 0);
233: hash = result;
234: }
235: }
|