1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.Component ; 11 import java.awt.Graphics ; 12 import java.awt.Insets ; 13 import java.awt.KeyEventPostProcessor ; 14 import java.awt.KeyboardFocusManager ; 15 import java.awt.Window ; 16 import java.awt.event.KeyEvent ; 17 import javax.swing.*; 18 import javax.swing.event.*; 19 import javax.swing.plaf.*; 20 import javax.swing.plaf.basic.*; 21 22 import com.sun.java.swing.plaf.windows.TMSchema.Part; 23 import com.sun.java.swing.plaf.windows.TMSchema.State; 24 import com.sun.java.swing.plaf.windows.XPStyle.Skin; 25 26 39 public class WindowsPopupMenuUI extends BasicPopupMenuUI { 40 41 static MnemonicListener mnemonicListener = null; 42 static final Object GUTTER_OFFSET_KEY = 43 new StringBuilder ("GUTTER_OFFSET_KEY"); 44 45 public static ComponentUI createUI(JComponent c) { 46 return new WindowsPopupMenuUI(); 47 } 48 49 public void installListeners() { 50 super.installListeners(); 51 if (! UIManager.getBoolean("Button.showMnemonics") && 52 mnemonicListener == null) { 53 54 mnemonicListener = new MnemonicListener(); 55 MenuSelectionManager.defaultManager(). 56 addChangeListener(mnemonicListener); 57 } 58 } 59 60 70 public Popup getPopup(JPopupMenu popupMenu, int x, int y) { 71 PopupFactory popupFactory = PopupFactory.getSharedInstance(); 72 return popupFactory.getPopup(popupMenu.getInvoker(), popupMenu, x, y); 73 } 74 75 static class MnemonicListener implements ChangeListener { 76 JRootPane repaintRoot = null; 77 78 public void stateChanged(ChangeEvent ev) { 79 MenuSelectionManager msm = (MenuSelectionManager)ev.getSource(); 80 MenuElement[] path = msm.getSelectedPath(); 81 if (path.length == 0) { 82 if(!WindowsLookAndFeel.isMnemonicHidden()) { 83 WindowsLookAndFeel.setMnemonicHidden(true); 85 if (repaintRoot != null) { 86 Window win = 87 SwingUtilities.getWindowAncestor(repaintRoot); 88 WindowsUtils.repaintMnemonicsInWindow(win); 89 } 90 } 91 } else { 92 Component c = (Component )path[0]; 93 if (c instanceof JPopupMenu) c = ((JPopupMenu)c).getInvoker(); 94 repaintRoot = SwingUtilities.getRootPane(c); 95 } 96 } 97 } 98 99 106 static int getTextOffset(JComponent c) { 107 int rv = -1; 108 return rv; 109 } 110 111 116 static int getSpanBeforeGutter() { 117 return 3; 118 } 119 120 125 static int getSpanAfterGutter() { 126 return 3; 127 } 128 129 134 static int getGutterWidth() { 135 int rv = 2; 136 XPStyle xp = XPStyle.getXP(); 137 if (xp != null) { 138 Skin skin = xp.getSkin(null, Part.MP_POPUPGUTTER); 139 rv = skin.getWidth(); 140 } 141 return rv; 142 } 143 144 152 private static boolean isLeftToRight(JComponent c) { 153 boolean leftToRight = true; 154 for (int i = c.getComponentCount() - 1; i >=0 && leftToRight; i-- ) { 155 leftToRight = 156 c.getComponent(i).getComponentOrientation().isLeftToRight(); 157 } 158 return leftToRight; 159 } 160 161 @Override 162 public void paint(Graphics g, JComponent c) { 163 167 if (WindowsMenuItemUI.isVistaPainting()) { 168 XPStyle xp = XPStyle.getXP(); 169 Skin skin = xp.getSkin(c, Part.MP_POPUPBACKGROUND); 170 skin.paintSkin(g, 0, 0, c.getWidth(),c.getHeight(), State.NORMAL); 171 int textOffset = getTextOffset(c); 172 if (textOffset >= 0 173 174 && isLeftToRight(c)) { 175 skin = xp.getSkin(c, Part.MP_POPUPGUTTER); 176 int gutterWidth = getGutterWidth(); 177 int gutterOffset = 178 textOffset - getSpanAfterGutter() - gutterWidth; 179 c.putClientProperty(GUTTER_OFFSET_KEY, 180 Integer.valueOf(gutterOffset)); 181 Insets insets = c.getInsets(); 182 skin.paintSkin(g, gutterOffset, insets.top, 183 gutterWidth, c.getHeight() - insets.bottom - insets.top, 184 State.NORMAL); 185 } else { 186 if (c.getClientProperty(GUTTER_OFFSET_KEY) != null) { 187 c.putClientProperty(GUTTER_OFFSET_KEY, null); 188 } 189 } 190 } else { 191 super.paint(g, c); 192 } 193 } 194 } 195 | Popular Tags |