1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.swing.plaf.windows; 27 28 import java.awt.*; 29 import javax.swing.*; 30 import javax.swing.plaf.*; 31 import javax.swing.plaf.basic.*; 32 33 import sun.swing.SwingUtilities2; 34 35 import com.sun.java.swing.plaf.windows.TMSchema.*; 36 import com.sun.java.swing.plaf.windows.XPStyle.*; 37 38 /** 39 * Windows rendition of the component. 40 * <p> 41 * <strong>Warning:</strong> 42 * Serialized objects of this class will not be compatible with 43 * future Swing releases. The current serialization support is appropriate 44 * for short term storage or RMI between applications running the same 45 * version of Swing. A future release of Swing will provide support for 46 * long term persistence. 47 * 48 * @author Igor Kushnirskiy 49 */ 50 51 public class WindowsMenuItemUI extends BasicMenuItemUI { 52 final WindowsMenuItemUIAccessor accessor = 53 new WindowsMenuItemUIAccessor() { 54 55 public JMenuItem getMenuItem() { 56 return menuItem; 57 } 58 59 public State getState(JMenuItem menuItem) { 60 return WindowsMenuItemUI.getState(this, menuItem); 61 } 62 63 public Part getPart(JMenuItem menuItem) { 64 return WindowsMenuItemUI.getPart(this, menuItem); 65 } 66 }; 67 public static ComponentUI createUI(JComponent c) { 68 return new WindowsMenuItemUI(); 69 } 70 71 /** 72 * Method which renders the text of the current menu item. 73 * <p> 74 * @param g Graphics context 75 * @param menuItem Current menu item to render 76 * @param textRect Bounding rectangle to render the text. 77 * @param text String to render 78 */ 79 protected void paintText(Graphics g, JMenuItem menuItem, 80 Rectangle textRect, String text) { 81 if (WindowsMenuItemUI.isVistaPainting()) { 82 WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text); 83 return; 84 } 85 ButtonModel model = menuItem.getModel(); 86 Color oldColor = g.getColor(); 87 88 if(model.isEnabled() && 89 (model.isArmed() || (menuItem instanceof JMenu && 90 model.isSelected()))) { 91 g.setColor(selectionForeground); // Uses protected field. 92 } 93 94 WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0); 95 96 g.setColor(oldColor); 97 } 98 99 @Override 100 protected void paintBackground(Graphics g, JMenuItem menuItem, 101 Color bgColor) { 102 if (WindowsMenuItemUI.isVistaPainting()) { 103 WindowsMenuItemUI.paintBackground(accessor, g, menuItem, bgColor); 104 return; 105 } 106 super.paintBackground(g, menuItem, bgColor); 107 } 108 109 static void paintBackground(WindowsMenuItemUIAccessor menuItemUI, 110 Graphics g, JMenuItem menuItem, Color bgColor) { 111 XPStyle xp = XPStyle.getXP(); 112 assert isVistaPainting(xp); 113 if (isVistaPainting(xp)) { 114 int menuWidth = menuItem.getWidth(); 115 int menuHeight = menuItem.getHeight(); 116 if (menuItem.isOpaque()) { 117 Color oldColor = g.getColor(); 118 g.setColor(menuItem.getBackground()); 119 g.fillRect(0,0, menuWidth, menuHeight); 120 g.setColor(oldColor); 121 } 122 Part part = menuItemUI.getPart(menuItem); 123 Skin skin = xp.getSkin(menuItem, part); 124 skin.paintSkin(g, 0 , 0, 125 menuWidth, 126 menuHeight, 127 menuItemUI.getState(menuItem)); 128 } 129 } 130 131 static void paintText(WindowsMenuItemUIAccessor menuItemUI, Graphics g, 132 JMenuItem menuItem, Rectangle textRect, 133 String text) { 134 assert isVistaPainting(); 135 if (isVistaPainting()) { 136 State state = menuItemUI.getState(menuItem); 137 138 /* part of it copied from WindowsGraphicsUtils.java */ 139 FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g); 140 int mnemIndex = menuItem.getDisplayedMnemonicIndex(); 141 // W2K Feature: Check to see if the Underscore should be rendered. 142 if (WindowsLookAndFeel.isMnemonicHidden() == true) { 143 mnemIndex = -1; 144 } 145 WindowsGraphicsUtils.paintXPText(menuItem, 146 menuItemUI.getPart(menuItem), state, 147 g, textRect.x, 148 textRect.y + fm.getAscent(), 149 text, mnemIndex); 150 } 151 } 152 153 static State getState(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) { 154 State state; 155 ButtonModel model = menuItem.getModel(); 156 if (model.isArmed()) { 157 state = (model.isEnabled()) ? State.HOT : State.DISABLEDHOT; 158 } else { 159 state = (model.isEnabled()) ? State.NORMAL : State.DISABLED; 160 } 161 return state; 162 } 163 164 static Part getPart(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) { 165 return Part.MP_POPUPITEM; 166 } 167 168 /* 169 * TODO idk can we use XPStyle.isVista? 170 * is it possible that in some theme some Vista parts are not defined while 171 * others are? 172 */ 173 static boolean isVistaPainting(final XPStyle xp) { 174 return xp != null && xp.isSkinDefined(null, Part.MP_POPUPITEM); 175 } 176 177 static boolean isVistaPainting() { 178 return isVistaPainting(XPStyle.getXP()); 179 } 180 }