/* SwingML
* Copyright (C) 2002 Robert Morris.
*
* 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 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.
*
* Authors:
* Robert Morris <robertj@morris.net>
* Alessandro Di Bella <alessandro.dibella@bpeng.com>
*/
package org.swingml.xml.mapper;
import java.awt.*;
import org.swingml.Constants;
import org.swingml.event.InvokableEventHandlerFactory;
import org.swingml.model.ExternalActionModel;
import org.swingml.model.ListenerModel;
import org.swingml.system.*;
import org.swingml.xml.Mapper;
import org.swingml.xml.MapperUtil;
import org.w3c.dom.Node;
/**
* @author <a href="mailto:robertj@morris.net">Robert J. Morris</a>
*
*/
public class ExternalActionMapper extends MapperUtil implements Mapper {
/**
* @see org.swingml.xml.Mapper#getModelToMap(Node, Object)
*/
public Object getModelToMap(Node aNode, Object aParent, Container aContainer) {
ExternalActionModel theModel = new ExternalActionModel();
ListenerModel theListener = (ListenerModel) aParent;
theListener.addAction(theModel);
return theModel;
}
/**
* @see org.swingml.xml.Mapper#mapModel(Node, Object)
*/
public void mapModel(Node aNode, Object aParent, Container aContainer) {
ExternalActionModel theModel = (ExternalActionModel) this.getModelToMap(aNode, aParent, aContainer);
this.mapModelAttributes(aNode, theModel, aParent);
super.iterate(aNode, theModel, aContainer);
}
/**
* @see org.swingml.xml.Mapper#mapModelAttributes(Node, Object, Object)
*/
public void mapModelAttributes(Node aNode, Object aModel, Object aParent) {
ExternalActionModel theActionModel = (ExternalActionModel) aModel;
Node theResultNode = null;
theResultNode = super.getAttribute(aNode, Constants.COMPONENT);
if (theResultNode != null) {
theActionModel.setComponent(theResultNode.getNodeValue());
}
theResultNode = super.getAttribute(aNode, Constants.EXTERNAL_CLASS);
if (theResultNode != null) {
Class externalClass = null;
String className = theResultNode.getNodeValue();
try {
externalClass = Class.forName(className);
} catch (Exception e) {
SwingMLLogger.getInstance().log("Syntax Error: Failed to load the class: [" + className + "]");
SwingMLLogger.getInstance().log(e);
externalClass = null;
} finally {
theActionModel.setExternalClass(externalClass);
}
}
theResultNode = super.getAttribute(aNode, Constants.EXTERNAL_FACTORY_CLASS);
if (theResultNode != null) {
Class externalFactoryClass = null;
String className = theResultNode.getNodeValue();
try {
externalFactoryClass = Class.forName(className);
if (externalFactoryClass.isAssignableFrom(InvokableEventHandlerFactory.class)) {
throw new IllegalArgumentException("The class " + className + " does not implement " + InvokableEventHandlerFactory.class.getName());
}
} catch (Exception e) {
SwingMLLogger.getInstance().log("Syntax Error: Failed to load the class: [" + className + "]");
SwingMLLogger.getInstance().log(e);
externalFactoryClass = null;
} finally {
theActionModel.setExternalFactoryClass(externalFactoryClass);
}
}
}
}