001: /*
002: * SwingML
003: * Copyright (C) 2002 Robert J. Morris.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
018: * Boston, MA 02111-1307, USA.
019: *
020: * Authors:
021: * Robert J. Morris <robertj@morris.net>
022: */
023:
024: package org.swingml.xml.mapper;
025:
026: import java.awt.*;
027:
028: import org.swingml.Constants;
029: import org.swingml.model.GridBagBaseModel;
030: import org.swingml.system.*;
031: import org.swingml.xml.Mapper;
032: import org.swingml.xml.MapperUtil;
033: import org.w3c.dom.Node;
034:
035: /**
036: * @author rmorris
037: *
038: * To change this generated comment edit the template variable "typecomment":
039: * Window>Preferences>Java>Templates.
040: * To enable and disable the creation of type comments go to
041: * Window>Preferences>Java>Code Generation.
042: */
043: public abstract class GridBagBaseMapper extends MapperUtil implements
044: Mapper {
045: /**
046: * @see org.swingml.xml.Mapper#getModelToMap(Node, Object)
047: */
048: public abstract Object getModelToMap(Node aNode, Object aParent,
049: Container aContainer);
050:
051: /**
052: * @see org.swingml.xml.Mapper#mapModel(Node, Object)
053: */
054: public abstract void mapModel(Node aNode, Object aParent,
055: Container aContainer);
056:
057: /**
058: * @see org.swingml.xml.Mapper#mapModelAttributes(Node, Object, Object)
059: */
060: public void mapModelAttributes(Node aNode, Object aModel,
061: Object aParent) {
062: GridBagBaseModel theBaseModel = (GridBagBaseModel) aModel;
063: Node theResultNode = null;
064: final String PERCENT_SYMBOL = "%";
065:
066: theResultNode = super .getAttribute(aNode, Constants.NAME);
067: if (theResultNode != null) {
068: theBaseModel.setName(theResultNode.getNodeValue());
069: }
070:
071: theResultNode = super .getAttribute(aNode, Constants.PADDING);
072: if (theResultNode != null) {
073: theBaseModel.setPadding(new Integer(theResultNode
074: .getNodeValue()).intValue());
075: }
076:
077: theResultNode = super .getAttribute(aNode, Constants.IPADX);
078: if (theResultNode != null) {
079: theBaseModel.setIpadx(new Integer(theResultNode
080: .getNodeValue()).intValue());
081: }
082:
083: theResultNode = super .getAttribute(aNode, Constants.IPADY);
084: if (theResultNode != null) {
085: theBaseModel.setIpady(new Integer(theResultNode
086: .getNodeValue()).intValue());
087: }
088:
089: theResultNode = super .getAttribute(aNode, Constants.FILL);
090: if (theResultNode != null) {
091: int theFill = GridBagConstraints.NONE; // (None|Horizontal|Vertical|Both)
092: String theFillValue = theResultNode.getNodeValue();
093:
094: if (theFillValue.equalsIgnoreCase(Constants.HORIZONTAL)) {
095: theFill = GridBagConstraints.HORIZONTAL;
096: } else if (theFillValue
097: .equalsIgnoreCase(Constants.VERTICAL)) {
098: theFill = GridBagConstraints.VERTICAL;
099: } else if (theFillValue.equalsIgnoreCase(Constants.BOTH)) {
100: theFill = GridBagConstraints.BOTH;
101: }
102:
103: theBaseModel.setFill(theFill);
104: }
105:
106: theResultNode = super .getAttribute(aNode, Constants.ANCHOR);
107: if (theResultNode != null) {
108: int theAnchor = GridBagConstraints.CENTER;
109: String theAnchorValue = theResultNode.getNodeValue();
110: if (theAnchorValue.equalsIgnoreCase(Constants.NORTH)) {
111: theAnchor = GridBagConstraints.NORTH;
112: } else if (theAnchorValue.equalsIgnoreCase(Constants.SOUTH)) {
113: theAnchor = GridBagConstraints.SOUTH;
114: } else if (theAnchorValue
115: .equalsIgnoreCase(Constants.CENTER)) {
116: theAnchor = GridBagConstraints.CENTER;
117: } else if (theAnchorValue.equalsIgnoreCase(Constants.EAST)) {
118: theAnchor = GridBagConstraints.EAST;
119: } else if (theAnchorValue.equalsIgnoreCase(Constants.WEST)) {
120: theAnchor = GridBagConstraints.WEST;
121: } else if (theAnchorValue
122: .equalsIgnoreCase(Constants.NORTHWEST)) {
123: theAnchor = GridBagConstraints.NORTHWEST;
124: } else if (theAnchorValue
125: .equalsIgnoreCase(Constants.NORTHEAST)) {
126: theAnchor = GridBagConstraints.NORTHEAST;
127: } else if (theAnchorValue
128: .equalsIgnoreCase(Constants.SOUTHWEST)) {
129: theAnchor = GridBagConstraints.SOUTHWEST;
130: } else if (theAnchorValue
131: .equalsIgnoreCase(Constants.SOUTHEAST)) {
132: theAnchor = GridBagConstraints.SOUTHEAST;
133: }
134: theBaseModel.setAnchor(theAnchor);
135: }
136:
137: theResultNode = super .getAttribute(aNode, Constants.WEIGHTX);
138: if (theResultNode != null) {
139: double theWeightx = 0;
140: StringBuffer theNodeValue = new StringBuffer(theResultNode
141: .getNodeValue());
142: int thePercentIdx = theNodeValue.indexOf(PERCENT_SYMBOL);
143:
144: if (theNodeValue.length() == 0 || thePercentIdx < 0) {
145: theWeightx = 0;
146: } else {
147: try {
148: theWeightx = new Double(theNodeValue
149: .replace(
150: thePercentIdx,
151: thePercentIdx
152: + PERCENT_SYMBOL.length(),
153: "").toString()).doubleValue();
154: if (theWeightx <= 100 && theWeightx >= 0) {
155: theWeightx *= 0.01;
156: } else {
157: theWeightx = 0;
158: }
159: } catch (Exception e) {
160: SwingMLLogger
161: .getInstance()
162: .log(
163: "Syntax Error: A non-numeric value was specified for theWeightx");
164: theWeightx = 0;
165: }
166: }
167: theBaseModel.setWeightx(theWeightx);
168: }
169:
170: theResultNode = super .getAttribute(aNode, Constants.WEIGHTY);
171: if (theResultNode != null) {
172: double theWeighty = 0;
173: StringBuffer theNodeValue = new StringBuffer(theResultNode
174: .getNodeValue());
175: int thePercentIdx = theNodeValue.indexOf(PERCENT_SYMBOL);
176:
177: if (theNodeValue.length() == 0 || thePercentIdx < 0) {
178: theWeighty = 0;
179: } else {
180: try {
181: theWeighty = new Double(theNodeValue
182: .replace(
183: thePercentIdx,
184: thePercentIdx
185: + PERCENT_SYMBOL.length(),
186: "").toString()).doubleValue();
187:
188: if (theWeighty <= 100 && theWeighty >= 0) {
189: theWeighty *= 0.01;
190: } else {
191: theWeighty = 0;
192: }
193: } catch (Exception e) {
194: SwingMLLogger
195: .getInstance()
196: .log(
197: "Syntax Error: A non-numeric value was specified for weighty");
198: theWeighty = 0;
199: }
200: }
201:
202: theBaseModel.setWeighty(theWeighty);
203: }
204:
205: theResultNode = super .getAttribute(aNode, Constants.GRIDHEIGHT);
206: if (theResultNode != null) {
207: int theGridHeight = 0;
208: try {
209: theGridHeight = new Integer(theResultNode
210: .getNodeValue()).intValue();
211: } catch (Exception e) {
212: SwingMLLogger
213: .getInstance()
214: .log(
215: "Syntax Error: A non-numeric value was specified for gridheight");
216: theGridHeight = 0;
217: } finally {
218: theBaseModel.setGridHeight(theGridHeight);
219: }
220: }
221:
222: theResultNode = super .getAttribute(aNode, Constants.GRIDWIDTH);
223: if (theResultNode != null) {
224: int theGridWidth = 0;
225: try {
226: theGridWidth = new Integer(theResultNode.getNodeValue())
227: .intValue();
228: } catch (Exception e) {
229: SwingMLLogger
230: .getInstance()
231: .log(
232: "Syntax Error: A non-numeric value was specified for gridwidth");
233: theGridWidth = 0;
234: } finally {
235: theBaseModel.setGridWidth(theGridWidth);
236: }
237: }
238: }
239: }
|