/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * 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 org.springframework.richclient.factory;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ToStringBuilder;

/**
 * A parameter object for a labelable component; consists of the text, mnemonic,
 * mnemonicIndex, and accelerator that may be associated with a labeled
 * component. This class also acts a factory for producing control prototypes
 * which are preconfigured with a LabelInfo's properties.
 * 
 * @author Keith Donald
 */
public class LabelInfo {
    private static final Log logger = LogFactory.getLog(LabelInfo.class);

    private String text = "";

    private int mnemonic;

    private int mnemonicIndex;

    public LabelInfo(String text) {
        this(text, 0, 0);
    }

    public LabelInfo(String text, int mnemonic) {
        this(text, mnemonic, 0);
    }

    public LabelInfo(String text, int mnemonic, int mnemonicIndex) {
        Assert.hasText(text);
        this.text = text;

        if (logger.isDebugEnabled()) {
            logger.debug("Constructing label info, properties: text='" + text
                    + "', mnemonic=" + mnemonic + ", mnemonicIndex="
                    + mnemonicIndex);
        }
        Assert.isTrue(mnemonic >= 0 && mnemonicIndex >= 0);
        Assert.isTrue(mnemonicIndex < text.length(),
                "The mnemonic index cannot be greater than the text length.");
        this.mnemonic = mnemonic;
        this.mnemonicIndex = mnemonicIndex;
    }

    public int hashCode() {
        return mnemonic + mnemonicIndex + text.hashCode();
    }

    public boolean equals(Object o) {
        if (!(o instanceof LabelInfo)) { return false; }
        LabelInfo info = (LabelInfo)o;
        return text.equals(info.text) && mnemonic == info.mnemonic
                && mnemonicIndex == info.mnemonicIndex;
    }

    public JLabel configureLabel(JLabel label) {
        label.setText(text);
        label.setDisplayedMnemonic(getMnemonic());
        int index = getMnemonicIndex();
        if (index > 0) {
            label.setDisplayedMnemonicIndex(index);
        }
        return label;
    }
    
    public JLabel configureLabelFor(JLabel label, JComponent component) {
        configureLabel(label);
        if (!(component instanceof JPanel)) {
            String labelText = label.getText();
            if (!labelText.endsWith(":")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Appending colon to text field label text '"
                            + text + "'");
                }
                label.setText(labelText += ":");
            }
        }
        label.setLabelFor(component);
        return label;
    }

    public String getText() {
        return text;
    }

    public int getMnemonic() {
        return mnemonic;
    }

    public int getMnemonicIndex() {
        return mnemonicIndex;
    }

    public String toString() {
        return new ToStringBuilder(this).appendProperties().toString();
    }
}
@@ -872,16 +872,61 @@
 Assert;%0A
+import org.springframework.util.StringUtils;%0A
 import o
@@ -1729,15 +1729,15 @@
 ert.
-hasText
+notNull
 (tex
@@ -1766,16 +1766,99 @@
 = text;%0A
+        if (!StringUtils.hasText(text)) %7B%0A            mnemonicIndex = -1;%0A        %7D
 %0A       
@@ -2140,17 +2140,18 @@
 ndex %3E= 
-0
+-1
 );%0A     

allow blank labels