001: /* SwingML
002: * Copyright (C) 2002 Robert Morris.
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 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
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Robert Morris <robertj@morris.net>
021: * Alessandro Di Bella <alessandro.dibella@bpeng.com>
022: */
023:
024: package org.swingml.xml.mapper;
025:
026: import java.awt.*;
027:
028: import org.swingml.Constants;
029: import org.swingml.event.InvokableEventHandlerFactory;
030: import org.swingml.model.ExternalActionModel;
031: import org.swingml.model.ListenerModel;
032: import org.swingml.system.*;
033: import org.swingml.xml.Mapper;
034: import org.swingml.xml.MapperUtil;
035: import org.w3c.dom.Node;
036:
037: /**
038: * @author <a href="mailto:robertj@morris.net">Robert J. Morris</a>
039: *
040: */
041: public class ExternalActionMapper extends MapperUtil implements Mapper {
042:
043: /**
044: * @see org.swingml.xml.Mapper#getModelToMap(Node, Object)
045: */
046: public Object getModelToMap(Node aNode, Object aParent,
047: Container aContainer) {
048: ExternalActionModel theModel = new ExternalActionModel();
049: ListenerModel theListener = (ListenerModel) aParent;
050: theListener.addAction(theModel);
051: return theModel;
052: }
053:
054: /**
055: * @see org.swingml.xml.Mapper#mapModel(Node, Object)
056: */
057: public void mapModel(Node aNode, Object aParent,
058: Container aContainer) {
059: ExternalActionModel theModel = (ExternalActionModel) this
060: .getModelToMap(aNode, aParent, aContainer);
061: this .mapModelAttributes(aNode, theModel, aParent);
062: super .iterate(aNode, theModel, aContainer);
063: }
064:
065: /**
066: * @see org.swingml.xml.Mapper#mapModelAttributes(Node, Object, Object)
067: */
068: public void mapModelAttributes(Node aNode, Object aModel,
069: Object aParent) {
070: ExternalActionModel theActionModel = (ExternalActionModel) aModel;
071: Node theResultNode = null;
072: theResultNode = super .getAttribute(aNode, Constants.COMPONENT);
073: if (theResultNode != null) {
074: theActionModel.setComponent(theResultNode.getNodeValue());
075: }
076: theResultNode = super .getAttribute(aNode,
077: Constants.EXTERNAL_CLASS);
078: if (theResultNode != null) {
079: Class externalClass = null;
080: String className = theResultNode.getNodeValue();
081: try {
082: externalClass = Class.forName(className);
083: } catch (Exception e) {
084: SwingMLLogger.getInstance().log(
085: "Syntax Error: Failed to load the class: ["
086: + className + "]");
087: SwingMLLogger.getInstance().log(e);
088: externalClass = null;
089: } finally {
090: theActionModel.setExternalClass(externalClass);
091: }
092: }
093: theResultNode = super .getAttribute(aNode,
094: Constants.EXTERNAL_FACTORY_CLASS);
095: if (theResultNode != null) {
096: Class externalFactoryClass = null;
097: String className = theResultNode.getNodeValue();
098: try {
099: externalFactoryClass = Class.forName(className);
100: if (externalFactoryClass
101: .isAssignableFrom(InvokableEventHandlerFactory.class)) {
102: throw new IllegalArgumentException("The class "
103: + className
104: + " does not implement "
105: + InvokableEventHandlerFactory.class
106: .getName());
107: }
108: } catch (Exception e) {
109: SwingMLLogger.getInstance().log(
110: "Syntax Error: Failed to load the class: ["
111: + className + "]");
112: SwingMLLogger.getInstance().log(e);
113: externalFactoryClass = null;
114: } finally {
115: theActionModel
116: .setExternalFactoryClass(externalFactoryClass);
117: }
118: }
119: }
120: }
|