// Sample code file: ButtonPanel.java
// Warning: This code has been marked up for HTML
/***************************************************************************
$name: ButtonPanel.java
$version: 1.0
$date_modified: 210104
$description:
$owner: Beans for Novell Services Team
Copyright (c) 1998 Novell, Inc. All Rights Reserved.
THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
class ButtonPanel extends FramedPanel
implements ActionListener
{
private JButton[] buttons = null;
private Vector listeners = null;
private boolean sizesAreSet = false;
public ButtonPanel(int width, int height)
{
super(width, height);
setLayout(null);
}
public ButtonPanel(int width, int height, String[] aBtnLabelArray)
{
super(width, height);
setLayout(null);
setButtons(aBtnLabelArray);
}
public void actionPerformed(ActionEvent evt)
{
if (buttons != null) {
for (int i = 0; i < buttons.length; i++) {
if (evt.getSource() == buttons[i]) {
fireButtonPressed(buttons[i]);
return;
}
}
}
}
protected void fireButtonPressed(JButton aButton)
{
if ((listeners == null) || (listeners.size() <= 0)) {
return;
}
ButtonEvent btnEvt = new ButtonEvent(this, aButton);
Vector listenerObjects;
synchronized(this) {
listenerObjects = (Vector)listeners.clone();
}
for (int i = 0; i < listenerObjects.size(); i++) {
ButtonListener l = (ButtonListener)listenerObjects.elementAt(i);
l.buttonPressed(btnEvt);
}
}
void setSizes(FontMetrics fm)
{
if (buttons == null) {
return;
}
Dimension d = getSize();
int xPos = 6;
for (int i = 0; i < buttons.length; i++) {
int strWidth = fm.stringWidth(buttons[i].getText());
buttons[i].setBounds(xPos, 5, strWidth + 28, d.height - 9);
xPos += strWidth + 29;
}
sizesAreSet = true;
}
public void setBounds(int x, int y, int width, int height)
{
super.setBounds(x, y, width, height);
sizesAreSet = false;
if (buttons != null) {
Font theFont = getFont();
if (theFont != null) {
setSizes(getToolkit().getFontMetrics(theFont));
invalidate();
validate();
}
}
}
public void setBounds(Rectangle newBounds)
{
setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height);
}
public void setSize(int width, int height)
{
super.setSize(width, height);
sizesAreSet = false;
if (buttons != null) {
Font theFont = getFont();
if (theFont != null) {
setSizes(getToolkit().getFontMetrics(theFont));
invalidate();
validate();
}
}
}
public void setSize(Dimension newSize)
{
setSize(newSize.width, newSize.height);
}
public void paint(Graphics g)
{
if (sizesAreSet == false) {
setSizes(g.getFontMetrics());
}
super.paint(g);
}
public void setButtons(String[] aBtnLabelArray)
{
buttons = new JButton[aBtnLabelArray.length];
for (int i = 0; i < aBtnLabelArray.length; i++) {
buttons[i] = new JButton(aBtnLabelArray[i]);
Font oldFont = buttons[i].getFont();
buttons[i].setFont(new Font(oldFont.getName(),
oldFont.getStyle(), 9));
add(buttons[i]);
buttons[i].setEnabled(false);
buttons[i].addActionListener(this);
}
}
public void enableButtons(String[] aBtnLabelArray, boolean enableFlag)
{
if (buttons != null) {
for (int i = 0; i < aBtnLabelArray.length; i++) {
for (int j = 0; j < buttons.length; j++) {
if (buttons[j].getText().equalsIgnoreCase(aBtnLabelArray[i])) {
buttons[j].setEnabled(enableFlag);
}
}
}
}
}
public void addButtonListener(ButtonListener l)
{
if (listeners == null) {
listeners = new Vector();
}
if (listeners.indexOf(l) >= 0) {
return; // already in list
}
listeners.addElement(l);
}
public void removeButtonListener(ButtonListener l)
{
if (listeners == null) {
return;
}
listeners.removeElement(l);
}
}