public void applyCss(Object bean, MetaData metaData, Component applyToComponent)
{
String css = metaData.getParameter(PARAM_CSS);
if (!Strings.isEmpty(css)) {
applyToComponent.add(new AttributeAppender("class", new Model<String>(css), " "));
}
String dynamicCssMethod = metaData.getParameter(PARAM_DYNAMIC_CSS);
if (!Strings.isEmpty(dynamicCssMethod)) {
Method method = null;
String cssReturn = null;
try {
method = component.getClass().getMethod(dynamicCssMethod,
new Class[] { beanClass, metaData.getClass() });
}
catch (NoSuchMethodException ex) {
throw new RuntimeException("dynamicCss method " + dynamicCssMethod + "(" + beanClass.getName() + ", "
+ metaData.getClass().getName() + ") is not defined in " + component.getClass());
}
catch (SecurityException ex) {
throw new RuntimeException("securty exception accessing dynamicCss method " + dynamicCssMethod + "("
+ beanClass.getName() + ", " + metaData.getClass().getName() + ") in "
+ component.getClass(), ex);
}
if (bean instanceof IModel) {
bean = ((IModel)bean).getObject();
}
try {
cssReturn = (String)method.invoke(component, new Object[] { bean, metaData });
}
catch (IllegalAccessException ex) {
throw new RuntimeException("access to dynamicCss method " + dynamicCssMethod + "("
+ beanClass.getName() + ", " + metaData.getClass().getName() + ") in "
+ component.getClass() + " is not allowed");
}
catch (IllegalArgumentException ex) {
throw new RuntimeException("illegal arguments for dynamicCss method " + dynamicCssMethod + "("
+ beanClass.getName() + ", " + metaData.getClass().getName() + ") in "
+ component.getClass());
}
catch (InvocationTargetException ex) {
throw new RuntimeException("invocation to dynamicCss method " + dynamicCssMethod + "("
+ beanClass.getName() + ", " + metaData.getClass().getName() + ") in "
+ component.getClass() + " has thrown an exception", ex);
}
if (!Strings.isEmpty(cssReturn)) {
applyToComponent.add(new AttributeAppender("class", new Model<String>(cssReturn), " "));
}
}
}