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.awt.Container;
026: import java.awt.FlowLayout;
027: import java.awt.LayoutManager;
028: import java.util.StringTokenizer;
029:
030: import javax.swing.BoxLayout;
031:
032: import org.beryl.gui.widgets.Dialog;
033: import org.beryl.gui.widgets.Frame;
034: import org.w3c.dom.Element;
035:
036: import cz.autel.dmi.HIGLayout;
037:
038: /**
039: * Supported layouts:
040: *
041: * HIGLayout (hig)
042: * BoxLayout (hbox/vbox)
043: * BorderLayout (border)
044: * FlowLayout(flow, lflow, rflow)
045: */
046:
047: public class LayoutFactory {
048: private static LayoutFactory factoryInstance = new LayoutFactory();
049:
050: private LayoutFactory() {
051: }
052:
053: public LayoutManager constructLayout(Widget widget,
054: Element layoutNode) throws GUIException {
055: String type = layoutNode.getAttribute("type");
056:
057: if (widget instanceof Frame)
058: widget = ((Frame) widget).getPanel();
059: else if (widget instanceof Dialog)
060: widget = ((Dialog) widget).getPanel();
061: if (type.equals("") || type.equals("hig"))
062: return constructHIGLayout(layoutNode);
063: else if (type.equals("vbox") || type.equals("hbox"))
064: return constructBoxLayout(widget, layoutNode);
065: else if (type.equals("border"))
066: return new BorderLayout();
067: else if (type.equals("flow"))
068: return new FlowLayout();
069: else if (type.equals("lflow"))
070: return new FlowLayout(FlowLayout.LEFT);
071: else if (type.equals("rflow"))
072: return new FlowLayout(FlowLayout.RIGHT);
073: else
074: throw new GUIException("Unknown layout [" + type + "]");
075: }
076:
077: private LayoutManager constructBoxLayout(Widget widget,
078: Element layoutNode) {
079: return new BoxLayout((Container) widget.getWidget(), layoutNode
080: .getAttribute("type").equals("vbox") ? BoxLayout.Y_AXIS
081: : BoxLayout.X_AXIS);
082: }
083:
084: private LayoutManager constructHIGLayout(Element layoutNode)
085: throws GUIException {
086: StringTokenizer horiz = new StringTokenizer(layoutNode
087: .getAttribute("horiz"), ",");
088: StringTokenizer vert = new StringTokenizer(layoutNode
089: .getAttribute("vert"), ",");
090: int horizCount = horiz.countTokens(), vertCount = vert
091: .countTokens();
092: int h[] = new int[horizCount], v[] = new int[vertCount];
093:
094: try {
095: for (int i = 0; i < horizCount; i++)
096: h[i] = Integer.parseInt(horiz.nextToken());
097: for (int i = 0; i < vertCount; i++)
098: v[i] = Integer.parseInt(vert.nextToken());
099: HIGLayout layout = new HIGLayout(h, v);
100:
101: if (!layoutNode.getAttribute("hweights").equals("")) {
102: StringTokenizer hweights = new StringTokenizer(
103: layoutNode.getAttribute("hweights"), ",");
104: int hweightsCount = hweights.countTokens();
105:
106: for (int i = 0; i < hweightsCount; i++) {
107: layout.setColumnWeight(i + 1, Integer
108: .parseInt(hweights.nextToken()));
109: }
110: }
111: if (!layoutNode.getAttribute("vweights").equals("")) {
112: StringTokenizer vweights = new StringTokenizer(
113: layoutNode.getAttribute("vweights"), ",");
114: int vweightsCount = vweights.countTokens();
115:
116: for (int i = 0; i < vweightsCount; i++) {
117: layout.setRowWeight(i + 1, Integer
118: .parseInt(vweights.nextToken()));
119: }
120: }
121: return layout;
122: } catch (NumberFormatException e) {
123: throw new GUIException("Invalid layout data", e);
124: }
125: }
126:
127: public static LayoutFactory getInstance() {
128: return factoryInstance;
129: }
130: }
|