net.roydesign.ui.JScreenMenuItem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mrjadapter Show documentation
Show all versions of mrjadapter Show documentation
MRJ Adapter is a wrapper around built in Java Virtual Machine APIs provided by Apple.
The newest version!
/*******************************************************************************
File: JScreenMenuItem.java
Author: Steve Roy
Part of MRJ Adapter, a unified API for easy integration of Mac OS specific
functionality within your cross-platform Java application.
This library is open source and can be modified and/or distributed under
the terms of the Artistic License.
Change History:
11/26/02 Created this file - Steve
02/26/04 Merged into MRJ Adapter - Steve
04/16/04 Renamed from JMenuItem to JScreenMenuItem - Steve
*******************************************************************************/
package net.roydesign.ui;
import net.roydesign.mac.MRJAdapter;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* A subclass of {@code javax.swing.JMenuItem} that adds the logistics
* needed to make menu bars conform to the Mac OS screen menu bar requirements
* without sacrificing the usual way of presenting menu bars on other platforms.
* See the class {@code JScreenMenuBar} for more details.
*
* As a convenience, this class implements support for the
* setAction() method for versions of Swing that didn't include it.
*
* @see JScreenMenuBar
*
* @version MRJ Adapter 1.2
*/
public class JScreenMenuItem extends JMenuItem
{
/**
* The action to be executed by this menu item. This is only used when
* running in a version of Java prior to 1.3, where this feature didn't
* exist. In 1.3 and up, this functionality is built-in and this is not used.
*/
private Action actionBefore13;
/**
* The object that listens for property change events coming from
* the action attached to the menu item. This is only used when
* running in a version of Java prior to 1.3.
*/
private final PropertyChangeListener actionPropertyChangeListener =
e -> {
if ("action".equals(e.getPropertyName()))
configurePropertiesFromAction((Action)e.getNewValue());
};
/**
* The user frames of this menu item.
*/
private final List> userFrames = new ArrayList<>();
/**
* Construct a menuItem with no set text or icon.
*/
public JScreenMenuItem()
{
}
/**
* Construct a menuItem with an icon.
* @param icon the icon of the menu item
*/
public JScreenMenuItem(Icon icon)
{
super(icon);
}
/**
* Construct a menuItem with text.
* @param text the text of the menu item
*/
public JScreenMenuItem(String text)
{
super(text);
}
/**
* Construct a menu item whose properties are taken from the
* given action.
* @param action the assigned action
*/
public JScreenMenuItem(Action action)
{
setAction(action);
}
/**
* Construct a menu item with the supplied text and icon.
* @param text the text of the menu item
* @param icon the icon of the menu item
*/
public JScreenMenuItem(String text, Icon icon)
{
super(text, icon);
}
/**
* Construct a menuItem with the specified text and
* keyboard mnemonic.
* @param text the text of the menu item
* @param mnemonic the keyboard mnemonic for the menu item
*/
public JScreenMenuItem(String text, int mnemonic)
{
super(text, mnemonic);
}
/**
* Sets the action to be performed by the menu item. This method
* is only used when running in a version of Java prior to 1.3.
* @param action the action to be performed by this menu item
*/
private void setActionBefore13(Action action)
{
Action oldAction = this.actionBefore13;
if (!Objects.equals(oldAction, action))
{
this.actionBefore13 = action;
if (oldAction != null)
{
removeActionListener(oldAction);
oldAction.removePropertyChangeListener(actionPropertyChangeListener);
}
configurePropertiesFromAction(this.actionBefore13);
if (this.actionBefore13 != null)
{
addActionListener(this.actionBefore13);
this.actionBefore13.addPropertyChangeListener(actionPropertyChangeListener);
}
firePropertyChange("action", oldAction, this.actionBefore13);
revalidate();
repaint();
}
}
/**
* Add the given {@code JFrame} subclass as a user
* of the menu item. When a menu item has no user frames, then all
* frames get the menu item.
*
* @param frameClass the {@code JFrame} subclass
*/
public void addUserFrame(Class extends JFrame> frameClass) {
userFrames.add(frameClass);
}
/**
* Remove the given {@code JFrame} subclass from the user
* frames of the menu item.
*
* @param frameClass the {@code JFrame} subclass
*/
public void removeUserFrame(Class extends JFrame> frameClass) {
userFrames.remove(frameClass);
}
/**
* Get whether the menu item is used by the given frame instance.
* @return whether the menu item is used by the given frame
*/
public boolean isUsedBy(JFrame frame)
{
return userFrames.contains(frame.getClass());
}
}