001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.awt.BorderLayout;
025: import java.util.StringTokenizer;
026:
027: import org.w3c.dom.Element;
028:
029: import cz.autel.dmi.HIGConstraints;
030:
031: public class AnchorFactory {
032: private static AnchorFactory factoryInstance = new AnchorFactory();
033: public static HIGConstraints constraints = new HIGConstraints();
034:
035: public class BoxAnchor {
036: private float alignmentX = 0.0f;
037: private float alignmentY = 0.0f;
038:
039: public BoxAnchor() {
040: }
041:
042: public BoxAnchor(float alignmentX, float alignmentY) {
043: this .alignmentX = alignmentX;
044: this .alignmentY = alignmentY;
045: }
046:
047: public float getAlignmentX() {
048: return alignmentX;
049: }
050:
051: public float getAlignmentY() {
052: return alignmentY;
053: }
054:
055: public void setAlignmentX(float alignmentX) {
056: this .alignmentX = alignmentX;
057: }
058:
059: public void setAlignmentY(float alignmentY) {
060: this .alignmentY = alignmentY;
061: }
062: }
063:
064: public Object constructAnchor(Element anchorNode)
065: throws GUIException {
066: String type = anchorNode.getAttribute("type");
067: if (type.equals("hig") || type.equals(""))
068: return constructHIGAnchor(anchorNode);
069: else if (type.equals("box"))
070: return constructBoxAnchor(anchorNode);
071: else if (type.equals("border"))
072: return constructBorderAnchor(anchorNode);
073: else
074: throw new GUIException("Unknown anchor type [" + type + "]");
075: }
076:
077: public String constructBorderAnchor(Element anchorNode)
078: throws GUIException {
079: String border = anchorNode.getAttribute("border");
080: if ("center".equals(border))
081: return BorderLayout.CENTER;
082: else if ("north".equals(border))
083: return BorderLayout.NORTH;
084: else if ("east".equals(border))
085: return BorderLayout.EAST;
086: else if ("south".equals(border))
087: return BorderLayout.SOUTH;
088: else if ("west".equals(border))
089: return BorderLayout.WEST;
090: else
091: throw new GUIException("Invalid border anchor data");
092: }
093:
094: public BoxAnchor constructBoxAnchor(Element anchorNode)
095: throws GUIException {
096: BoxAnchor anchor = new BoxAnchor();
097: try {
098: if (!anchorNode.getAttribute("alignx").equals(""))
099: anchor.setAlignmentX(Float.parseFloat(anchorNode
100: .getAttribute("alignx")));
101: if (!anchorNode.getAttribute("aligny").equals(""))
102: anchor.setAlignmentY(Float.parseFloat(anchorNode
103: .getAttribute("aligny")));
104: return anchor;
105: } catch (NumberFormatException e) {
106: throw new GUIException("Invalid box anchor data", e);
107: }
108: }
109:
110: public HIGConstraints constructHIGAnchor(Element anchorNode)
111: throws GUIException {
112: StringTokenizer pos = new StringTokenizer(anchorNode
113: .getAttribute("pos"), ",");
114: String align = anchorNode.getAttribute("align");
115: int posCount = pos.countTokens();
116:
117: try {
118: if (align.equals("")
119: && anchorNode.getAttributes().getNamedItem("align") == null)
120: align = "rltb";
121: if (posCount == 2) {
122: return constraints.rc(
123: Integer.parseInt(pos.nextToken()), Integer
124: .parseInt(pos.nextToken()), align);
125: } else if (posCount == 3) {
126: return constraints.rcwh(Integer.parseInt(pos
127: .nextToken()), Integer
128: .parseInt(pos.nextToken()), Integer
129: .parseInt(pos.nextToken()), 1, align);
130: } else if (posCount == 4) {
131: return constraints.rcwh(Integer.parseInt(pos
132: .nextToken()), Integer
133: .parseInt(pos.nextToken()), Integer
134: .parseInt(pos.nextToken()), Integer
135: .parseInt(pos.nextToken()), align);
136: } else {
137: throw new GUIException("Bad anchor argument count");
138: }
139: } catch (NumberFormatException e) {
140: throw new GUIException("Invalid anchor data", e);
141: }
142: }
143:
144: public static AnchorFactory getInstance() {
145: return factoryInstance;
146: }
147: }
|