/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.templaterender.helper;
import java.awt.*;
import java.lang.reflect.Method;
import java.util.*;
import org.onemind.awtbridge.peer.BridgePeer;
import org.onemind.commons.java.datastructure.InheritableValueMap;
import org.onemind.commons.java.lang.reflect.ReflectUtils;
import org.onemind.swingweb.util.ComponentOptions;
import org.onemind.swingweb.util.HtmlUtils;
public class ComponentAttributeGenerator
{
private Object[] EMPTY_ARGS = new Object[0];
private Map _resolvedConfigs = new HashMap();
private ListenerChecker _listenerChecker = new ListenerChecker();
public String getAttribute(Object obj, ComponentOptions opts) throws Exception
{
InheritableValueMap configs = opts.getOptions("attrs");
Class c = obj.getClass();
Map config = getConfig(c, configs);
StringBuffer sb = new StringBuffer();
Iterator entryIt = config.entrySet().iterator();
while (entryIt.hasNext())
{
Map.Entry entry = (Map.Entry) entryIt.next();
String key = (String) entry.getKey();
if (key.length() > 0 && !key.startsWith("!") && entry.getValue() instanceof String)
{
appendAttribute(sb, obj, key, (String) entry.getValue(), configs, opts);
}
}
return sb.toString();
}
private void appendAttribute(StringBuffer sb, Object obj, String key, String mappedKey, InheritableValueMap configs,
ComponentOptions opts) throws Exception
{
if (key.equals("class"))
{
appendAttribute(sb, "class", obj.getClass().getName());
} else if (key.equals("id"))
{
BridgePeer peer = null;
if (obj instanceof Component)
{
peer = (BridgePeer) ((Component) obj).getPeer();
} else if (obj instanceof MenuComponent)
{
peer = (BridgePeer) ((MenuComponent) obj).getPeer();
}
if (peer != null)
{
appendAttribute(sb, "id", peer.getId());
}
} else if (key.equals("handler"))
{
InheritableValueMap map = opts.getOptions("handler");
String handler = (String) map.resolve(obj.getClass());
appendAttribute(sb, "handler", handler);
} else if (key.equals("font"))
{
if (obj instanceof Component)
{
Component com = (Component) obj;
Font f = com.getFont();
if (f != null)
{
appendAttribute(sb, "fontSize", String.valueOf(f.getSize()));
}
}
} else if (key.equals("events"))
{
InheritableValueMap evtConfigs = opts.getOptions("events");
Iterator it = getConfig(obj.getClass(), evtConfigs).entrySet().iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
if (_listenerChecker.hasListener((Component) obj, (String) entry.getValue()))
{
sb.append("events=\"true\"");
break;
}
}
} else
{
String methodName = "get" + Character.toUpperCase(key.charAt(0)) + key.substring(1);
Method m;
try
{
m = ReflectUtils.getMethod(obj.getClass(), methodName, new Object[0]);
} catch (NoSuchMethodException e)
{
methodName = "is" + Character.toUpperCase(key.charAt(0)) + key.substring(1);
m = ReflectUtils.getMethod(obj.getClass(), methodName, new Object[0]);
}
Object value = m.invoke(obj, EMPTY_ARGS);
if (value != null)
{
if (mappedKey != null)
{
key = mappedKey;
}
if (value instanceof Color)
{
int rgb = ((Color) value).getRGB();
String hexString = (Integer.toHexString(rgb)).substring(2);
appendAttribute(sb, key, "#" + hexString);
} else
{
appendAttribute(sb, key, HtmlUtils.escapeXML(value.toString()));
}
}
}
}
private void appendAttribute(StringBuffer sb, String name, String value)
{
sb.append(name);
sb.append("=\"");
sb.append(value);
sb.append("\" ");
}
public Map getConfig(Class c, InheritableValueMap configs)
{
Collection resolvedConfigs = configs.resolveAll(c);
Map resolved = (Map) _resolvedConfigs.get(resolvedConfigs);
if (resolved != null)
{
return resolved;
}
Iterator it = resolvedConfigs.iterator();
resolved = new LinkedHashMap();
while (it.hasNext())
{
String str = (String) it.next();
String[] attrs = str.split(",");
for (int i = 0; i < attrs.length; i++)
{
String attr = attrs[i].trim();
if (attr.startsWith("!"))
{//exclude
attr = attr.substring(1);
if (!resolved.containsKey(attr.substring(1)))
{
resolved.put(attr, Boolean.FALSE);
}
} else
{//include
String[] mapping = attr.split("=");
if (!resolved.containsKey(mapping[0]) && !resolved.containsKey("!" + mapping[0]))
{
if (mapping.length == 1)
{
resolved.put(mapping[0], mapping[0]);
} else
{
resolved.put(mapping[0], mapping[1]);
}
}
}
}
}
return resolved;
}
}