4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/Aimport java.beans.*;
4632N/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.plaf.*;
4632N/Aimport javax.swing.plaf.basic.BasicMenuItemUI;
4632N/A
4632N/Aimport apple.laf.JRSUIConstants.Size;
4632N/A
4632N/A// TODO: no screen menu bar for now
4632N/Apublic class AquaMenuItemUI extends BasicMenuItemUI implements AquaMenuPainter.Client/*, ScreenMenuItemUI*/ {
4632N/A static final int kPlain = 0, kCheckBox = 1, kRadioButton = 2;
4632N/A static final String sPropertyPrefixes[] = { "MenuItem", "CheckBoxMenuItem", "RadioButtonMenuItem" };
4632N/A
4632N/A boolean fIsScreenMenuItem = false;
4632N/A boolean fIsIndeterminate = false;
4632N/A int fType;
4632N/A
4632N/A AquaMenuItemUI(final int type) {
4632N/A super();
4632N/A fType = type;
4632N/A }
4632N/A
4632N/A public static ComponentUI createUI(final JComponent c) {
4632N/A int type = kPlain;
4632N/A if (c instanceof JCheckBoxMenuItem) type = kCheckBox;
4632N/A if (c instanceof JRadioButtonMenuItem) type = kRadioButton;
4632N/A return new AquaMenuItemUI(type);
4632N/A }
4632N/A
4632N/A // The only real difference between the three is which property prefix it returns
4632N/A // and therefore which icons!
4632N/A protected String getPropertyPrefix() {
4632N/A return sPropertyPrefixes[fType];
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected void installListeners() {
4632N/A super.installListeners();
4632N/A IndeterminateListener.install(menuItem);
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected void uninstallListeners() {
4632N/A IndeterminateListener.uninstall(menuItem);
4632N/A super.uninstallListeners();
4632N/A }
4632N/A
4632N/A public void updateListenersForScreenMenuItem() {
4632N/A setIsScreenMenu(true);
4632N/A }
4632N/A
4632N/A // Users can dynamically change the kind of menu we're on by calling JPopupMenu.setInvoker
4632N/A // so we need to be prepared to put the listeners back on
4632N/A protected void setIsScreenMenu(final boolean isScreenMenuItem) {
4632N/A if (fIsScreenMenuItem != isScreenMenuItem) {
4632N/A fIsScreenMenuItem = isScreenMenuItem;
4632N/A if (fIsScreenMenuItem) removeListeners();
4632N/A else addListeners();
4632N/A }
4632N/A }
4632N/A
4632N/A protected void removeListeners() {
4632N/A menuItem.removeMouseListener(mouseInputListener);
4632N/A menuItem.removeMouseMotionListener(mouseInputListener);
4632N/A menuItem.removeMenuDragMouseListener(menuDragMouseListener);
4632N/A }
4632N/A
4632N/A protected void addListeners() {
4632N/A menuItem.addMouseListener(mouseInputListener);
4632N/A menuItem.addMouseMotionListener(mouseInputListener);
4632N/A menuItem.addMenuDragMouseListener(menuDragMouseListener);
4632N/A }
4632N/A
4632N/A protected void paintMenuItem(final Graphics g, final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final Color background, final Color foreground, final int localDefaultTextIconGap) {
4632N/A AquaMenuPainter.instance().paintMenuItem(this, g, c, localCheckIcon, localArrowIcon, background, foreground, disabledForeground, selectionForeground, localDefaultTextIconGap, acceleratorFont);
4632N/A }
4632N/A
4632N/A protected Dimension getPreferredMenuItemSize(final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final int localDefaultTextIconGap) {
4632N/A return AquaMenuPainter.instance().getPreferredMenuItemSize(c, localCheckIcon, localArrowIcon, localDefaultTextIconGap, acceleratorFont);
4632N/A }
4632N/A
4632N/A public void update(final Graphics g, final JComponent c) {
4632N/A if (c.isOpaque()) {
4632N/A // sja fix ((PenGraphics)g).alphaClearRect(0,0,c.getWidth(),c.getHeight());
4632N/A final Color oldColor = g.getColor();
4632N/A g.setColor(c.getBackground());
4632N/A g.fillRect(0, 0, c.getWidth(), c.getHeight());
4632N/A g.setColor(oldColor);
4632N/A }
4632N/A
4632N/A paint(g, c);
4632N/A }
4632N/A
4632N/A public void paintBackground(final Graphics g, final JComponent c, final int menuWidth, final int menuHeight) {
4632N/A if ((c.getParent() instanceof JMenuBar)) return;
4632N/A final Color oldColor = g.getColor();
4632N/A
4632N/A g.setColor(c.getBackground());
4632N/A g.fillRect(0, 0, menuWidth, menuHeight);
4632N/A if (((JMenuItem)c).isBorderPainted()) {
4632N/A if (((JMenuItem)c).getModel().isArmed()) {
4632N/A AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, menuWidth, menuHeight);
4632N/A }
4632N/A //getTheme().drawMenuItem(c, g, 0, 0, menuWidth, menuHeight);
4632N/A } else {
4632N/A // If selected, use black (see AquaLookAndFeel "Menu.selectionBackground")
4632N/A if (((JMenuItem)c).getModel().isArmed()) {
4632N/A final Color holdc = g.getColor();
4632N/A g.setColor(Color.black);
4632N/A g.fillRect(0, 0, menuWidth, menuHeight);
4632N/A g.setColor(holdc);
4632N/A } else {
4632N/A g.setColor(Color.green);
4632N/A g.fillRect(0, 0, menuWidth, menuHeight);
4632N/A //super.paintBackground(g,c,menuWidth, menuHeight); //getTheme().drawMenuBackground((Component)c, g, (short)1, 0, 0, menuWidth, menuHeight);
4632N/A }
4632N/A }
4632N/A g.setColor(oldColor);
4632N/A }
4632N/A
4632N/A protected void doClick(final MenuSelectionManager msm) {
4632N/A final Dimension size = menuItem.getSize();
4632N/A AquaUtils.blinkMenu(new AquaUtils.Selectable() {
4632N/A public void paintSelected(final boolean selected) {
4632N/A menuItem.setArmed(selected);
4632N/A menuItem.paintImmediately(0, 0, size.width, size.height);
4632N/A }
4632N/A });
4632N/A super.doClick(msm);
4632N/A }
4632N/A
4632N/A static final IndeterminateListener INDETERMINATE_LISTENER = new IndeterminateListener();
4632N/A static class IndeterminateListener implements PropertyChangeListener {
4632N/A static final String CLIENT_PROPERTY_KEY = "JMenuItem.selectedState";
4632N/A
4632N/A static void install(final JMenuItem menuItem) {
4632N/A menuItem.addPropertyChangeListener(CLIENT_PROPERTY_KEY, INDETERMINATE_LISTENER);
4632N/A apply(menuItem, menuItem.getClientProperty(CLIENT_PROPERTY_KEY));
4632N/A }
4632N/A
4632N/A static void uninstall(final JMenuItem menuItem) {
4632N/A menuItem.removePropertyChangeListener(CLIENT_PROPERTY_KEY, INDETERMINATE_LISTENER);
4632N/A }
4632N/A
4632N/A public void propertyChange(final PropertyChangeEvent evt) {
4632N/A final String key = evt.getPropertyName();
4632N/A if (!CLIENT_PROPERTY_KEY.equalsIgnoreCase(key)) return;
4632N/A
4632N/A final Object source = evt.getSource();
4632N/A if (!(source instanceof JMenuItem)) return;
4632N/A
4632N/A final JMenuItem c = (JMenuItem)source;
4632N/A apply(c, evt.getNewValue());
4632N/A }
4632N/A
4632N/A static void apply(final JMenuItem menuItem, final Object value) {
4632N/A final ButtonUI ui = menuItem.getUI();
4632N/A if (!(ui instanceof AquaMenuItemUI)) return;
4632N/A
4632N/A final AquaMenuItemUI aquaUI = (AquaMenuItemUI)ui;
4632N/A
4632N/A if (aquaUI.fIsIndeterminate = "indeterminate".equals(value)) {
4632N/A aquaUI.checkIcon = UIManager.getIcon(aquaUI.getPropertyPrefix() + ".dashIcon");
4632N/A } else {
4632N/A aquaUI.checkIcon = UIManager.getIcon(aquaUI.getPropertyPrefix() + ".checkIcon");
4632N/A }
4632N/A }
4632N/A
4632N/A public static boolean isIndeterminate(final JMenuItem menuItem) {
4632N/A return "indeterminate".equals(menuItem.getClientProperty(CLIENT_PROPERTY_KEY));
4632N/A }
4632N/A }
4632N/A}