Package org.swingml.xml.mapper

Source Code of org.swingml.xml.mapper.GridBagBaseMapper

/*
* SwingML
* Copyright (C) 2002 Robert J. 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 J. Morris    <robertj@morris.net>
*/

package org.swingml.xml.mapper;

import java.awt.*;

import org.swingml.Constants;
import org.swingml.model.GridBagBaseModel;
import org.swingml.system.*;
import org.swingml.xml.Mapper;
import org.swingml.xml.MapperUtil;
import org.w3c.dom.Node;



/**
* @author rmorris
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public abstract class GridBagBaseMapper extends MapperUtil implements Mapper {
    /**
     * @see org.swingml.xml.Mapper#getModelToMap(Node, Object)
     */
    public abstract Object getModelToMap(Node aNode, Object aParent, Container aContainer);

    /**
     * @see org.swingml.xml.Mapper#mapModel(Node, Object)
     */
    public abstract void mapModel(Node aNode, Object aParent, Container aContainer);

    /**
     * @see org.swingml.xml.Mapper#mapModelAttributes(Node, Object, Object)
     */
    public void mapModelAttributes(Node aNode, Object aModel, Object aParent) {
        GridBagBaseModel theBaseModel = (GridBagBaseModel) aModel;
        Node theResultNode = null;
        final String PERCENT_SYMBOL = "%";

        theResultNode = super.getAttribute(aNode, Constants.NAME);
        if (theResultNode != null) {
            theBaseModel.setName(theResultNode.getNodeValue());
        }

        theResultNode = super.getAttribute(aNode, Constants.PADDING);
        if (theResultNode != null) {
            theBaseModel.setPadding(new Integer(theResultNode.getNodeValue()).intValue());
        }

        theResultNode = super.getAttribute(aNode, Constants.IPADX);
        if (theResultNode != null) {
            theBaseModel.setIpadx(new Integer(theResultNode.getNodeValue()).intValue());
        }

        theResultNode = super.getAttribute(aNode, Constants.IPADY);
        if (theResultNode != null) {
            theBaseModel.setIpady(new Integer(theResultNode.getNodeValue()).intValue());
        }

        theResultNode = super.getAttribute(aNode, Constants.FILL);
        if (theResultNode != null) {
            int theFill = GridBagConstraints.NONE; // (None|Horizontal|Vertical|Both)
            String theFillValue = theResultNode.getNodeValue();

            if (theFillValue.equalsIgnoreCase(Constants.HORIZONTAL)) {
                theFill = GridBagConstraints.HORIZONTAL;
            } else if (theFillValue.equalsIgnoreCase(Constants.VERTICAL)) {
                theFill = GridBagConstraints.VERTICAL;
            } else if (theFillValue.equalsIgnoreCase(Constants.BOTH)) {
                theFill = GridBagConstraints.BOTH;
            }

            theBaseModel.setFill(theFill);
        }

        theResultNode = super.getAttribute(aNode, Constants.ANCHOR);
        if (theResultNode != null) {
            int theAnchor = GridBagConstraints.CENTER;
            String theAnchorValue = theResultNode.getNodeValue();
            if (theAnchorValue.equalsIgnoreCase(Constants.NORTH)) {
                theAnchor = GridBagConstraints.NORTH;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.SOUTH)) {
                theAnchor = GridBagConstraints.SOUTH;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.CENTER)) {
                theAnchor = GridBagConstraints.CENTER;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.EAST)) {
                theAnchor = GridBagConstraints.EAST;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.WEST)) {
                theAnchor = GridBagConstraints.WEST;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.NORTHWEST)) {
                theAnchor = GridBagConstraints.NORTHWEST;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.NORTHEAST)) {
                theAnchor = GridBagConstraints.NORTHEAST;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.SOUTHWEST)) {
                theAnchor = GridBagConstraints.SOUTHWEST;
            } else if (theAnchorValue.equalsIgnoreCase(Constants.SOUTHEAST)) {
                theAnchor = GridBagConstraints.SOUTHEAST;
            }
            theBaseModel.setAnchor(theAnchor);
        }

        theResultNode = super.getAttribute(aNode, Constants.WEIGHTX);
        if (theResultNode != null) {
            double theWeightx = 0;
            StringBuffer theNodeValue = new StringBuffer(theResultNode.getNodeValue());
            int thePercentIdx = theNodeValue.indexOf(PERCENT_SYMBOL);

            if (theNodeValue.length() == 0 || thePercentIdx < 0) {
                theWeightx = 0;
            } else {
                try {
                    theWeightx = new Double(theNodeValue.replace(thePercentIdx, thePercentIdx + PERCENT_SYMBOL.length(), "").toString()).doubleValue();
                    if (theWeightx <= 100 && theWeightx >= 0) {
                        theWeightx *= 0.01;
                    } else {
                        theWeightx = 0;
                    }
                } catch (Exception e) {
                    SwingMLLogger.getInstance().log("Syntax Error: A non-numeric value was specified for theWeightx");
                    theWeightx = 0;
                }
            }
            theBaseModel.setWeightx(theWeightx);
        }

        theResultNode = super.getAttribute(aNode, Constants.WEIGHTY);
        if (theResultNode != null) {
            double theWeighty = 0;
            StringBuffer theNodeValue = new StringBuffer(theResultNode.getNodeValue());
            int thePercentIdx = theNodeValue.indexOf(PERCENT_SYMBOL);

            if (theNodeValue.length() == 0 || thePercentIdx < 0) {
                theWeighty = 0;
            } else {
                try {
                    theWeighty = new Double(theNodeValue.replace(thePercentIdx, thePercentIdx + PERCENT_SYMBOL.length(), "").toString()).doubleValue();

                    if (theWeighty <= 100 && theWeighty >= 0) {
                        theWeighty *= 0.01;
                    } else {
                        theWeighty = 0;
                    }
                } catch (Exception e) {
                    SwingMLLogger.getInstance().log("Syntax Error: A non-numeric value was specified for weighty");
                    theWeighty = 0;
                }
            }

            theBaseModel.setWeighty(theWeighty);
        }

        theResultNode = super.getAttribute(aNode, Constants.GRIDHEIGHT);
        if (theResultNode != null) {
            int theGridHeight = 0;
            try {
                theGridHeight = new Integer(theResultNode.getNodeValue()).intValue();
            } catch (Exception e) {
                SwingMLLogger.getInstance().log("Syntax Error: A non-numeric value was specified for gridheight");
                theGridHeight = 0;
            } finally {
                theBaseModel.setGridHeight(theGridHeight);
            }
        }

        theResultNode = super.getAttribute(aNode, Constants.GRIDWIDTH);
        if (theResultNode != null) {
            int theGridWidth = 0;
            try {
                theGridWidth = new Integer(theResultNode.getNodeValue()).intValue();
            } catch (Exception e) {
                SwingMLLogger.getInstance().log("Syntax Error: A non-numeric value was specified for gridwidth");
                theGridWidth = 0;
            } finally {
                theBaseModel.setGridWidth(theGridWidth);
            }
        }
    }
}
TOP

Related Classes of org.swingml.xml.mapper.GridBagBaseMapper

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.