1 7 8 package javax.swing.plaf.metal; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import java.awt.*; 12 import java.awt.event.*; 13 import javax.swing.*; 14 import javax.swing.BorderFactory ; 15 import javax.swing.border.Border ; 16 import javax.swing.plaf.*; 17 import javax.swing.plaf.basic.BasicToolTipUI ; 18 19 20 35 public class MetalToolTipUI extends BasicToolTipUI { 36 37 static MetalToolTipUI sharedInstance = new MetalToolTipUI (); 38 private Font smallFont; 39 private JToolTip tip; 41 public static final int padSpaceBetweenStrings = 12; 42 private String acceleratorDelimiter; 43 44 public MetalToolTipUI() { 45 super(); 46 } 47 48 public static ComponentUI createUI(JComponent c) { 49 return sharedInstance; 50 } 51 52 public void installUI(JComponent c) { 53 super.installUI(c); 54 tip = (JToolTip)c; 55 Font f = c.getFont(); 56 smallFont = new Font( f.getName(), f.getStyle(), f.getSize() - 2 ); 57 acceleratorDelimiter = UIManager.getString( "MenuItem.acceleratorDelimiter" ); 58 if ( acceleratorDelimiter == null ) { acceleratorDelimiter = "-"; } 59 } 60 61 public void uninstallUI(JComponent c) { 62 super.uninstallUI(c); 63 tip = null; 64 } 65 66 public void paint(Graphics g, JComponent c) { 67 JToolTip tip = (JToolTip)c; 68 69 super.paint(g, c); 70 71 Font font = c.getFont(); 72 FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font); 73 String keyText = getAcceleratorString(tip); 74 String tipText = tip.getTipText(); 75 if (tipText == null) { 76 tipText = ""; 77 } 78 if (! (keyText.equals(""))) { g.setFont(smallFont); 80 g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() ); 81 SwingUtilities2.drawString(tip, g, keyText, 82 SwingUtilities2.stringWidth( 83 tip, metrics, tipText) + padSpaceBetweenStrings, 84 2 + metrics.getAscent()); 85 } 86 } 87 88 public Dimension getPreferredSize(JComponent c) { 89 Dimension d = super.getPreferredSize(c); 90 91 String key = getAcceleratorString((JToolTip)c); 92 if (! (key.equals(""))) { 93 FontMetrics fm = c.getFontMetrics(smallFont); 94 d.width += SwingUtilities2.stringWidth(c, fm, key) + 95 padSpaceBetweenStrings; 96 } 97 return d; 98 } 99 100 protected boolean isAcceleratorHidden() { 101 Boolean b = (Boolean )UIManager.get("ToolTip.hideAccelerator"); 102 return b != null && b.booleanValue(); 103 } 104 105 private String getAcceleratorString(JToolTip tip) { 106 this.tip = tip; 107 108 String retValue = getAcceleratorString(); 109 110 this.tip = null; 111 return retValue; 112 } 113 114 public String getAcceleratorString() { 121 if (tip == null || isAcceleratorHidden()) { 122 return ""; 123 } 124 JComponent comp = tip.getComponent(); 125 if (comp == null) { 126 return ""; 127 } 128 129 KeyStroke[] keys; 130 131 if (comp instanceof JTabbedPane) { 132 TabbedPaneUI ui = ( (JTabbedPane)(comp) ).getUI(); 133 if (ui instanceof MetalTabbedPaneUI ) { 134 int rolloverTabIndex = ( (MetalTabbedPaneUI )ui ).getRolloverTabIndex(); 137 if (rolloverTabIndex == -1) 138 keys = new KeyStroke[0]; 139 else { 140 int mnemonic = ((JTabbedPane)comp).getMnemonicAt(rolloverTabIndex); 142 if (mnemonic == -1) 143 keys = new KeyStroke[0]; 144 else { 145 KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic, Event.ALT_MASK); 147 keys = new KeyStroke[1]; 148 keys[0] = keyStroke; 149 } 150 } 151 } 152 else 153 keys = comp.getRegisteredKeyStrokes(); 154 } 155 else 156 keys = comp.getRegisteredKeyStrokes(); 157 158 String controlKeyStr = ""; 159 160 for (int i = 0; i < keys.length; i++) { 161 int mod = keys[i].getModifiers(); 162 int condition = comp.getConditionForKeyStroke(keys[i]); 163 164 if ( condition == JComponent.WHEN_IN_FOCUSED_WINDOW ) 165 { 166 controlKeyStr = KeyEvent.getKeyModifiersText(mod) + 167 acceleratorDelimiter + 168 KeyEvent.getKeyText(keys[i].getKeyCode()); 169 break; 170 } 171 } 172 173 175 if ( controlKeyStr.equals("") && comp instanceof JMenuItem ) 176 { 177 int mnemonic = ((JMenuItem) comp).getMnemonic(); 178 if ( mnemonic != 0 ) 179 { 180 controlKeyStr = 181 KeyEvent.getKeyModifiersText(KeyEvent.ALT_MASK) + 182 acceleratorDelimiter + (char)mnemonic; 183 } 184 } 185 186 return controlKeyStr; 187 } 188 189 } 190 | Popular Tags |