/*
* 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.client.core.ui;
import java.util.ArrayList;
import java.util.List;
import org.onemind.swingweb.client.core.AbstractClient;
import org.onemind.swingweb.client.dom.DomHelper;
import org.onemind.swingweb.client.dom.DomNode;
public abstract class AbstractUIHandler implements UIHandler
{
private AbstractClient _client;
public AbstractUIHandler(AbstractClient client)
{
_client = client;
}
public final Object createComponent(Object parent, DomNode element)
{
Object com = createComponentInstance(parent, element);
registerListeners(com);
//use a panel with label to stub
return updateUI(com, element);
}
/**
* {@inheritDoc}
*/
public final Object updateComponent(Object com, DomNode element)
{
return updateUI(com, element);
}
protected abstract Object updateUI(Object com, DomNode node);
protected abstract void registerListeners(Object com);
protected abstract Object createComponentInstance(Object parent, DomNode node);
/**
* {@inheritDoc}
*/
public Object disposeComponent(Object com)
{
//do nothing
return com;
}
protected void postEvent(Object com, Object value, boolean submit)
{
postEvent(com, null, value, submit);
}
protected void postEvent(Object com, String suffix, Object value, boolean submit)
{
String id = _client.getComponentId(com);
String key = (suffix == null) ? id : id + suffix;
_client.getChanges().put(key, value);
if (submit)
{
_client.submitRequest();
}
}
public AbstractClient getClient()
{
return _client;
}
public static boolean isComponentNode(DomNode n)
{
String className = n.getAttribute("class");
if (className != null && className.length() > 0)
{
return true;
} else
{
return false;
}
}
public static DomNode getFirstChildElement(DomNode element)
{
List nodes = element.getChildNodes();
for (int i = 0; i < nodes.size(); i++)
{
DomNode childNode = (DomNode) nodes.get(i);
if (isComponentNode(childNode))
{
return (DomNode) childNode;
}
}
return null;
}
public static DomNode getFirstElementByTag(DomNode element, String tagName)
{
return DomHelper.getFirstElementByTag(element, tagName);
}
public static List getChildrenByTag(DomNode node, String tagName)
{
return DomHelper.getChildrenByTag(node, tagName);
}
public static List getComponentNodes(DomNode node)
{
List result = new ArrayList();
List childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.size(); i++)
{
DomNode childNode = (DomNode) childNodes.get(i);
if (isComponentNode(childNode))
{
result.add(childNode);
}
}
return result;
}
protected int getInt(DomNode node, String attrName)
{
String attr = node.getAttribute(attrName);
if (attr != null && attr.length() > 0)
{
try
{
return Integer.parseInt(attr);
} catch (NumberFormatException e)
{
getClient().log(getClient().LOG_TERSE, e.getMessage() + " on " + attrName + ", value=" + attr);
return 0;
}
} else
{
return -1;
}
}
}