/*
 * Copyright 2010 55 Minutes (http://www.55minutes.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fiftyfive.wicket.basic;


import fiftyfive.wicket.util.Shortcuts;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;


// TODO: use I18N?

/**
 * An extension of Wicket's Label component that allows a default value to be
 * specified. If the model is empty, then:
 * <ul>
 * <li>A CSS class of "empty" will be added to the tag</li>
 * <li>The default value (if one is specified) will be emitted as the
 * body of the tag</li>
 * </ul>
 * If the model is not empty, this behaves like the standard Label.
 * @see #setDefault
 * @see Label
 */
public class LabelWithDefault extends Label
{
    private String _defaultValue;
    
    /**
     * @see Label#Label(String)
     */
    public LabelWithDefault(final String id)
    {
        this(id, (IModel) null);
    }

    /**
     * @see Label#Label(String, String)
     */
    public LabelWithDefault(final String id, String label)
    {
        this(id, new Model(label));
    }

    /**
     * @see Label#Label(String, IModel)
     */
    public LabelWithDefault(final String id, IModel<?> model)
    {
        super(id, model);
        add(Shortcuts.cssClassIf("empty", this, "empty"));
    }
    
    /**
     * Sets the value that will be used if the value provided by the
     * model is empty.
     */
    public LabelWithDefault setDefault(String valueIfEmpty)
    {
        _defaultValue = valueIfEmpty;
        return this;
    }
    
    /**
     * Returns <code>true</code> if the model of this label is empty.
     * @see Shortcuts#empty
     */
    public boolean isEmpty()
    {
        return Shortcuts.empty(getDefaultModelObject());
    }

    /**
     * Renders the label using the string value of the model as the tag body.
     * If the value of the model is empty and default was provided by
     * {@link #setDefault}, use the default instead.
     * @see Shortcuts#empty
     */
    @Override
    protected void onComponentTagBody(MarkupStream markupStream,
                                      ComponentTag openTag)
    {
        String str = getDefaultModelObjectAsString();
        if(_defaultValue != null && isEmpty())
        {
            str = _defaultValue;
        }
        replaceComponentTagBody(markupStream, openTag, str);
    }
}

@@ -649,17 +649,16 @@
 basic;%0A%0A
-%0A
 import f
@@ -689,17 +689,16 @@
 rtcuts;%0A
-%0A
 import o
@@ -914,28 +914,8 @@
 ;%0A%0A%0A
-// TODO: use I18N?%0A%0A
 /**%0A
@@ -1370,22 +1370,30 @@
 private 
+IModel%3C
 String
+%3E
  _defaul
@@ -2094,32 +2094,381 @@
 eIfEmpty)%0A    %7B%0A
+        return setDefault(Model.of(valueIfEmpty));%0A    %7D%0A    %0A    /**%0A     * Sets the value that will be used if the value provided by the label's%0A     * normal model is empty. For localization, consider passing in an%0A     * instance of Wicket's StringResourceModel.%0A     */%0A    public LabelWithDefault setDefault(IModel%3CString%3E valueIfEmpty)%0A    %7B%0A
         _default
@@ -2724,32 +2724,249 @@
 Object());%0A    %7D
+%0A    %0A    /**%0A     * Detaches the model that holds the default value.%0A     */%0A    @Override%0A    protected void onDetach()%0A    %7B%0A        if(null != _defaultValue) _defaultValue.detach();%0A        super.onDetach();%0A    %7D
 %0A%0A    /**%0A     *
@@ -3448,24 +3448,24 @@
 )%0A        %7B%0A
-
             
@@ -3479,24 +3479,36 @@
 defaultValue
+.getObject()
 ;%0A        %7D%0A

Enhance API to allow localization of default values.