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.widgets;
023:
024: import java.awt.Component;
025: import java.awt.Dimension;
026:
027: import javax.swing.JComponent;
028:
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.Widget;
031: import org.beryl.gui.WidgetInfo;
032:
033: /**
034: * Spacer for BoxLayouts and other uses (MenuBar etc)
035: */
036: public class Spacer extends Widget {
037: protected static WidgetInfo spacerInfo = null;
038: private static final String TYPE_GLUE = "glue";
039: private static final String TYPE_STRUT = "strut";
040: private static final String AXIS_HORIZONTAL = "h";
041: private static final String AXIS_VERTICAL = "v";
042:
043: private Dimension minimumSize = null;
044: private Dimension preferredSize = null;
045: private Dimension maximumSize = null;
046: private JSpacer spacer = null;
047: private boolean isConstructed = false;
048:
049: private String type = TYPE_GLUE;
050: private String axis = AXIS_VERTICAL;
051: private int size = 10;
052:
053: static {
054: spacerInfo = new WidgetInfo(Spacer.class, widgetInfo);
055: spacerInfo.addProperty("type", "string", "strut");
056: spacerInfo.addProperty("axis", "string", "h");
057: spacerInfo.addProperty("size", "int", new Integer(10));
058: };
059:
060: private class JSpacer extends JComponent {
061: public Dimension getMinimumSize() {
062: return minimumSize;
063: }
064:
065: public Dimension getPreferredSize() {
066: return preferredSize;
067: }
068:
069: public Dimension getMaximumSize() {
070: return maximumSize;
071: }
072: };
073:
074: public Spacer(Widget parent, String name) throws GUIException {
075: super (parent, name);
076:
077: spacer = new JSpacer();
078: }
079:
080: public void setProperty(String name, Object value)
081: throws GUIException {
082: if ("type".equals(name)) {
083: type = (String) value;
084: } else if ("axis".equals(name)) {
085: axis = (String) value;
086: } else if ("size".equals(name)) {
087: size = ((Integer) value).intValue();
088: }
089: if (isConstructed)
090: finalizeConstruction();
091: }
092:
093: public void finalizeConstruction() throws GUIException {
094: if (type.equals(TYPE_GLUE)) {
095: if (axis.equals(AXIS_HORIZONTAL)) {
096: minimumSize = new Dimension(0, 0);
097: preferredSize = new Dimension(0, 0);
098: maximumSize = new Dimension(Short.MAX_VALUE, 0);
099: } else if (axis.equals(AXIS_VERTICAL)) {
100: minimumSize = new Dimension(0, 0);
101: preferredSize = new Dimension(0, 0);
102: maximumSize = new Dimension(0, Short.MAX_VALUE);
103: } else {
104: throw new GUIException("Unknown spacer axis [" + axis
105: + "]");
106: }
107: } else if (type.equals(TYPE_STRUT)) {
108: if (axis.equals(AXIS_HORIZONTAL)) {
109: minimumSize = new Dimension(size, 0);
110: preferredSize = new Dimension(size, 0);
111: maximumSize = new Dimension(size, Short.MAX_VALUE);
112: } else if (axis.equals(AXIS_VERTICAL)) {
113: minimumSize = new Dimension(0, size);
114: preferredSize = new Dimension(0, size);
115: maximumSize = new Dimension(Short.MAX_VALUE, size);
116: } else {
117: throw new GUIException("Unknown spacer axis [" + axis
118: + "]");
119: }
120: } else {
121: throw new GUIException("Unknown spacer type [" + type + "]");
122: }
123: if (isConstructed)
124: spacer.invalidate();
125: isConstructed = true;
126: }
127:
128: public Component getWidget() {
129: return spacer;
130: }
131:
132: public WidgetInfo getWidgetInfo() {
133: return spacerInfo;
134: }
135: }
|