/*
* Beryl - A web platform based on XML, XSLT and Java
* This file is part of the Beryl XML GUI
*
* Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
*/
package org.beryl.gui.widgets;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import org.beryl.gui.GUIEvent;
import org.beryl.gui.GUIEventListener;
import org.beryl.gui.GUIException;
import org.beryl.gui.ImageIconFactory;
import org.beryl.gui.InternationalizationManager;
import org.beryl.gui.Widget;
import org.beryl.gui.WidgetInfo;
public class Button extends Widget {
protected static WidgetInfo buttonInfo = null;
private JButton button = null;
private boolean isDefault = false;
static {
buttonInfo = new WidgetInfo(Button.class, widgetInfo);
buttonInfo.addProperty("text", "istring", "");
buttonInfo.addProperty("mnemonic", "istring", "");
buttonInfo.addProperty("default", "bool", Boolean.FALSE);
buttonInfo.addPreset("ok");
buttonInfo.addPreset("cancel");
buttonInfo.addPreset("hover");
buttonInfo.addEvent("clicked");
try {
buttonInfo.addProperty("icon", "icon", ImageIconFactory.getIcon("broken"));
} catch (Exception e) {
throw new RuntimeException(e);
}
};
public Button(Widget parent, String name, String preset) throws GUIException {
this(parent, name);
if (preset.equals("cancel")) {
button.setText(InternationalizationManager.getString("xmlgui.button.cancel"));
button.setMnemonic(InternationalizationManager.getString("xmlgui.button.cancel.mnemonic").charAt(0));
button.setIcon(ImageIconFactory.getIcon("close"));
} else if (preset.equals("ok")) {
button.setText(InternationalizationManager.getString("xmlgui.button.ok"));
button.setMnemonic(InternationalizationManager.getString("xmlgui.button.ok.mnemonic").charAt(0));
button.setIcon(ImageIconFactory.getIcon("ok"));
} else if (preset.equals("hover")) {
button.setBorderPainted(false);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (button.isEnabled())
button.setBorderPainted(true);
}
public void mouseExited(MouseEvent e) {
if (button.isEnabled())
button.setBorderPainted(false);
}
});
} else {
throw new GUIException("Unknown preset [" + preset + "]");
}
}
public Button(Widget parent, String name) throws GUIException {
super(parent, name);
button = new JButton();
// necessary for Alloy L&F
button.setContentAreaFilled(false);
}
public void addListener(String event, final String name, final GUIEventListener listener) throws GUIException {
if ("clicked".equals(event)) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
listener.eventOccured(new GUIEvent(Button.this, name, e));
}
});
} else {
super.addListener(event, name, listener);
}
}
public void setProperty(String name, Object value) throws GUIException {
if (name.equals("mnemonic")) {
if (((String) value).length() > 0)
button.setMnemonic(((String) value).charAt(0));
else
button.setMnemonic('\0');
} else if (name.equals("default")) {
Widget widget = getParentWidgetByClass(Frame.class);
if (widget == null) {
widget = getParentWidgetByClass(Dialog.class);
}
if (widget == null) {
throw new GUIException("Could not find root window");
}
if (((Boolean) value).booleanValue()) {
widget.setProperty("default", this);
} else {
if (widget.getProperty("default") == this)
widget.setProperty("default", null);
}
} else {
super.setProperty(name, value);
}
}
public Component getWidget() {
return button;
}
public WidgetInfo getWidgetInfo() {
return buttonInfo;
}
}