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 javax.swing.ImageIcon;
025: import javax.swing.JLabel;
026:
027: import org.beryl.gui.GUIException;
028: import org.beryl.gui.ImageIconFactory;
029: import org.beryl.gui.Widget;
030: import org.beryl.gui.WidgetInfo;
031: import org.beryl.gui.validators.ValidationException;
032:
033: import cz.autel.dmi.HIGLayout;
034:
035: public class LabeledWidget extends Panel {
036: protected static WidgetInfo labeledWidgetInfo = null;
037: private static ImageIcon icon = null;
038: private static final int[] width = { 0,
039: LABEL_COMPONENT_SPACING_LEFT, DEFAULT_WIDTH };
040: private static final int[] heights = { DEFAULT_HEIGHT };
041: private Label label = null;
042: private Widget dataWidget = null;
043:
044: static {
045: labeledWidgetInfo = new WidgetInfo(LabeledWidget.class,
046: widgetInfo); /* Not inheriting from panelInfo */
047: labeledWidgetInfo.addProperty("label.text", "istring", "");
048: labeledWidgetInfo.addProperty("mnemonic", "istring", "");
049: icon = ImageIconFactory.getIcon("error_tiny");
050: };
051:
052: public LabeledWidget(Widget parent, String name)
053: throws GUIException {
054: super (parent, name);
055:
056: label = new Label(this , null);
057:
058: if (!(parent instanceof Group)) {
059: /* If the parent is a Group, let it handle the layout */
060: HIGLayout layout = new HIGLayout(width, heights);
061: layout.setColumnWeight(3, 1);
062: setProperty("layout", layout);
063: super .addChild(label, constraints.rc(1, 1));
064: }
065: }
066:
067: public void addChild(Widget widget, Object constraint)
068: throws GUIException {
069: if (constraint != null)
070: throw new GUIException(
071: "Anchors not supported inside a labeled dataWidget");
072: if (dataWidget != null)
073: throw new GUIException(
074: "A labeled component cannot have more than one child");
075: ((JLabel) label.getRealWidget()).setLabelFor(widget
076: .getRealWidget());
077: dataWidget = widget;
078:
079: if (!(getParentWidget() instanceof Group)) {
080: super .addChild(widget, constraints.rc(1, 3));
081: } else {
082: addChild(widget);
083: }
084: }
085:
086: public void setProperty(String name, Object value)
087: throws GUIException {
088: if (name.startsWith("label.")) {
089: label.setProperty(name.substring(6), value);
090: } else if (name.equals("mnemonic")) {
091: ((JLabel) label.getRealWidget())
092: .setDisplayedMnemonic(((String) value).charAt(0));
093: } else {
094: super .setProperty(name, value);
095: }
096: }
097:
098: public Label getLabel() {
099: return label;
100: }
101:
102: public Widget getDataWidget() throws GUIException {
103: if (dataWidget == null)
104: return new Panel(this , null);
105: return dataWidget;
106: }
107:
108: public void clearError() throws GUIException {
109: label.setIcon(null);
110: label.setTooltipText(null);
111: }
112:
113: public void setError(ValidationException e) throws GUIException {
114: label.setIcon(icon);
115: label.setTooltipText(e.getMessage());
116: }
117:
118: public void revalidate() throws GUIException {
119: if (getParentWidget() instanceof Group)
120: ((Group) getParentWidget()).revalidate();
121: else
122: super .revalidate();
123: }
124:
125: public WidgetInfo getWidgetInfo() {
126: return labeledWidgetInfo;
127: }
128: }
|