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.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.MouseAdapter;
028: import java.awt.event.MouseEvent;
029:
030: import javax.swing.Icon;
031: import javax.swing.JButton;
032:
033: import org.beryl.gui.GUIEvent;
034: import org.beryl.gui.GUIEventListener;
035: import org.beryl.gui.GUIException;
036: import org.beryl.gui.ImageIconFactory;
037: import org.beryl.gui.InternationalizationManager;
038: import org.beryl.gui.Widget;
039: import org.beryl.gui.WidgetInfo;
040:
041: public class Button extends Widget {
042: protected static WidgetInfo buttonInfo = null;
043: private JButton button = null;
044: private boolean isDefault = false;
045:
046: static {
047: buttonInfo = new WidgetInfo(Button.class, widgetInfo);
048: try {
049: buttonInfo.addProperty("rolloverIcon", "icon",
050: ImageIconFactory.getIcon("broken"));
051: buttonInfo.addProperty("icon", "icon", ImageIconFactory
052: .getIcon("broken"));
053: } catch (Exception e) {
054: throw new RuntimeException(e);
055: }
056: buttonInfo.addProperty("mnemonic", "istring", "");
057: buttonInfo.addProperty("default", "bool", Boolean.FALSE);
058: buttonInfo.addProperty("text", "istring", "");
059: buttonInfo.addPreset("ok");
060: buttonInfo.addPreset("cancel");
061: buttonInfo.addPreset("hover");
062: buttonInfo.addEvent("clicked");
063: };
064:
065: public Button(Widget parent, String name, String preset)
066: throws GUIException {
067: this (parent, name);
068:
069: if (preset.equals("cancel")) {
070: button.setText(InternationalizationManager
071: .getString("xmlgui.button.cancel"));
072: button.setMnemonic(InternationalizationManager.getString(
073: "xmlgui.button.cancel.mnemonic").charAt(0));
074: button.setIcon(ImageIconFactory.getIcon("close"));
075: } else if (preset.equals("ok")) {
076: button.setText(InternationalizationManager
077: .getString("xmlgui.button.ok"));
078: button.setMnemonic(InternationalizationManager.getString(
079: "xmlgui.button.ok.mnemonic").charAt(0));
080: button.setIcon(ImageIconFactory.getIcon("ok"));
081: } else if (preset.equals("hover")) {
082: button.setBorderPainted(false);
083: button.setOpaque(false);
084: button.setContentAreaFilled(false);
085: button.addMouseListener(new MouseAdapter() {
086: public void mouseEntered(MouseEvent e) {
087: if (button.isEnabled())
088: button.setBorderPainted(true);
089: }
090:
091: public void mouseExited(MouseEvent e) {
092: if (button.isEnabled())
093: button.setBorderPainted(false);
094: }
095: });
096: } else {
097: throw new GUIException("Unknown preset [" + preset + "]");
098: }
099: }
100:
101: public Button(Widget parent, String name) throws GUIException {
102: super (parent, name);
103: button = new JButton();
104: // necessary for Alloy L&F
105: button.setContentAreaFilled(false);
106: }
107:
108: public void addListener(String event, final String name,
109: final GUIEventListener listener) throws GUIException {
110: if ("clicked".equals(event)) {
111: button.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent e) {
113: listener.eventOccured(new GUIEvent(Button.this ,
114: name, e));
115: }
116: });
117: } else {
118: super .addListener(event, name, listener);
119: }
120: }
121:
122: public void setProperty(String name, Object value)
123: throws GUIException {
124: if (name.equals("mnemonic")) {
125: if (((String) value).length() > 0)
126: button.setMnemonic(((String) value).charAt(0));
127: else
128: button.setMnemonic('\0');
129: } else if (name.equals("default")) {
130: Widget widget = getParentWidgetByClass(Frame.class);
131: if (widget == null) {
132: widget = getParentWidgetByClass(Dialog.class);
133: }
134: if (widget == null) {
135: throw new GUIException("Could not find root window");
136: }
137: if (((Boolean) value).booleanValue()) {
138: widget.setProperty("default", this );
139: } else {
140: if (widget.getProperty("default") == this )
141: widget.setProperty("default", null);
142: }
143: } else if (name.equals("rolloverIcon")) {
144: button.setRolloverIcon((Icon) value);
145: button.setBorderPainted(false);
146: } else {
147: super .setProperty(name, value);
148: }
149: }
150:
151: public Component getWidget() {
152: return button;
153: }
154:
155: public WidgetInfo getWidgetInfo() {
156: return buttonInfo;
157: }
158: }
|