[Buix-commits] r208 - trunk/jaxx/src/main/java/jaxx/tags
Author: tchemit Date: 2008-02-12 15:42:33 +0000 (Tue, 12 Feb 2008) New Revision: 208 Modified: trunk/jaxx/src/main/java/jaxx/tags/DefaultComponentHandler.java Log: ajout support i18n dans jaxx :) les attributs text, title et toolTipText sont automatiquement encapsul?\195?\169s par un I18n._("XXX"), sauf si cela est d?\195?\169j?\195?\160 fait dans quel cas, on affiche un warning. Modified: trunk/jaxx/src/main/java/jaxx/tags/DefaultComponentHandler.java =================================================================== --- trunk/jaxx/src/main/java/jaxx/tags/DefaultComponentHandler.java 2008-02-12 15:39:54 UTC (rev 207) +++ trunk/jaxx/src/main/java/jaxx/tags/DefaultComponentHandler.java 2008-02-12 15:42:33 UTC (rev 208) @@ -20,15 +20,19 @@ import java.beans.IntrospectionException; import java.io.IOException; import java.lang.reflect.Field; +import java.util.List; +import java.util.Arrays; public class DefaultComponentHandler extends DefaultObjectHandler { private String containerDelegate; + private static final List<String> I18N_ATTRIBUTES = Arrays.asList("text","title","toolTipText"); public DefaultComponentHandler(ClassDescriptor beanClass) { super(beanClass); - if (!ClassDescriptorLoader.getClassDescriptor(Component.class).isAssignableFrom(beanClass)) { + ClassDescriptorLoader.checkSupportClass(getClass(), beanClass, Component.class); + /*if (!ClassDescriptorLoader.getClassDescriptor(Component.class).isAssignableFrom(beanClass)) { throw new IllegalArgumentException(getClass().getName() + " does not support the class " + beanClass.getName()); - } + }*/ } @Override @@ -82,10 +86,11 @@ protected void openComponent(CompiledObject object, Element tag, JAXXCompiler compiler) throws CompilerException { String constraints = tag.getAttribute("constraints"); - if (constraints != null && constraints.length() > 0) + if (constraints != null && constraints.length() > 0) { compiler.openComponent(object, constraints); - else + } else { compiler.openComponent(object); + } } protected void closeComponent(CompiledObject object, Element tag, JAXXCompiler compiler) throws CompilerException { @@ -100,32 +105,39 @@ @Override public ClassDescriptor getPropertyType(CompiledObject object, String propertyName, JAXXCompiler compiler) throws CompilerException { if (propertyName.equals("x") || propertyName.equals("y") || propertyName.equals("width") || propertyName.equals("height") || - propertyName.equals("font-size")) + "font-size".equals(propertyName)) return ClassDescriptorLoader.getClassDescriptor(Integer.class); - if (propertyName.equals("font-face") || propertyName.equals("font-style") || propertyName.equals("font-weight")) + if (propertyName.equals("font-face") || propertyName.equals("font-style") || propertyName.equals("font-weight")) { return ClassDescriptorLoader.getClassDescriptor(String.class); + } return super.getPropertyType(object, propertyName, compiler); } @Override public String getGetPropertyCode(String id, String name, JAXXCompiler compiler) throws CompilerException { - if (name.equals("font-face")) + if (name.equals("font-face")) { return id + ".getFont().getFontName()"; - if (name.equals("font-size")) + } + if (name.equals("font-size")) { return id + ".getFont().getSize()"; - if (name.equals("font-weight")) + } + if (name.equals("font-weight")) { return "(" + id + ".getFont().getStyle() & Font.BOLD) != 0 ? \"bold\" : \"normal\""; - if (name.equals("font-style")) + } + if (name.equals("font-style")) { return "(" + id + ".getFont().getStyle() & Font.ITALIC) != 0 ? \"italic\" : \"normal\""; + } return super.getGetPropertyCode(id, name, compiler); } @Override public String getSetPropertyCode(String id, String name, String valueCode, JAXXCompiler compiler) throws CompilerException { - if (name.equals("x")) + if (name.equals("x")) { return id + ".setLocation(" + valueCode + ", " + id + ".getY());"; - if (name.equals("y")) + } + if (name.equals("y")) { return id + ".setLocation(" + id + ".getX(), " + valueCode + ");"; + } if (name.equals("width")) { // need to optimize case when both width and height are being assigned return "jaxx.runtime.Util.setComponentWidth(" + id + "," + valueCode + ");\n"; } @@ -139,34 +151,49 @@ return "if (" + id + ".getFont() != null) " + id + ".setFont(" + id + ".getFont().deriveFont((float) " + valueCode + "));"; } if (name.equals("font-weight")) { - if (valueCode.equals("\"bold\"")) + if (valueCode.equals("\"bold\"")) { return "if (" + id + ".getFont() != null) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() | Font.BOLD));"; - if (valueCode.equals("\"normal\"")) + } + if (valueCode.equals("\"normal\"")) { return "if (" + id + ".getFont() != null) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() & ~Font.BOLD));"; - if (!valueCode.startsWith("\"")) + } + if (!valueCode.startsWith("\"")) { return "if (" + id + ".getFont() != null) { if ((" + valueCode + ").equals(\"bold\")) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() | Font.BOLD)); else " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() & ~Font.BOLD)); }"; + } compiler.reportError("font-weight must be either \"normal\" or \"bold\", found " + valueCode); return ""; } if (name.equals("font-style")) { - if (valueCode.equals("\"italic\"")) + if (valueCode.equals("\"italic\"")) { return "if (" + id + ".getFont() != null) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() | Font.ITALIC));"; - if (valueCode.equals("\"normal\"")) + } + if (valueCode.equals("\"normal\"")) { return "if (" + id + ".getFont() != null) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() & ~Font.ITALIC));"; - if (!valueCode.startsWith("\"")) + } + if (!valueCode.startsWith("\"")) { return "if (" + id + ".getFont() != null) { if ((" + valueCode + ").equals(\"italic\")) " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() | Font.ITALIC)); else " + id + ".setFont(" + id + ".getFont().deriveFont(" + id + ".getFont().getStyle() & ~Font.ITALIC)); }"; + } compiler.reportError("font-style must be either \"normal\" or \"italic\", found " + valueCode); return ""; } if (ClassDescriptorLoader.getClassDescriptor(Container.class).isAssignableFrom(getBeanClass()) && name.equals("layout")) { // handle containerDelegate (e.g. contentPane on JFrame) String containerDelegate = (String) getJAXXBeanInfo().getJAXXBeanDescriptor().getValue("containerDelegate"); - if (containerDelegate != null) + if (containerDelegate != null) { return id + '.' + containerDelegate + "().setLayout(" + valueCode + ");"; + } } + // ajout du support i18n + if (I18N_ATTRIBUTES.contains(name)) { + if (valueCode.contains("_(") || valueCode.contains(")")) { + compiler.reportWarning("\n\tjaxx supports i18n, no need to add explicit call to I18n._ for attribute '"+name+"' in component '"+id+"'"); + } else { + valueCode = "_(" + valueCode + ")"; + } + } return super.getSetPropertyCode(id, name, valueCode, compiler); } @@ -187,23 +214,19 @@ */ protected int constantValue(String key, String value) { if ((key.equals("mnemonic") || key.equals("displayedMnemonic"))) { - if (value.length() == 1) + if (value.length() == 1) { return value.charAt(0); - else { - try { - Field vk = java.awt.event.KeyEvent.class.getField(value); - return (Integer) vk.get(null); - } - catch (NoSuchFieldException e) { - throw new IllegalArgumentException("mnemonics must be either a single character or the name of a field in KeyEvent (found: '" + value + "')"); - } - catch (IllegalAccessException e) { - throw new RuntimeException(e); - } } - } else { - return super.constantValue(key, value); + try { + Field vk = java.awt.event.KeyEvent.class.getField(value); + return (Integer) vk.get(null); + } catch (NoSuchFieldException e) { + throw new IllegalArgumentException("mnemonics must be either a single character or the name of a field in KeyEvent (found: '" + value + "')"); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } } + return super.constantValue(key, value); } @@ -219,12 +242,12 @@ if (container) { try { init(); - } - catch (IntrospectionException e) { + if (Boolean.FALSE.equals(getJAXXBeanInfo().getJAXXBeanDescriptor().getValue("isContainer"))) { + container = false; + } + } catch (IntrospectionException e) { throw new RuntimeException(e); } - if (Boolean.FALSE.equals(getJAXXBeanInfo().getJAXXBeanDescriptor().getValue("isContainer"))) - container = false; } return container; } @@ -233,11 +256,10 @@ public String getContainerDelegate() { try { init(); - } - catch (IntrospectionException e) { + return containerDelegate; + } catch (IntrospectionException e) { throw new RuntimeException(e); } - return containerDelegate; } } \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org