001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.behavior;
018:
019: import org.apache.wicket.RequestCycle;
020: import org.apache.wicket.ResourceReference;
021: import org.apache.wicket.markup.html.IHeaderContributor;
022: import org.apache.wicket.markup.html.IHeaderResponse;
023: import org.apache.wicket.markup.html.resources.CompressedResourceReference;
024:
025: /**
026: * A {@link org.apache.wicket.behavior.AbstractHeaderContributor} behavior that is
027: * specialized on package resources. If you use this class, you have to
028: * pre-register the resources you want to contribute. A shortcut for common
029: * cases is to call {@link #forCss(Class, String)} to contribute a package css
030: * file or {@link #forJavaScript(Class, String)} to contribute a packaged
031: * javascript file. For instance:
032: *
033: * <pre>
034: * add(HeaderContributor.forCss(MyPanel.class, "mystyle.css"));
035: * </pre>
036: *
037: * @author Eelco Hillenius
038: * @author Matej Knopp
039: */
040: public class HeaderContributor extends AbstractHeaderContributor {
041: private static final long serialVersionUID = 1L;
042:
043: /**
044: * Returns a new instance of {@link HeaderContributor} with a header
045: * contributor that references a CSS file that lives in a package.
046: *
047: * @param scope
048: * The scope of the package resource (typically the class of the
049: * caller, or a class that lives in the package where the
050: * resource lives).
051: * @param path
052: * The path
053: * @return the new header contributor instance
054: */
055: public static final HeaderContributor forCss(final Class scope,
056: final String path) {
057: return new HeaderContributor(new IHeaderContributor() {
058: private static final long serialVersionUID = 1L;
059:
060: public void renderHead(IHeaderResponse response) {
061: response
062: .renderCSSReference(new CompressedResourceReference(
063: scope, path));
064: }
065: });
066: }
067:
068: /**
069: * Returns a new instance of {@link HeaderContributor} with a header
070: * contributor that references a CSS file that lives in a package.
071: *
072: * @param scope
073: * The scope of the package resource (typically the class of the
074: * caller, or a class that lives in the package where the
075: * resource lives).
076: * @param path
077: * The path
078: * @param media
079: * The media type for this CSS ("print", "screen", etc.)
080: * @return the new header contributor instance
081: */
082: public static final HeaderContributor forCss(final Class scope,
083: final String path, final String media) {
084: return new HeaderContributor(new IHeaderContributor() {
085: private static final long serialVersionUID = 1L;
086:
087: public void renderHead(IHeaderResponse response) {
088: response.renderCSSReference(
089: new CompressedResourceReference(scope, path),
090: media);
091: }
092: });
093: }
094:
095: /**
096: * Returns a new instance of {@link HeaderContributor} with a header
097: * contributor that references a CSS file that lives in a package.
098: *
099: * @param reference
100: *
101: * @return the new header contributor instance
102: */
103: public static final HeaderContributor forCss(
104: final ResourceReference reference) {
105: return new HeaderContributor(new IHeaderContributor() {
106: private static final long serialVersionUID = 1L;
107:
108: public void renderHead(IHeaderResponse response) {
109: response.renderCSSReference(reference);
110: }
111: });
112: }
113:
114: /**
115: * Returns a new instance of {@link HeaderContributor} with a header
116: * contributor that references a CSS file that lives in a package.
117: *
118: * @param reference
119: * @param media
120: * The media type for this CSS ("print", "screen", etc.)
121: * @return the new header contributor instance
122: */
123: public static final HeaderContributor forCss(
124: final ResourceReference reference, final String media) {
125: return new HeaderContributor(new IHeaderContributor() {
126: private static final long serialVersionUID = 1L;
127:
128: public void renderHead(IHeaderResponse response) {
129: response.renderCSSReference(reference, media);
130: }
131: });
132: }
133:
134: /**
135: * Returns a new instance of {@link HeaderContributor} with a header
136: * contributor referencing a CSS file using one of the following schemes:
137: * <ul>
138: * <li>Starts with http:// or https:// for an external reference.</li>
139: * <li>Starts with "/" for an absolute reference that Wicket will not
140: * rewrite.</li>
141: * <li>Starts with anything else, which Wicket will automatically prepend
142: * to make relative to the context root of your web-app.</li>
143: * </ul>
144: *
145: * @param location
146: * The location of the css file.
147: * @return the new header contributor instance
148: */
149: public static final HeaderContributor forCss(final String location) {
150: return new HeaderContributor(new IHeaderContributor() {
151: private static final long serialVersionUID = 1L;
152:
153: public void renderHead(IHeaderResponse response) {
154: response
155: .renderCSSReference(returnRelativePath(location));
156: }
157: });
158: }
159:
160: /**
161: * Returns a new instance of {@link HeaderContributor} with a header
162: * contributor referencing a CSS file using one of the following schemes:
163: * <ul>
164: * <li>Starts with http:// or https:// for an external reference.</li>
165: * <li>Starts with "/" for an absolute reference that Wicket will not
166: * rewrite.</li>
167: * <li>Starts with anything else, which Wicket will automatically prepend
168: * to make relative to the context root of your web-app.</li>
169: * </ul>
170: *
171: * @param location
172: * The location of the css.
173: * @param media
174: * The media type for this CSS ("print", "screen", etc.)
175: * @return the new header contributor instance
176: */
177: public static final HeaderContributor forCss(final String location,
178: final String media) {
179: return new HeaderContributor(new IHeaderContributor() {
180: private static final long serialVersionUID = 1L;
181:
182: public void renderHead(IHeaderResponse response) {
183: response.renderCSSReference(
184: returnRelativePath(location), media);
185: }
186: });
187: }
188:
189: /**
190: * Returns a new instance of {@link HeaderContributor} with a header
191: * contributor that references a java script file that lives in a package.
192: *
193: * @param scope
194: * The scope of the package resource (typically the class of the
195: * caller, or a class that lives in the package where the
196: * resource lives).
197: * @param path
198: * The path
199: * @return the new header contributor instance
200: */
201: public static final HeaderContributor forJavaScript(
202: final Class scope, final String path) {
203: return new HeaderContributor(new IHeaderContributor() {
204: private static final long serialVersionUID = 1L;
205:
206: public void renderHead(IHeaderResponse response) {
207: response
208: .renderJavascriptReference(new CompressedResourceReference(
209: scope, path));
210: }
211: });
212: }
213:
214: /**
215: * Returns a new instance of {@link HeaderContributor} with a header
216: * contributor that references a java script file that lives in a package.
217: *
218: * @param reference
219: *
220: * @return the new header contributor instance
221: */
222: public static final HeaderContributor forJavaScript(
223: final ResourceReference reference) {
224: return new HeaderContributor(new IHeaderContributor() {
225: private static final long serialVersionUID = 1L;
226:
227: public void renderHead(IHeaderResponse response) {
228: response.renderJavascriptReference(reference);
229: }
230: });
231: }
232:
233: /**
234: * Returns a new instance of {@link HeaderContributor} with a header
235: * contributor referencing a CSS file using one of the following schemes:
236: * <ul>
237: * <li>Starts with http:// or https:// for an external reference.</li>
238: * <li>Starts with "/" for an absolute reference that Wicket will not
239: * rewrite.</li>
240: * <li>Starts with anything else, which Wicket will automatically prepend
241: * to make relative to the context root of your web-app.</li>
242: * </ul>
243: *
244: * @param location
245: * The location of the css file.
246: * @return the new header contributor instance
247: */
248: public static final HeaderContributor forJavaScript(
249: final String location) {
250: return new HeaderContributor(new IHeaderContributor() {
251: private static final long serialVersionUID = 1L;
252:
253: public void renderHead(IHeaderResponse response) {
254: response
255: .renderJavascriptReference(returnRelativePath(location));
256: }
257: });
258: }
259:
260: // Adds ../ links to make the location relative to the root of the webapp,
261: // provided it's not a fully-qualified URL.
262: private static final String returnRelativePath(String location) {
263: // WICKET-59 allow external URLs, WICKET-612 allow absolute URLs.
264: if (location.startsWith("http://")
265: || location.startsWith("https://")
266: || location.startsWith("/")) {
267: return location;
268: } else {
269: return RequestCycle.get().getRequest()
270: .getRelativePathPrefixToContextRoot()
271: + location;
272: }
273: }
274:
275: /**
276: * Resource reference to contribute.
277: */
278: private IHeaderContributor headerContributor = null;
279:
280: /**
281: * Construct.
282: *
283: * @param headerContributor
284: * the header contributor
285: */
286: public HeaderContributor(IHeaderContributor headerContributor) {
287: if (headerContributor == null) {
288: throw new IllegalArgumentException(
289: "header contributor may not be null");
290: }
291: this .headerContributor = headerContributor;
292: }
293:
294: /**
295: * @see org.apache.wicket.behavior.AbstractHeaderContributor#getHeaderContributors()
296: */
297: public final IHeaderContributor[] getHeaderContributors() {
298: return new IHeaderContributor[] { headerContributor };
299: }
300: }
|