/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library 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 library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
/**
* This class provides a JMenu that gets enabled
* if at least one item belonging to it becomes enabled. It
* gets disabled if none of its menu items is enabled.
* The default initial state of this component is disabled.
*
* @author A. Benkwitz, Markus Kraetzig
*/
public final class AutoEnableMenu
extends JMenu
implements PropertyChangeListener {
/**
* See JMenu default constructor.
*/
public AutoEnableMenu() {
super();
setEnabled(false);
}
/**
* See JMenu(String s) constructor.
*/
public AutoEnableMenu(String s) {
super(s);
setEnabled(false);
}
/**
* See JMenu(String s, boolean b) constructor.
*/
public AutoEnableMenu(String s, boolean b) {
super(s, b);
setEnabled(false);
}
/**
* Overwrites superclass method to register itself as
* property change listener to newly added menus.
*
* @param item the item to add to this menu
*/
public JMenuItem add(JMenuItem item) {
super.add(item);
if (item.isEnabled())
setEnabled(true);
item.addPropertyChangeListener(this);
return item;
}
/**
* Checks, whether there are any enabled items
* and disables/enables this menu accordingly.
*
* @param e a property change event, responds only to
* enabled property
*/
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals("enabled")) {
boolean __enabled = false;
int n = getItemCount();
int i = 0;
while (!__enabled && i < n) {
JMenuItem item = getItem(i++);
if (!item.equals(this)) {
__enabled = item.isEnabled();
}
}
setEnabled(__enabled);
}
}
}
Ads help maintain this website.