001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.core.ui;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025: import org.onemind.swingweb.client.core.AbstractClient;
026: import org.onemind.swingweb.client.dom.DomHelper;
027: import org.onemind.swingweb.client.dom.DomNode;
028:
029: public abstract class AbstractUIHandler implements UIHandler {
030:
031: private AbstractClient _client;
032:
033: public AbstractUIHandler(AbstractClient client) {
034: _client = client;
035: }
036:
037: public final Object createComponent(Object parent, DomNode element) {
038: Object com = createComponentInstance(parent, element);
039: registerListeners(com);
040: //use a panel with label to stub
041: return updateUI(com, element);
042: }
043:
044: /**
045: * {@inheritDoc}
046: */
047: public final Object updateComponent(Object com, DomNode element) {
048: return updateUI(com, element);
049: }
050:
051: protected abstract Object updateUI(Object com, DomNode node);
052:
053: protected abstract void registerListeners(Object com);
054:
055: protected abstract Object createComponentInstance(Object parent,
056: DomNode node);
057:
058: /**
059: * {@inheritDoc}
060: */
061: public Object disposeComponent(Object com) {
062: //do nothing
063: return com;
064: }
065:
066: protected void postEvent(Object com, Object value, boolean submit) {
067: postEvent(com, null, value, submit);
068: }
069:
070: protected void postEvent(Object com, String suffix, Object value,
071: boolean submit) {
072: String id = _client.getComponentId(com);
073: String key = (suffix == null) ? id : id + suffix;
074: _client.getChanges().put(key, value);
075: if (submit) {
076: _client.submitRequest();
077: }
078: }
079:
080: public AbstractClient getClient() {
081: return _client;
082: }
083:
084: public static boolean isComponentNode(DomNode n) {
085: String className = n.getAttribute("class");
086: if (className != null && className.length() > 0) {
087: return true;
088: } else {
089: return false;
090: }
091: }
092:
093: public static DomNode getFirstChildElement(DomNode element) {
094: List nodes = element.getChildNodes();
095: for (int i = 0; i < nodes.size(); i++) {
096: DomNode childNode = (DomNode) nodes.get(i);
097: if (isComponentNode(childNode)) {
098: return (DomNode) childNode;
099: }
100: }
101: return null;
102: }
103:
104: public static DomNode getFirstElementByTag(DomNode element,
105: String tagName) {
106: return DomHelper.getFirstElementByTag(element, tagName);
107: }
108:
109: public static List getChildrenByTag(DomNode node, String tagName) {
110: return DomHelper.getChildrenByTag(node, tagName);
111: }
112:
113: public static List getComponentNodes(DomNode node) {
114: List result = new ArrayList();
115: List childNodes = node.getChildNodes();
116: for (int i = 0; i < childNodes.size(); i++) {
117: DomNode childNode = (DomNode) childNodes.get(i);
118: if (isComponentNode(childNode)) {
119: result.add(childNode);
120: }
121: }
122: return result;
123: }
124:
125: protected int getInt(DomNode node, String attrName) {
126: String attr = node.getAttribute(attrName);
127: if (attr != null && attr.length() > 0) {
128: try {
129: return Integer.parseInt(attr);
130: } catch (NumberFormatException e) {
131: getClient().log(
132: getClient().LOG_TERSE,
133: e.getMessage() + " on " + attrName + ", value="
134: + attr);
135: return 0;
136: }
137: } else {
138: return -1;
139: }
140: }
141: }
|