Package org.onemind.swingweb.client.awt

Source Code of org.onemind.swingweb.client.awt.SwingWebClient

/*
* 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.awt;

import java.awt.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.onemind.swingweb.client.awt.ui.ComponentUIHandler;
import org.onemind.swingweb.client.core.AbstractClient;
import org.onemind.swingweb.client.core.ui.UIHandler;
import org.onemind.swingweb.client.dom.DomNode;
import org.w3c.dom.*;
public class SwingWebClient extends AbstractClient implements Logger
{

    private Console _console;

    private HttpClient _client;

    private Logger _logger;

    private UIHandler _defaultHandler = new ComponentUIHandler(this);

    int _requestId = 0;

    public SwingWebClient(String url)
    {
        super(url);
        _client = new HttpClient();
        _console = new Console(url, this);
        _console.pack();
        _console.setSize(600, 300);
        _console.setVisible(true);
    }

    public void setLogger(Logger logger)
    {
        _logger = logger;
    }

    public Document sendRequest(HttpMethod request) throws IOException
    {
        try
        {
            int statusCode = _client.executeMethod(request);
            if (statusCode != HttpStatus.SC_OK)
            {
                System.err.println("Method failed: " + request.getStatusLine());
                return null;
            } else
            {
                byte[] responseBody = request.getResponseBody();
                log(_requestId, new String(responseBody));
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                try
                {
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    Document document = builder.parse(new ByteArrayInputStream(responseBody));
                    return document;
                } catch (Exception spe)
                {
                    spe.printStackTrace();
                    return null;
                }
            }
        } finally
        {
            request.releaseConnection();
        }
    }

    public void refreshUI() throws IOException
    {
        GetMethod method = new GetMethod(getUrl());
        Document doc = sendRequest(method);
        _handleDocument(doc);
    }

    public Object handle(Object parent, DomNode element, String id)
    {
        String className = element.getAttribute("class");
        String handlerName = element.getAttribute("handler");
        UIHandler handler = (UIHandler) getHandlerByName(handlerName);
        if (handler == null)
        {
            handler = _defaultHandler;
            System.out.println("Unable to handle " + className);
        }
        Object com = null;
        if (getComponent(id) != null)
        {
            com = handler.updateComponent(getComponent(id), element);
        } else
        {
            com = handler.createComponent(parent, element);
            addComponent(id, com);
        }
        return com;
    }

    public void submitRequest()
    {
        try
        {
            GetMethod method = new GetMethod(getUrl());
            List nvPairs = new ArrayList();
            Iterator it = getChanges().entrySet().iterator();
            while (it.hasNext())
            {
                Map.Entry entry = (Map.Entry) it.next();
                if (entry.getValue() instanceof String[])
                {
                    String[] nvs = (String[]) entry.getValue();
                    for (int i = 0; i < nvs.length; i++)
                    {
                        NameValuePair pair = new NameValuePair();
                        pair.setName((String) entry.getKey());
                        pair.setValue(nvs[i]);
                        nvPairs.add(pair);
                    }
                } else
                {
                    NameValuePair pair = new NameValuePair();
                    pair.setName((String) entry.getKey());
                    pair.setValue((String) entry.getValue());
                    nvPairs.add(pair);
                }
            }
            method.setQueryString((NameValuePair[]) nvPairs.toArray(new NameValuePair[0]));
            Document doc = sendRequest(method);
            getChanges().clear();
            _handleDocument(doc);
            _requestId++;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void log(String tag, String message)
    {
        //TODO
    }

    public void log(int requestId, String log)
    {
        if (_logger != null)
        {
            _logger.log(requestId, log);
        } else
        {
            System.out.println(requestId + ": " + log);
        }
    }

    private void _handleDocument(Document doc)
    {
        NodeList nodes = doc.getChildNodes();
        if (nodes.getLength() > 0)
        {
            Element element = (Element) nodes.item(0);
            if (element.getNodeName().equals("thin-ui"))
            {
                NodeList childNodes = element.getChildNodes();
                for (int i = 0; i < childNodes.getLength(); i++)
                {
                    if (childNodes.item(i) instanceof Element)
                    {
                        Element child = (Element) childNodes.item(i);
                        Component com = (Component) handle(null, new W3CDomNode(child, null));
                    }
                }
            }
        }
    }
}
TOP

Related Classes of org.onemind.swingweb.client.awt.SwingWebClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.