Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuItemUI.java
41154 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;28import java.beans.*;2930import javax.swing.*;31import javax.swing.plaf.*;32import javax.swing.plaf.basic.BasicMenuItemUI;3334import apple.laf.JRSUIConstants.Size;3536// TODO: no screen menu bar for now37public class AquaMenuItemUI extends BasicMenuItemUI implements AquaMenuPainter.Client/*, ScreenMenuItemUI*/ {38static final int kPlain = 0, kCheckBox = 1, kRadioButton = 2;39static final String[] sPropertyPrefixes = { "MenuItem", "CheckBoxMenuItem", "RadioButtonMenuItem" };4041boolean fIsScreenMenuItem = false;42boolean fIsIndeterminate = false;43int fType;4445AquaMenuItemUI(final int type) {46super();47fType = type;48}4950public static ComponentUI createUI(final JComponent c) {51int type = kPlain;52if (c instanceof JCheckBoxMenuItem) type = kCheckBox;53if (c instanceof JRadioButtonMenuItem) type = kRadioButton;54return new AquaMenuItemUI(type);55}5657// The only real difference between the three is which property prefix it returns58// and therefore which icons!59protected String getPropertyPrefix() {60return sPropertyPrefixes[fType];61}6263@Override64protected void installListeners() {65super.installListeners();66IndeterminateListener.install(menuItem);67}6869@Override70protected void uninstallListeners() {71IndeterminateListener.uninstall(menuItem);72super.uninstallListeners();73}7475public void updateListenersForScreenMenuItem() {76setIsScreenMenu(true);77}7879// Users can dynamically change the kind of menu we're on by calling JPopupMenu.setInvoker80// so we need to be prepared to put the listeners back on81protected void setIsScreenMenu(final boolean isScreenMenuItem) {82if (fIsScreenMenuItem != isScreenMenuItem) {83fIsScreenMenuItem = isScreenMenuItem;84if (fIsScreenMenuItem) removeListeners();85else addListeners();86}87}8889protected void removeListeners() {90menuItem.removeMouseListener(mouseInputListener);91menuItem.removeMouseMotionListener(mouseInputListener);92menuItem.removeMenuDragMouseListener(menuDragMouseListener);93}9495protected void addListeners() {96menuItem.addMouseListener(mouseInputListener);97menuItem.addMouseMotionListener(mouseInputListener);98menuItem.addMenuDragMouseListener(menuDragMouseListener);99}100101protected void paintMenuItem(final Graphics g, final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final Color background, final Color foreground, final int localDefaultTextIconGap) {102AquaMenuPainter.instance().paintMenuItem(this, g, c, localCheckIcon, localArrowIcon, background, foreground, disabledForeground, selectionForeground, localDefaultTextIconGap, acceleratorFont);103}104105protected Dimension getPreferredMenuItemSize(final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final int localDefaultTextIconGap) {106return AquaMenuPainter.instance().getPreferredMenuItemSize(c, localCheckIcon, localArrowIcon, localDefaultTextIconGap, acceleratorFont);107}108109public void update(final Graphics g, final JComponent c) {110if (c.isOpaque()) {111// sja fix ((PenGraphics)g).alphaClearRect(0,0,c.getWidth(),c.getHeight());112final Color oldColor = g.getColor();113g.setColor(c.getBackground());114g.fillRect(0, 0, c.getWidth(), c.getHeight());115g.setColor(oldColor);116}117118paint(g, c);119}120121public void paintBackground(final Graphics g, final JComponent c, final int menuWidth, final int menuHeight) {122if ((c.getParent() instanceof JMenuBar)) return;123final Color oldColor = g.getColor();124125g.setColor(c.getBackground());126g.fillRect(0, 0, menuWidth, menuHeight);127if (((JMenuItem)c).isBorderPainted()) {128if (((JMenuItem)c).getModel().isArmed()) {129AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, menuWidth, menuHeight);130}131//getTheme().drawMenuItem(c, g, 0, 0, menuWidth, menuHeight);132} else {133// If selected, use black (see AquaLookAndFeel "Menu.selectionBackground")134if (((JMenuItem)c).getModel().isArmed()) {135final Color holdc = g.getColor();136g.setColor(Color.black);137g.fillRect(0, 0, menuWidth, menuHeight);138g.setColor(holdc);139} else {140g.setColor(Color.green);141g.fillRect(0, 0, menuWidth, menuHeight);142//super.paintBackground(g,c,menuWidth, menuHeight); //getTheme().drawMenuBackground((Component)c, g, (short)1, 0, 0, menuWidth, menuHeight);143}144}145g.setColor(oldColor);146}147148protected void doClick(final MenuSelectionManager msm) {149final Dimension size = menuItem.getSize();150AquaUtils.blinkMenu(new AquaUtils.Selectable() {151public void paintSelected(final boolean selected) {152menuItem.setArmed(selected);153menuItem.paintImmediately(0, 0, size.width, size.height);154}155});156super.doClick(msm);157}158159static final IndeterminateListener INDETERMINATE_LISTENER = new IndeterminateListener();160static class IndeterminateListener implements PropertyChangeListener {161static final String CLIENT_PROPERTY_KEY = "JMenuItem.selectedState";162163static void install(final JMenuItem menuItem) {164menuItem.addPropertyChangeListener(CLIENT_PROPERTY_KEY, INDETERMINATE_LISTENER);165apply(menuItem, menuItem.getClientProperty(CLIENT_PROPERTY_KEY));166}167168static void uninstall(final JMenuItem menuItem) {169menuItem.removePropertyChangeListener(CLIENT_PROPERTY_KEY, INDETERMINATE_LISTENER);170}171172public void propertyChange(final PropertyChangeEvent evt) {173final String key = evt.getPropertyName();174if (!CLIENT_PROPERTY_KEY.equalsIgnoreCase(key)) return;175176final Object source = evt.getSource();177if (!(source instanceof JMenuItem)) return;178179final JMenuItem c = (JMenuItem)source;180apply(c, evt.getNewValue());181}182183static void apply(final JMenuItem menuItem, final Object value) {184final ButtonUI ui = menuItem.getUI();185if (!(ui instanceof AquaMenuItemUI)) return;186187final AquaMenuItemUI aquaUI = (AquaMenuItemUI)ui;188189if (aquaUI.fIsIndeterminate = "indeterminate".equals(value)) {190aquaUI.checkIcon = UIManager.getIcon(aquaUI.getPropertyPrefix() + ".dashIcon");191} else {192aquaUI.checkIcon = UIManager.getIcon(aquaUI.getPropertyPrefix() + ".checkIcon");193}194}195196public static boolean isIndeterminate(final JMenuItem menuItem) {197return "indeterminate".equals(menuItem.getClientProperty(CLIENT_PROPERTY_KEY));198}199}200}201202203