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.util.ArrayList;
025:
026: import javax.swing.BorderFactory;
027: import javax.swing.Box;
028: import javax.swing.BoxLayout;
029: import javax.swing.JPanel;
030: import javax.swing.border.Border;
031:
032: import org.beryl.gui.GUIException;
033: import org.beryl.gui.Widget;
034: import org.beryl.gui.WidgetInfo;
035:
036: import cz.autel.dmi.HIGLayout;
037:
038: /**
039: * A Group is an intelligent panel which lays out LabeledWidgets. It takes
040: * care of spacing according to the Java Look and Feel Design Guidelines.
041: * Child widgets which are no labeled widgets are placed on the right side
042: * as if they had no label. Buttons are placed at the bottom.
043: */
044:
045: public class Group extends Panel {
046: protected static WidgetInfo groupInfo = null;
047: private static final Border GROUP_BORDER = BorderFactory
048: .createEmptyBorder(2, 2, 2, 2);
049: private static final int[] widths = { 0,
050: LABEL_COMPONENT_SPACING_LEFT, DEFAULT_WIDTH };
051: private ArrayList widgets = null;
052: private ArrayList buttons = null;
053:
054: static {
055: groupInfo = new WidgetInfo(Panel.class, panelInfo);
056: };
057:
058: public Group(Widget parent, String name) throws GUIException {
059: super (parent, name);
060: buttons = new ArrayList();
061: widgets = new ArrayList();
062: panel.setBorder(GROUP_BORDER);
063: }
064:
065: public void addChild(Widget widget, Object constraint)
066: throws GUIException {
067: if (constraint != null)
068: throw new GUIException(
069: "Constraints not supported inside a group");
070: addChild(widget);
071: if (widget instanceof Button)
072: buttons.add(widget);
073: else
074: widgets.add(widget);
075: }
076:
077: /* Calculate a layout and add the widgets */
078: public void finalizeConstruction() throws GUIException {
079: int height = widgets.size() * 2 + (buttons.size() > 0 ? 2 : 0); // hack to fix annoying cut-off buttons occuring in some dialogs
080: int heights[] = new int[height];
081:
082: for (int i = 0; i < widgets.size(); i++) {
083: heights[i * 2] = 0;
084: if (i > 1
085: && !widgets.get(i).getClass().equals(
086: widgets.get(i - 1).getClass())) {
087: heights[i * 2 + 1] = COMPONENT_GROUP_SPACING;
088: } else {
089: heights[i * 2 + 1] = COMPONENT_SPACING;
090: }
091: }
092: if (widgets.size() > 0) {
093: heights[widgets.size() * 2 - 1] = buttons.size() > 0 ? CONTENT_BUTTON_SPACING
094: : 0;
095: if (buttons.size() > 0)
096: heights[heights.length - 1] = 3; // hack to fix annoying cut-off buttons occuring in some dialogs
097: }
098:
099: HIGLayout layout = new HIGLayout(widths, heights);
100: layout.setRowWeight(widgets.size() * 2, 1);
101: layout.setColumnWeight(3, 1);
102: panel.setLayout(layout);
103:
104: for (int i = 0; i < widgets.size(); i++) {
105: Widget widget = (Widget) widgets.get(i);
106: if (widget instanceof LabeledWidget) {
107: LabeledWidget lw = (LabeledWidget) widget;
108: if (lw.getDataWidget().getWidget().getPreferredSize().height > DEFAULT_HEIGHT) {
109: panel.add(lw.getLabel().getWidget(), constraints
110: .rc(i * 2 + 1, 1, "rlt"));
111: } else {
112: panel.add(lw.getLabel().getWidget(), constraints
113: .rc(i * 2 + 1, 1));
114: }
115: panel.add(lw.getDataWidget().getWidget(), constraints
116: .rc(i * 2 + 1, 3));
117: } else if (widget instanceof Separator) {
118: panel.add(widget.getWidget(), constraints.rcwh(
119: i * 2 + 1, 1, 3, 1));
120: } else {
121: panel.add(widget.getWidget(), constraints.rc(i * 2 + 1,
122: 3, "rtb"));
123: }
124: }
125: if (buttons.size() > 0) {
126: JPanel buttonLayout = new JPanel();
127: buttonLayout.setLayout(new BoxLayout(buttonLayout,
128: BoxLayout.X_AXIS));
129: buttonLayout.add(Box.createHorizontalGlue());
130: for (int i = 0; i < buttons.size(); i++) {
131: buttonLayout.add(Box
132: .createHorizontalStrut(COMPONENT_SPACING));
133: buttonLayout.add(((Button) buttons.get(i)).getWidget());
134: }
135: panel.add(buttonLayout, constraints.rcwh(
136: widgets.size() * 2 + 1, 1, 3, 1, "r"));
137: }
138: }
139:
140: public void removeChildWidget(Widget widget) throws GUIException {
141: if (widget instanceof Button)
142: buttons.remove(widget);
143: else
144: widgets.remove(widget);
145: super .removeChildWidget(widget);
146: }
147:
148: public void revalidate() throws GUIException {
149: panel.removeAll();
150: finalizeConstruction();
151: super .revalidate();
152: }
153:
154: public WidgetInfo getWidgetInfo() {
155: return widgetInfo;
156: }
157: }
|