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 */
017package org.apache.wicket.markup.html.form;
018
019import org.apache.wicket.markup.html.WebMarkupContainer;
020import org.apache.wicket.model.IModel;
021import org.apache.wicket.model.IWrapModel;
022
023/**
024 * Default implementation of {@link ILabelProvider}.
025 * 
026 * @author almaw
027 * 
028 */
029public abstract class LabeledWebMarkupContainer extends WebMarkupContainer
030        implements
031                ILabelProvider<String>
032{
033        /**
034         * 
035         */
036        private static final long serialVersionUID = 1L;
037        /**
038         * The value will be made available to the validator property by means of ${label}. It does not
039         * have any specific meaning to FormComponent itself.
040         */
041        private IModel<String> labelModel = null;
042
043        @Override
044        protected void onDetach()
045        {
046                super.onDetach();
047                if (labelModel != null)
048                {
049                        labelModel.detach();
050                        if (labelModel instanceof IWrapModel)
051                        {
052                                ((IWrapModel<?>)labelModel).getWrappedModel().detach();
053                        }
054                }
055        }
056
057        /**
058         * @see org.apache.wicket.Component#Component(String)
059         */
060        public LabeledWebMarkupContainer(final String id)
061        {
062                super(id);
063        }
064
065        /**
066         * @see org.apache.wicket.Component#Component(String, IModel)
067         */
068        public LabeledWebMarkupContainer(final String id, final IModel<?> model)
069        {
070                super(id, model);
071        }
072
073        /**
074         * @see org.apache.wicket.markup.html.form.ILabelProvider#getLabel()
075         */
076        @Override
077        public IModel<String> getLabel()
078        {
079                return labelModel;
080        }
081
082        /**
083         * Provide internal setter. We need this because people want to be able to chain together our
084         * setters in FormComponent, etc. e.g. for .setLabel(foo).setRequired()
085         * 
086         * @param labelModel
087         * @return {@code this}
088         */
089        public LabeledWebMarkupContainer setLabel(IModel<String> labelModel)
090        {
091                this.labelModel = wrap(labelModel);
092                return this;
093        }
094}