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