⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actioncheckboxmenuitem.java

📁 报表设计软件,很好的
💻 JAVA
字号:
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2002, by Simba Management Limited and Contributors.
 *
 * 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.
 *
 * -------------------
 * ActionMenuItem.java
 * -------------------
 * (C)opyright 2002, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: ActionCheckBoxMenuItem.java,v 1.2 2004/04/20 18:54:53 taqua Exp $
 *
 * ChangeLog
 * ---------
 * 30-Aug-2002 : Initial version
 * 01-Aug-2002 : Documentation
 * 10-Dec-2002 : Minor Javadoc updates (DG);
 *
 */

package org.jfree.designer.util;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.KeyStroke;

import org.jfree.report.modules.gui.base.components.ActionDowngrade;
import org.jfree.report.util.Log;

/**
 * The ActionMenuItem is used to connect an Action and its properties to an MenuItem. <P>
 * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2
 * compatibility.
 *
 * @author Thomas Morgner
 */
public final class ActionCheckBoxMenuItem
        extends JCheckBoxMenuItem
{
  /**
   * The action.
   */
  private Action action;

  /**
   * The property change handler.
   */
  private ActionEnablePropertyChangeHandler propertyChangeHandler;

  /**
   * Helperclass to handle the property change event raised by the action. Changed
   * properties in the action will affect the button.
   */
  private final class ActionEnablePropertyChangeHandler
          implements PropertyChangeListener
  {
    /**
     * Receives notification of a property change event.
     *
     * @param event the property change event.
     */
    public final void propertyChange (final PropertyChangeEvent event)
    {
      try
      {
        if (event.getPropertyName().equals("enabled"))
        {
          setEnabled(getAction().isEnabled());
        }
        else if (event.getPropertyName().equals(Action.SMALL_ICON))
        {
          setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
        }
        else if (event.getPropertyName().equals(Action.NAME))
        {
          setText((String) getAction().getValue(Action.NAME));
        }
        else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION))
        {
          ActionCheckBoxMenuItem.this.setToolTipText((String)
                  getAction().getValue(Action.SHORT_DESCRIPTION));
        }

        final Action ac = getAction();
        if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY))
        {
          final KeyStroke oldVal = (KeyStroke) event.getOldValue();
          if (oldVal != null)
          {
            unregisterKeyboardAction(oldVal);
          }
          final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
          if (o instanceof KeyStroke)
          {
            final KeyStroke k = (KeyStroke) o;
            registerKeyboardAction(ac, k, WHEN_IN_FOCUSED_WINDOW);
          }
        }
        else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY))
        {
          final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
          if (o instanceof Character)
          {
            final Character c = (Character) o;
            setMnemonic(c.charValue());
          }
          else if (o instanceof Integer)
          {
            final Integer c = (Integer) o;
            setMnemonic(c.intValue());
          }
        }
      }
      catch (Exception e)
      {
        Log.warn("Error on PropertyChange in ActionButton: ", e);
      }
    }
  }

  /**
   * Creates an initially unselected checkboxMenuItem with no set text or icon.
   */
  public ActionCheckBoxMenuItem ()
  {
  }

  /**
   * Creates an initially unselected checkboxMenuItem with an icon.
   *
   * @param icon the icon of the CheckBoxMenuItem.
   */
  public ActionCheckBoxMenuItem (final Icon icon)
  {
    super(icon);
  }

  /**
   * Creates an initially unselected checkboxMenuItem with text.
   *
   * @param text the text of the CheckBoxMenuItem
   */
  public ActionCheckBoxMenuItem (final String text)
  {
    super(text);
  }

  /**
   * Creates a checkboxMenuItem with the specified text and selection state.
   *
   * @param text the text of the CheckBoxMenuItem.
   * @param b    the selected state of the checkboxmenuitem
   */
  public ActionCheckBoxMenuItem (final String text, final boolean b)
  {
    super(text, b);
  }

  /**
   * Creates an initially unselected checkboxMenuItem with the specified text and icon.
   *
   * @param text the text of the CheckBoxMenuItem
   * @param icon the icon of the CheckBoxMenuItem
   */
  public ActionCheckBoxMenuItem (final String text, final Icon icon)
  {
    super(text, icon);
  }

  /**
   * Creates a checkboxMenuItem with the specified text, icon, and selection state.
   *
   * @param text the text of the CheckBoxMenuItem
   * @param icon the icon of the CheckBoxMenuItem
   * @param b    the selected state of the checkboxmenuitem
   */
  public ActionCheckBoxMenuItem (final String text, final Icon icon, final boolean b)
  {
    super(text, icon, b);
  }

  /**
   * Creates a new menu item based on the specified action.
   *
   * @param action the action.
   */
  public ActionCheckBoxMenuItem (final Action action)
  {
    setAction(action);
  }

  /**
   * Creates a new menu item based on the specified action.
   *
   * @param action the action.
   */
  public ActionCheckBoxMenuItem (final Action action, final boolean selected)
  {
    setSelected(selected);
    setAction(action);
  }

  /**
   * Returns the assigned action or null if no action has been assigned.
   *
   * @return the action.
   */
  public final Action getAction ()
  {
    return action;
  }

  /**
   * Returns and initializes the PropertyChangehandler for this ActionMenuItem. The
   * PropertyChangeHandler monitors the action and updates the menuitem if necessary.
   *
   * @return the property change handler.
   */
  private ActionEnablePropertyChangeHandler getPropertyChangeHandler ()
  {
    if (propertyChangeHandler == null)
    {
      propertyChangeHandler = new ActionEnablePropertyChangeHandler();
    }
    return propertyChangeHandler;
  }

  /**
   * Enables and disables this button and if an action is assigned to this menuitem the
   * propertychange is forwarded to the assigned action.
   *
   * @param b the new enable-state of this menuitem
   */
  public final void setEnabled (final boolean b)
  {
    super.setEnabled(b);
    if (getAction() != null)
    {
      getAction().setEnabled(b);
    }
  }

  /**
   * Assigns the given action to this menuitem. The properties of the action will be
   * assigned to the menuitem. If an previous action was set, the old action is
   * unregistered.
   * <p/>
   * <ul> <li>NAME - specifies the menuitem text <li>SMALL_ICON - specifies the menuitems
   * icon <li>MNEMONIC_KEY - specifies the menuitems mnemonic key <li>ACCELERATOR_KEY -
   * specifies the menuitems accelerator </ul>
   *
   * @param newAction the new action
   */
  public final void setAction (final Action newAction)
  {
    final Action oldAction = getAction();
    if (oldAction != null)
    {
      removeActionListener(oldAction);
      oldAction.removePropertyChangeListener(getPropertyChangeHandler());

      final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
      if (o instanceof KeyStroke)
      {
        final KeyStroke k = (KeyStroke) o;
        unregisterKeyboardAction(k);
      }
    }
    this.action = newAction;
    if (action != null)
    {
      addActionListener(newAction);
      newAction.addPropertyChangeListener(getPropertyChangeHandler());

      setText((String) (newAction.getValue(Action.NAME)));
      setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
      setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
      setEnabled(action.isEnabled());

      Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
      if (o != null)
      {
        if (o instanceof Character)
        {
          final Character c = (Character) o;
          setMnemonic(c.charValue());
        }
        else if (o instanceof Integer)
        {
          final Integer c = (Integer) o;
          setMnemonic(c.intValue());
        }
      }

      o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
      if (o instanceof KeyStroke)
      {
        final KeyStroke k = (KeyStroke) o;
        registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -