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;
022:
023: import java.util.*;
024: import org.onemind.swingweb.client.core.ui.EventInterceptor;
025: import org.onemind.swingweb.client.core.ui.UIHandler;
026: import org.onemind.swingweb.client.dom.DomNode;
027:
028: public abstract class AbstractClient {
029:
030: public static final String LOG_TERSE = "terse",
031: LOG_VERBOSE = "verbose", LOG_DEBUG = "debug";
032:
033: private String _url;
034:
035: private Map _components = new HashMap();
036:
037: private Map _changes = new HashMap();
038:
039: private Map _handlers = new HashMap();
040:
041: private Set _logLevels = new HashSet();
042:
043: private String _pregenId;
044:
045: private Map _eventInterceptors = new HashMap();
046:
047: public AbstractClient(String url) {
048: _url = url;
049: }
050:
051: public UIHandler getHandlerByName(String name) {
052: return (UIHandler) _handlers.get(name);
053: }
054:
055: public void addHandler(UIHandler handler) {
056: _handlers.put(handler.getName(), handler);
057: }
058:
059: public void addComponent(String id, Object com) {
060: if (_pregenId != null && _pregenId.startsWith(id)) {
061: id = _pregenId;
062: }
063: if (_components.containsKey(id)) {
064: throw new IllegalArgumentException("Component id " + id
065: + " is not unique");
066: }
067: _components.put(id, com);
068: }
069:
070: public Object getComponent(String id) {
071: return _components.get(id);
072: }
073:
074: public String getComponentId(Object com) {
075: Iterator it = _components.entrySet().iterator();
076: while (it.hasNext()) {
077: Map.Entry entry = (Map.Entry) it.next();
078: if (entry.getValue() == com) {
079: return (String) entry.getKey();
080: }
081: }
082: return null;
083: }
084:
085: public void setPregenId(String id) {
086: _pregenId = id;
087: }
088:
089: public Map getChanges() {
090: return _changes;
091: }
092:
093: public void removeComponent(String id) {
094: _components.remove(id);
095: }
096:
097: public String getUrl() {
098: return _url;
099: }
100:
101: public void setLogLevel(String logLevel, boolean b) {
102: if (b) {
103: _logLevels.add(logLevel);
104: } else {
105: _logLevels.remove(logLevel);
106: }
107: }
108:
109: public boolean isLoggable(String logLevel) {
110: return _logLevels.contains(logLevel);
111: }
112:
113: public final Object handle(Object parent, DomNode element) {
114: return handle(parent, element, element.getAttribute("id"));
115: }
116:
117: public final void setEventInterceptor(Object widget,
118: EventInterceptor intc) {
119: _eventInterceptors.put(widget, intc);
120: }
121:
122: public EventInterceptor getEventInterceptor(Object widget) {
123: return (EventInterceptor) _eventInterceptors.get(widget);
124: }
125:
126: public final void removeEventInterceptor(Object widget) {
127: _eventInterceptors.remove(widget);
128: }
129:
130: public void purgeSession() {
131: _eventInterceptors.clear();
132: _components.clear();
133: _changes.clear();
134: }
135:
136: public abstract Object handle(Object parent, DomNode element,
137: String id);
138:
139: public abstract void refreshUI() throws Exception;
140:
141: public abstract void log(String logLevel, String msg);
142:
143: public abstract void submitRequest();
144: }
|