package org.valkyriercp.form.builder;

import org.springframework.context.MessageSource;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.valkyriercp.binding.form.FormModel;

import javax.swing.*;
import java.util.Locale;

/**
 * <code>FormComponentInterceptor</code> that allows customization on how a CheckBox form property is rendered.
 *
 * @see CheckBoxFormComponentInterceptorFactory for more details on configuration
 *
 * @author Peter De Bruycker
 */
public class CheckBoxFormComponentInterceptor extends AbstractFormComponentInterceptor {

	private boolean showLabel = true;
	private boolean showText = false;

	private String textKey = "text";

	private MessageSource messageSource;

	public CheckBoxFormComponentInterceptor(FormModel formModel, MessageSource messageSource, boolean showLabel,
			boolean showText, String textKey) {
		super(formModel);
		this.messageSource = messageSource;
		this.showLabel = showLabel;
		this.showText = showText;
		this.textKey = textKey;
	}

	public void processComponent(String propertyName, JComponent component) {
		if (component instanceof JCheckBox) {
			if (showText) {
				JCheckBox checkBox = (JCheckBox) component;

				checkBox.setText(fetchExtraText(messageSource, propertyName));
			}
		}
	}

	protected String fetchExtraText(MessageSource messageSource, String propertyName) {
		return messageSource.getMessage(new DefaultMessageSourceResolvable(getExtraTextKeys(propertyName), propertyName
				+ "." + textKey), Locale.getDefault());
	}

	/**
	 * Returns the keys used to fetch the extra text from the <code>MessageSource</code>.
	 * <p>
	 * The keys returned are
	 * <code>&lt;formModelId&gt;.&lt;propertyName&gt;.&lt;textKey&gt;, &lt;propertyName&gt;.&lt;textKey&gt;, &lt;textKey&gt;</code>
	 * <p>
	 * Can safely be overridden to add extra keys
	 *
	 * @param propertyName
	 *            the property name
	 * @return the keys
	 */
	protected String[] getExtraTextKeys(String propertyName) {
		return new String[] { getFormModel().getId() + "." + propertyName + "." + textKey,
				propertyName + "." + textKey, textKey };
	}

	protected boolean isCheckBoxProperty(String propertyName) {
		Class type = getFormModel().getFieldMetadata(propertyName).getPropertyType();

		return Boolean.TYPE.equals(type) || Boolean.class.equals(type);
	}

	public void processLabel(String propertyName, JComponent label) {
		if (!showLabel && isCheckBoxProperty(propertyName)) {
			((JLabel) label).setText("");
		}
	}
}


@@ -32,16 +32,260 @@
 ilder;%0A%0A
+import java.util.Locale;%0A%0Aimport javax.swing.JCheckBox;%0Aimport javax.swing.JComponent;%0Aimport javax.swing.JLabel;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Configurable;%0A
 import o
@@ -453,56 +453,8 @@
 l;%0A%0A
-import javax.swing.*;%0Aimport java.util.Locale;%0A%0A
 /**%0A
@@ -528,16 +528,19 @@
 on how a
+%0A *
  CheckBo
@@ -570,16 +570,17 @@
 ered.%0A *
+ 
 %0A * @see
@@ -639,16 +639,24 @@
 tails on
+%0A *     
  configu
@@ -664,16 +664,17 @@
 ation%0A *
+ 
 %0A * @aut
@@ -699,16 +699,30 @@
 ker%0A */%0A
+@Configurable%0A
 public c
@@ -766,17 +766,19 @@
  extends
- 
+%0A%09%09
 Abstract
@@ -911,16 +911,28 @@
 text%22;%0A%0A
+%09@Autowired%0A
 %09private
@@ -1023,17 +1023,20 @@
 rmModel,
- 
+%0A%09%09%09
 MessageS
@@ -1074,20 +1074,17 @@
 owLabel,
-%0A%09%09%09
+ 
 boolean 
@@ -1092,17 +1092,20 @@
 howText,
- 
+%0A%09%09%09
 String t
@@ -1584,17 +1584,20 @@
 eSource,
- 
+%0A%09%09%09
 String p
@@ -1680,16 +1680,21 @@
 olvable(
+%0A%09%09%09%09
 getExtra
@@ -1729,21 +1729,17 @@
 ertyName
-%0A%09%09%09%09
+ 
 + %22.%22 + 
@@ -1747,17 +1747,21 @@
 extKey),
- 
+%0A%09%09%09%09
 Locale.g
@@ -1841,16 +1841,20 @@
 from the
+%0A%09 *
  %3Ccode%3EM
@@ -2095,16 +2095,17 @@
 keys%0A%09 *
+ 
 %0A%09 * @pa
@@ -2263,17 +2263,21 @@
 ring%5B%5D %7B
- 
+%0A%09%09%09%09
 getFormM
@@ -2499,16 +2499,21 @@
 rtyName)
+%0A%09%09%09%09
 .getProp
@@ -2760,9 +2760,8 @@
 %09%7D%0A%09%7D%0A%7D%0A
-%0A

Support new'ing via @Configurable