001: /*
002: * $Id: Label.java 457783 2005-10-02 10:06:33Z jcompagner $ $Revision:
003: * 1.13 $ $Date: 2005-10-02 12:06:33 +0200 (Sun, 02 Oct 2005) $
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.basic;
019:
020: import wicket.markup.ComponentTag;
021: import wicket.markup.MarkupStream;
022: import wicket.markup.html.WebComponent;
023: import wicket.model.IModel;
024: import wicket.model.Model;
025:
026: /**
027: * A Label component replaces its body with the String version of its model
028: * object returned by getModelObjectAsString().
029: * <p>
030: * Exactly what is displayed as the body, depends on the model. The simplest
031: * case is a Label with a static String model, which can be constructed like
032: * this:
033: *
034: * <pre>
035: * add(new Label("myLabel", "the string to display"))
036: * </pre>
037: *
038: * A Label with a dynamic model can be created like this:
039: *
040: * <pre>
041: *
042: * add(new Label("myLabel", new PropertyModel(person, "name"));
043: *
044: * </pre>
045: *
046: * In this case, the Label component will replace the body of the tag it is
047: * attached to with the 'name' property of the given Person object, where Person
048: * might look like:
049: *
050: * <pre>
051: * public class Person
052: * {
053: * private String name;
054: *
055: * public String getName()
056: * {
057: * return name;
058: * }
059: *
060: * public void setName(String name)
061: * {
062: * this.name = name;
063: * }
064: * }
065: * </pre>
066: *
067: * @author Jonathan Locke
068: */
069: public class Label extends WebComponent {
070: private static final long serialVersionUID = 1L;
071:
072: /**
073: * Constructor
074: *
075: * @param id
076: * See Component
077: */
078: public Label(final String id) {
079: super (id);
080: }
081:
082: /**
083: * Convenience constructor. Same as Label(String, new Model(String))
084: *
085: * @param id
086: * See Component
087: * @param label
088: * The label text
089: *
090: * @see wicket.Component#Component(String, IModel)
091: */
092: public Label(final String id, String label) {
093: this (id, new Model(label));
094: }
095:
096: /**
097: * @see wicket.Component#Component(String, IModel)
098: */
099: public Label(final String id, IModel model) {
100: super (id, model);
101: }
102:
103: /**
104: * @see wicket.Component#onComponentTagBody(wicket.markup.MarkupStream,
105: * wicket.markup.ComponentTag)
106: */
107: protected void onComponentTagBody(final MarkupStream markupStream,
108: final ComponentTag openTag) {
109: replaceComponentTagBody(markupStream, openTag,
110: getModelObjectAsString());
111: }
112: }
|