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.awt;
022:
023: import java.awt.Component;
024: import java.io.ByteArrayInputStream;
025: import java.io.IOException;
026: import java.util.*;
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import org.apache.commons.httpclient.*;
030: import org.apache.commons.httpclient.methods.GetMethod;
031: import org.onemind.swingweb.client.awt.ui.ComponentUIHandler;
032: import org.onemind.swingweb.client.core.AbstractClient;
033: import org.onemind.swingweb.client.core.ui.UIHandler;
034: import org.onemind.swingweb.client.dom.DomNode;
035: import org.w3c.dom.*;
036:
037: public class SwingWebClient extends AbstractClient implements Logger {
038:
039: private Console _console;
040:
041: private HttpClient _client;
042:
043: private Logger _logger;
044:
045: private UIHandler _defaultHandler = new ComponentUIHandler(this );
046:
047: int _requestId = 0;
048:
049: public SwingWebClient(String url) {
050: super (url);
051: _client = new HttpClient();
052: _console = new Console(url, this );
053: _console.pack();
054: _console.setSize(600, 300);
055: _console.setVisible(true);
056: }
057:
058: public void setLogger(Logger logger) {
059: _logger = logger;
060: }
061:
062: public Document sendRequest(HttpMethod request) throws IOException {
063: try {
064: int statusCode = _client.executeMethod(request);
065: if (statusCode != HttpStatus.SC_OK) {
066: System.err.println("Method failed: "
067: + request.getStatusLine());
068: return null;
069: } else {
070: byte[] responseBody = request.getResponseBody();
071: log(_requestId, new String(responseBody));
072: DocumentBuilderFactory factory = DocumentBuilderFactory
073: .newInstance();
074: try {
075: DocumentBuilder builder = factory
076: .newDocumentBuilder();
077: Document document = builder
078: .parse(new ByteArrayInputStream(
079: responseBody));
080: return document;
081: } catch (Exception spe) {
082: spe.printStackTrace();
083: return null;
084: }
085: }
086: } finally {
087: request.releaseConnection();
088: }
089: }
090:
091: public void refreshUI() throws IOException {
092: GetMethod method = new GetMethod(getUrl());
093: Document doc = sendRequest(method);
094: _handleDocument(doc);
095: }
096:
097: public Object handle(Object parent, DomNode element, String id) {
098: String className = element.getAttribute("class");
099: String handlerName = element.getAttribute("handler");
100: UIHandler handler = (UIHandler) getHandlerByName(handlerName);
101: if (handler == null) {
102: handler = _defaultHandler;
103: System.out.println("Unable to handle " + className);
104: }
105: Object com = null;
106: if (getComponent(id) != null) {
107: com = handler.updateComponent(getComponent(id), element);
108: } else {
109: com = handler.createComponent(parent, element);
110: addComponent(id, com);
111: }
112: return com;
113: }
114:
115: public void submitRequest() {
116: try {
117: GetMethod method = new GetMethod(getUrl());
118: List nvPairs = new ArrayList();
119: Iterator it = getChanges().entrySet().iterator();
120: while (it.hasNext()) {
121: Map.Entry entry = (Map.Entry) it.next();
122: if (entry.getValue() instanceof String[]) {
123: String[] nvs = (String[]) entry.getValue();
124: for (int i = 0; i < nvs.length; i++) {
125: NameValuePair pair = new NameValuePair();
126: pair.setName((String) entry.getKey());
127: pair.setValue(nvs[i]);
128: nvPairs.add(pair);
129: }
130: } else {
131: NameValuePair pair = new NameValuePair();
132: pair.setName((String) entry.getKey());
133: pair.setValue((String) entry.getValue());
134: nvPairs.add(pair);
135: }
136: }
137: method.setQueryString((NameValuePair[]) nvPairs
138: .toArray(new NameValuePair[0]));
139: Document doc = sendRequest(method);
140: getChanges().clear();
141: _handleDocument(doc);
142: _requestId++;
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146: }
147:
148: public void log(String tag, String message) {
149: //TODO
150: }
151:
152: public void log(int requestId, String log) {
153: if (_logger != null) {
154: _logger.log(requestId, log);
155: } else {
156: System.out.println(requestId + ": " + log);
157: }
158: }
159:
160: private void _handleDocument(Document doc) {
161: NodeList nodes = doc.getChildNodes();
162: if (nodes.getLength() > 0) {
163: Element element = (Element) nodes.item(0);
164: if (element.getNodeName().equals("thin-ui")) {
165: NodeList childNodes = element.getChildNodes();
166: for (int i = 0; i < childNodes.getLength(); i++) {
167: if (childNodes.item(i) instanceof Element) {
168: Element child = (Element) childNodes.item(i);
169: Component com = (Component) handle(null,
170: new W3CDomNode(child, null));
171: }
172: }
173: }
174: }
175: }
176: }
|