001/*
002 * Copyright (c) 2004-2012, Willem Cazander
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 * 
008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009 *   following disclaimer.
010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
011 *   the following disclaimer in the documentation and/or other materials provided with the distribution.
012 * 
013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.x4o.xml.test.swixml;
025
026import java.awt.Component;
027import java.lang.reflect.Field;
028
029import javax.swing.Action;
030
031import org.x4o.xml.test.swixml.SwiXmlParser.SwiXmlVersion;
032
033/**
034 * SwingEngine demo.
035 * 
036 * @author Willem Cazander
037 * @version 1.0 Aug 15, 2012
038 */
039public class SwingEngine {
040
041        private Object uiHandler;
042        private Component rootComponent = null;
043        
044        public SwingEngine(Object uiHandler) {
045                this.uiHandler=uiHandler;
046        }
047        
048        public Action getUIActionByName(String name) {
049                if (name==null) {
050                        return null;
051                }
052                if (uiHandler==null) {
053                        return null;
054                }
055                try {
056                        for (Field f:uiHandler.getClass().getFields()) {
057                                if (name.equals(f.getName())) {
058                                        Object value = f.get(uiHandler);
059                                        if (value instanceof Action) {
060                                                return (Action)value;
061                                        } else {
062                                                return null;
063                                        }
064                                }
065                        }
066                } catch (Exception e) {
067                        throw new RuntimeException(e);
068                }
069                return null;
070        }
071        
072        public Component render(String resource,SwiXmlVersion xmlVersion) {
073                SwiXmlParser parser = new SwiXmlParser(this,xmlVersion);
074                try {
075                        parser.parseResource(resource);
076                } catch (Exception e) {
077                        throw new RuntimeException(e);
078                }
079                rootComponent = parser.getRootComponent();
080                return getRootComponent();
081        }
082        
083        public Component getRootComponent() {
084                return rootComponent;
085        }
086}