Author: tchemit Date: 2008-10-07 18:59:01 +0000 (Tue, 07 Oct 2008) New Revision: 887 Added: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/I18nHelper.java Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/DefaultComponentHandler.java lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/swing/TabHandler.java Log: improve i18n with a dedicated I18nHelper class Added: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/I18nHelper.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/I18nHelper.java (rev 0) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/I18nHelper.java 2008-10-07 18:59:01 UTC (rev 887) @@ -0,0 +1,70 @@ +package jaxx.compiler; + +import jaxx.tags.DefaultComponentHandler; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Arrays; +import java.util.List; + +/** + * I18n methods to add {@link org.codelutin.i18n.I18n#_(String, Object[])} method on some attributes. + * <p/> + * Make sure to set an i18nable compiler to have his {@link CompilerOptions#isI18nable()} returning true. + * + * @author chemit + */ +public class I18nHelper { + + protected static final Log log = LogFactory.getLog(DefaultComponentHandler.class); + + public static final List<String> I18N_ATTRIBUTES = Arrays.asList("text", "title", "toolTipText"); + + /** + * Test if we have an active i18n attribute (says an i18n attribute on a i18neable compiler). + * + * @param attributeName name of attribute to test + * @param compiler current used compiler (contains options) + * @return <code>true</code> if wa have an active i18n attribute, <code>false</code> otherwise. + */ + public static boolean isI18nableAttribute(String attributeName, JAXXCompiler compiler) { + return compiler.getOptions().isI18nable() && isI18nAttribute(attributeName); + } + + /** + * Test if we have an i18n attribute. + * + * @param attributeName name of attribute to test + * @return <code>true</code> if wa have an active i18n attribute, <code>false</code> otherwise. + */ + public static boolean isI18nAttribute(String attributeName) { + return I18N_ATTRIBUTES.contains(attributeName); + } + /** + * Add the i18n on a attribute. + * <p/> + * Note: <b>Be ware : </b> no test is done here to ensure we are on a i18neable attribute for an i18nable compiler. + * <p/> + * Make sure with the method {@link jaxx.compiler.I18nHelper#isI18nableAttribute(String, JAXXCompiler)} returns + * <code>true</code< before using this method. + * + * @param widgetId the id of the widget + * @param attributeName the name of the attribute + * @param attributeValueCode the value code of the attribute value + * @param compiler the current used compile + * @return the surrender i18n call if attribute name is matchrf the attributeValueCode otherwise + */ + public static String addI18nInvocation(String widgetId, String attributeName, String attributeValueCode, JAXXCompiler compiler) { + + if (log.isDebugEnabled()) { + log.debug(" try i18n support for [" + widgetId + ":" + attributeName + "] : " + attributeValueCode); + } + if (attributeValueCode.contains("_(") && attributeValueCode.contains(")")) { + compiler.reportWarning("\n\tjaxx supports i18n, no need to add explicit call to I18n._ for attribute '" + attributeName + "' in component '" + widgetId + "' : ["+attributeValueCode+"]"); + } else { + attributeValueCode = "_(" + attributeValueCode + ")"; + } + + return attributeValueCode; + } +} Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/DefaultComponentHandler.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/DefaultComponentHandler.java 2008-10-07 18:56:33 UTC (rev 886) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/DefaultComponentHandler.java 2008-10-07 18:59:01 UTC (rev 887) @@ -8,6 +8,7 @@ import jaxx.UnsupportedAttributeException; import jaxx.compiler.CompiledObject; import jaxx.compiler.JAXXCompiler; +import jaxx.compiler.I18nHelper; import jaxx.reflect.ClassDescriptor; import jaxx.reflect.ClassDescriptorLoader; import org.apache.commons.logging.Log; @@ -22,8 +23,6 @@ import java.beans.IntrospectionException; import java.io.IOException; import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.List; public class DefaultComponentHandler extends DefaultObjectHandler { /** log */ @@ -31,8 +30,6 @@ private String containerDelegate; - private static final List<String> I18N_ATTRIBUTES = Arrays.asList("text", "title", "toolTipText"); - public DefaultComponentHandler(ClassDescriptor beanClass) { super(beanClass); ClassDescriptorLoader.checkSupportClass(getClass(), beanClass, Component.class); @@ -203,34 +200,14 @@ } } // ajout du support i18n - valueCode = addI18nInvocation(id, name, valueCode, compiler); + if (I18nHelper.isI18nableAttribute(name,compiler)) { + valueCode = I18nHelper.addI18nInvocation(id, name, valueCode, compiler); + } return super.getSetPropertyCode(id, name, valueCode, compiler); } - /** - * - * @param widgetId the id of the widget - * @param attributeName the name of the attribute - * @param attributeValueCode the value code of the attribute value - * @param compiler the current used compile - * @return the surrender i18n call if attribute name is matchrf the attributeValueCode otherwise - */ - public static String addI18nInvocation(String widgetId, String attributeName, String attributeValueCode, JAXXCompiler compiler) { - if (I18N_ATTRIBUTES.contains(attributeName)) { - if (log.isDebugEnabled()) { - log.debug(" try i18n support for [" + widgetId + ":" + attributeName + "] : " + attributeValueCode); - } - if (attributeValueCode.contains("_(") || attributeValueCode.contains(")")) { - compiler.reportWarning("\n\tjaxx supports i18n, no need to add explicit call to I18n._ for attribute '" + attributeName + "' in component '" + widgetId + "'"); - } else { - attributeValueCode = "_(" + attributeValueCode + ")"; - } - } - return attributeValueCode; - } - /** * Maps string values onto integers, so that int-valued enumeration properties can be specified by strings. For * example, when passed a key of 'alignment', this method should normally map the values 'left', 'center', and Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/swing/TabHandler.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/swing/TabHandler.java 2008-10-07 18:56:33 UTC (rev 886) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/tags/swing/TabHandler.java 2008-10-07 18:59:01 UTC (rev 887) @@ -7,10 +7,10 @@ import jaxx.CompilerException; import jaxx.compiler.CompiledObject; import jaxx.compiler.JAXXCompiler; +import jaxx.compiler.I18nHelper; import jaxx.reflect.ClassDescriptorLoader; import jaxx.runtime.swing.TabInfo; import jaxx.tags.TagHandler; -import jaxx.tags.DefaultComponentHandler; import jaxx.types.TypeManager; import org.w3c.dom.Attr; import org.w3c.dom.Element; @@ -63,16 +63,20 @@ return; } + String valueCode = TypeManager.getJavaCode(value); + // add i18n support - String i18nvalue = DefaultComponentHandler.addI18nInvocation(id,name,TypeManager.getJavaCode(value),compiler); + if (I18nHelper.isI18nableAttribute(name,compiler)) { + value = valueCode = I18nHelper.addI18nInvocation(id,name,valueCode,compiler); + } if (name.equals("title")) { - tabInfo.setTitle(i18nvalue); - compiledTabInfo.appendInitializationCode(id + ".setTitle(" + i18nvalue + ");"); + tabInfo.setTitle(value); + compiledTabInfo.appendInitializationCode(id + ".setTitle(" + valueCode + ");"); //compiledTabInfo.appendInitializationCode(id + ".setTitle(" + TypeManager.getJavaCode(value) + ");"); } else if (name.equals("toolTipText")) { - tabInfo.setToolTipText(i18nvalue); - compiledTabInfo.appendInitializationCode(id + ".setToolTipText(" + i18nvalue + ");"); + tabInfo.setToolTipText(value); + compiledTabInfo.appendInitializationCode(id + ".setToolTipText(" + valueCode + ");"); //compiledTabInfo.appendInitializationCode(id + ".setToolTipText(" + TypeManager.getJavaCode(value) + ");"); } else if (name.equals("icon")) { Icon icon = (Icon) TypeManager.convertFromString(value, Icon.class);