001: /*
002: * $Id: BodyContainer.java 460546 2006-05-06 09:09:53Z jcompagner $ $Revision:
003: * 5436 $ $Date: 2006-05-06 11:09:53 +0200 (Sat, 06 May 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;
019:
020: import java.io.Serializable;
021:
022: import wicket.AttributeModifier;
023: import wicket.Component;
024: import wicket.markup.html.body.BodyTagAttributeModifier;
025: import wicket.model.IModel;
026: import wicket.model.Model;
027:
028: /**
029: * This is not realy a Container component in the standard Wicket sense. It
030: * rather is a facade which allows to easily add attribute modifier to the
031: * MarkupContainer associated with <body>. That container might be a
032: * Wicket generated one (automatically) or one manually added by a user the
033: * standard Wicket way.
034: * <p>
035: * Container for the page body. This is mostly an internal class that is used
036: * for contributions to the body tag's onload event handler.
037: *
038: * @author jcompagner
039: *
040: * TODO Post 1.2: Change the name. It is not derived from MarkupContainer
041: */
042: public final class BodyContainer implements Serializable {
043: private static final long serialVersionUID = 1L;
044:
045: /** The webpage where the body container is in */
046: private final WebPage page;
047:
048: /** The container id */
049: private final String id;
050:
051: /**
052: * Construct.
053: *
054: * @param page
055: * The webpage where the body container is in
056: * @param id
057: * The container id
058: */
059: public BodyContainer(final WebPage page, final String id) {
060: this .page = page;
061: this .id = id;
062: }
063:
064: /**
065: * Add a new AttributeModifier to the body container which appends the
066: * 'value' to the onLoad attribute of the body tag.
067: * <p>
068: * Note: This method is not suitable for
069: *
070: * @param value
071: * The value to append to 'onLoad'
072: * @return this
073: * @deprecated use {@link #addOnLoadModifier(String, Component)} instead
074: */
075: public final BodyContainer addOnLoadModifier(final String value) {
076: final Model model = new Model(value);
077: final Component bodyContainer = page.get(id);
078:
079: // TODO Post 1.2: Move all attribute modifier into a separate package
080: // and add an AppendingAttributeModifier to that package
081: bodyContainer.add(new AppendingAttributeModifier("onload",
082: model));
083: return this ;
084: }
085:
086: /**
087: * Add a new AttributeModifier to the body container which appends the
088: * 'value' to the onLoad attribute of the body tag.
089: *
090: * @param model
091: * The model that holds the value that must be appended to
092: * 'onLoad'
093: * @return this
094: * @deprecated use {@link #addOnLoadModifier(IModel, Component)} instead
095: */
096: public final BodyContainer addOnLoadModifier(final IModel model) {
097: final Component bodyContainer = page.get(id);
098: bodyContainer.add(new AppendingAttributeModifier("onload",
099: model));
100: return this ;
101: }
102:
103: /**
104: * Add a new AttributeModifier to the body container which appends the
105: * 'value' to the onUnLoad attribute of the body tag.
106: *
107: * @param value
108: * The value to append to 'onUnLoad'
109: * @return this
110: * @deprecated use {@link #addOnUnLoadModifier(String, Component)} instead
111: */
112: public final BodyContainer addOnUnLoadModifier(final String value) {
113: final IModel model = new Model(value);
114: final Component bodyContainer = page.get(id);
115: bodyContainer.add(new AppendingAttributeModifier("onunload",
116: model));
117: return this ;
118: }
119:
120: /**
121: * Add a new AttributeModifier to the body container which appends the value
122: * of the model to the onUnLoad attribute of the body tag.
123: *
124: * @param model
125: * The model which holds the value to be appended to 'onUnLoad'
126: * @return this
127: * @deprecated use {@link #addOnUnLoadModifier(IModel, Component)} instead
128: */
129: public final BodyContainer addOnUnLoadModifier(final IModel model) {
130: final Component bodyContainer = page.get(id);
131: bodyContainer.add(new AppendingAttributeModifier("onunload",
132: model));
133: return this ;
134: }
135:
136: /**
137: * Little helper
138: */
139: public static class AppendingAttributeModifier extends
140: AttributeModifier {
141: private static final long serialVersionUID = 1L;
142:
143: /**
144: * Construct.
145: *
146: * @param attribute
147: * @param replaceModel
148: */
149: public AppendingAttributeModifier(final String attribute,
150: IModel replaceModel) {
151: super (attribute, true, replaceModel);
152: }
153:
154: protected String newValue(final String currentValue,
155: final String replacementValue) {
156: if (currentValue != null
157: && !currentValue.trim().endsWith(";")) {
158: return currentValue + ";" + replacementValue;
159: }
160: return (currentValue == null ? replacementValue
161: : currentValue + replacementValue);
162: }
163: }
164:
165: /**
166: * Get the real body container (WebMarkupContainer)
167: *
168: * @return WebMarkupContainer associated with the <body> tag
169: */
170: public WebMarkupContainer getBodyContainer() {
171: return (WebMarkupContainer) this .page.get(this .id);
172: }
173:
174: /**
175: * Add a new AttributeModifier to the body container which appends the
176: * 'value' to the onLoad attribute of the body tag. Remember the component
177: * which requested to add the modified to the body container. This for
178: * example is required in cases where on a dynamic page the Component (e.g.
179: * a Panel) gets removed and/or replaced and the body attribute modifier
180: * must be removed/replaced as well.
181: *
182: * @param value
183: * The value to append to 'onLoad'
184: * @param behaviorOwner
185: * The component which 'owns' the attribute modifier. Null is a allowed value.
186: * @return this
187: *
188: * @TODO Post 1.2: A listener hook on IBheavior which gets called on removal
189: * of the component would be the better solution
190: */
191: public final BodyContainer addOnLoadModifier(final String value,
192: final Component behaviorOwner) {
193: final IModel model = new Model(value);
194: return addOnLoadModifier(model, behaviorOwner);
195: }
196:
197: /**
198: * Add a new AttributeModifier to the body container which appends the
199: * 'value' to the onLoad attribute of the body tag. Remember the component
200: * which requested to add the modified to the body container. This for
201: * example is required in cases where on a dynamic page the Component (e.g.
202: * a Panel) gets removed and/or replaced and the body attribute modifier
203: * must be removed/replaced as well.
204: *
205: * @param model
206: * The model which holds the value to be appended to 'onLoad'
207: * @param behaviorOwner
208: * The component which 'owns' the attribute modifier. Null is a allowed value.
209: * @return this
210: *
211: * @TODO Post 1.2: A listener hook on IBheavior which gets called on removal
212: * of the component would be the better solution
213: */
214: public final BodyContainer addOnLoadModifier(final IModel model,
215: final Component behaviorOwner) {
216: if (behaviorOwner == null) {
217: return addOnLoadModifier(model);
218: }
219:
220: final Component bodyContainer = page.get(id);
221: bodyContainer.add(new BodyTagAttributeModifier("onload", true,
222: model, behaviorOwner));
223: return this ;
224: }
225:
226: /**
227: * Add a new AttributeModifier to the body container which appends the
228: * 'value' to the onUnLoad attribute of the body tag. Remember the component
229: * which requested to add the modified to the body container. This for
230: * example is required in cases where on a dynamic page the Component (e.g.
231: * a Panel) gets removed and/or replaced and the body attribute modifier
232: * must be removed/replaced as well.
233: *
234: * @param value
235: * The value to append to 'onUnLoad'
236: * @param behaviorOwner
237: * The component which 'owns' the attribute modifier. Null is a allowed value.
238: * @return this
239: *
240: * @TODO Post 1.2: A listener hook on IBehavior which gets called on removal
241: * of the component would be the better solution
242: */
243: public final BodyContainer addOnUnLoadModifier(final String value,
244: final Component behaviorOwner) {
245: final IModel model = new Model(value);
246: return addOnUnLoadModifier(model, behaviorOwner);
247: }
248:
249: /**
250: * Add a new AttributeModifier to the body container which appends the
251: * 'value' to the onUnLoad attribute of the body tag. Remember the component
252: * which requested to add the modified to the body container. This for
253: * example is required in cases where on a dynamic page the Component (e.g.
254: * a Panel) gets removed and/or replaced and the body attribute modifier
255: * must be removed/replaced as well.
256: *
257: * @param model
258: * The model which holds the value to be appended to 'onUnLoad'
259: * @param behaviorOwner
260: * The component which 'owns' the attribute modifier. Null is a allowed value.
261: * @return this
262: *
263: * @TODO Post 1.2: A listener hook on IBheavior which gets called on removal
264: * of the component would be the better solution
265: */
266: public final BodyContainer addOnUnLoadModifier(final IModel model,
267: final Component behaviorOwner) {
268: if (behaviorOwner == null) {
269: return addOnUnLoadModifier(model);
270: }
271:
272: final Component bodyContainer = page.get(id);
273: bodyContainer.add(new BodyTagAttributeModifier("onunload",
274: true, model, behaviorOwner));
275: return this;
276: }
277: }
|