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.Color;
025: import java.awt.Component;
026: import java.awt.event.MouseEvent;
027: import java.awt.event.MouseListener;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.JButton;
033: import javax.swing.JLabel;
034: import javax.swing.border.Border;
035: import javax.swing.plaf.basic.BasicButtonUI;
036:
037: import org.beryl.gui.GUIException;
038: import org.beryl.gui.LFConstants;
039: import org.beryl.gui.Widget;
040: import org.beryl.gui.WidgetInfo;
041: import org.beryl.gui.swing.JCascadePanel;
042:
043: import cz.autel.dmi.HIGConstraints;
044: import cz.autel.dmi.HIGLayout;
045:
046: public class OutlookPanel extends Widget {
047: protected static WidgetInfo outlookPanelInfo = null;
048: private static Color backgroundColor = new Color(120, 120, 120);
049: private static Border fillerBorder = BorderFactory
050: .createEmptyBorder(2, 2, 2, 2);
051: private static Border raisedBorder = BorderFactory
052: .createCompoundBorder(BorderFactory
053: .createRaisedBevelBorder(), fillerBorder);
054: private static Border loweredBorder = BorderFactory
055: .createCompoundBorder(BorderFactory
056: .createLoweredBevelBorder(), fillerBorder);
057: private static Border emptyBorder = BorderFactory
058: .createEmptyBorder(4, 4, 4, 4);
059: private JCascadePanel panel = null;
060: private boolean isConstructed = false;
061: private JButton button = null;
062: private Map textForButton = null;
063:
064: static {
065: outlookPanelInfo = new WidgetInfo(OutlookPanel.class);
066: };
067:
068: public OutlookPanel(Widget parent, String name) throws GUIException {
069: super (parent, name);
070:
071: textForButton = new HashMap();
072: panel = new JCascadePanel();
073: button = new JButton("(unnamed)");
074: panel.setBackground(backgroundColor);
075: panel.setButton(button);
076: }
077:
078: public void addChild(Widget widget, Object constraint)
079: throws GUIException {
080: if (widget instanceof Button) {
081: final JButton button = (JButton) widget.getRealWidget();
082: button.setBorder(emptyBorder);
083: button.setForeground(backgroundColor);
084: button.setBackground(backgroundColor);
085: button.setUI(new BasicButtonUI());
086: button.addMouseListener(new MouseListener() {
087:
088: public void mouseClicked(MouseEvent e) {
089: }
090:
091: public void mousePressed(MouseEvent e) {
092: button.setBorder(loweredBorder);
093: }
094:
095: public void mouseReleased(MouseEvent e) {
096: if (button.contains(e.getPoint()))
097: button.setBorder(raisedBorder);
098: else
099: button.setBorder(emptyBorder);
100: }
101:
102: public void mouseEntered(MouseEvent e) {
103: button.setBorder(raisedBorder);
104: }
105:
106: public void mouseExited(MouseEvent e) {
107: button.setBorder(emptyBorder);
108: }
109: });
110: addChild(widget);
111: if (isConstructed)
112: finalizeConstruction();
113: } else {
114: throw new GUIException(
115: "OutlookPanel can only have Button children");
116: }
117: }
118:
119: public void removeChildWidget(Widget widget) throws GUIException {
120: super .removeChildWidget(widget);
121: textForButton.remove(widget.getRealWidget());
122: try {
123: if (isConstructed)
124: finalizeConstruction();
125: } catch (Exception e) {
126: throw new RuntimeException(e);
127: }
128: }
129:
130: public void setProperty(String name, Object value)
131: throws GUIException {
132: if (name.startsWith("text")) {
133: button.setText((String) value);
134: } else {
135: super .setProperty(name, value);
136: }
137: }
138:
139: public void finalizeConstruction() throws GUIException {
140: int childCount = getChildCount();
141: int heights[] = new int[childCount * 4 + 2];
142:
143: for (int i = 0; i < childCount; i++) {
144: heights[i * 4] = LFConstants.CONTENT_BUTTON_SPACING; // inter-button-spacing
145: heights[i * 4 + 1] = 0; // button
146: heights[i * 4 + 2] = LFConstants.COMPONENT_SPACING; // button-label spacing
147: heights[i * 4 + 3] = 0; // label
148: }
149: heights[childCount * 4 + 1] = heights[0] = 30; // upper + lower spacing
150:
151: HIGLayout layout = new HIGLayout(new int[] { 0, 0, 0 }, heights);
152: HIGConstraints constraints = new HIGConstraints();
153: layout.setColumnWeight(1, 1);
154: layout.setColumnWeight(3, 1);
155: layout.setRowWeight(0, 1);
156: layout.setRowWeight(childCount * 4 + 2, 1);
157: panel.removeAll();
158: panel.setLayout(layout);
159:
160: for (int i = 0; i < childCount; i++) {
161: JButton button = (JButton) getChild(i).getRealWidget();
162: String text = button.getText();
163: if (text == null)
164: text = (String) textForButton.get(button);
165: if (text != null)
166: textForButton.put(button, text);
167: button.setText(null);
168: panel.add(button, constraints.rc(i * 4 + 2, 2));
169: JLabel label = new JLabel(text);
170: label.setForeground(Color.white);
171: panel.add(label, constraints.rcwh(i * 4 + 4, 1, 3, 1, ""));
172: }
173: isConstructed = true;
174: }
175:
176: public Component getWidget() {
177: return panel;
178: }
179:
180: public WidgetInfo getWidgetInfo() {
181: return outlookPanelInfo;
182: }
183: }
|