001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey Burlak
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Dimension;
023: import java.awt.Font;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026:
027: import javax.swing.AbstractButton;
028: import javax.swing.JComponent;
029: import javax.swing.JToolTip;
030: import javax.swing.UIManager;
031: import javax.swing.plaf.ComponentUI;
032: import javax.swing.plaf.basic.BasicToolTipUI;
033:
034: import org.apache.harmony.x.swing.Utilities;
035:
036: public class MetalToolTipUI extends BasicToolTipUI {
037: public static final int padSpaceBetweenStrings = 12;
038:
039: private JToolTip tooltip;
040: private boolean hideAccelerator;
041:
042: public static ComponentUI createUI(final JComponent c) {
043: return new MetalToolTipUI();
044: }
045:
046: public void installUI(final JComponent c) {
047: super .installUI(c);
048: tooltip = (JToolTip) c;
049: hideAccelerator = UIManager
050: .getBoolean("ToolTip.hideAccelerator");
051: }
052:
053: public void uninstallUI(final JComponent c) {
054: super .uninstallUI(c);
055: hideAccelerator = false;
056: }
057:
058: public void paint(final Graphics g, final JComponent c) {
059: super .paint(g, c);
060: if (hideAccelerator) {
061: return;
062: }
063: JComponent component = tooltip.getComponent();
064:
065: String acceleratorText = getAcceleratorString();
066: Font f = MetalLookAndFeel.getSubTextFont();
067: FontMetrics fm = tooltip.getFontMetrics(f);
068: Dimension stringSize = Utilities.getStringSize(acceleratorText,
069: fm);
070:
071: int textX = tooltip.getWidth() - stringSize.width
072: - padSpaceBetweenStrings / 2;
073: int textY = fm.getAscent();
074: Utilities.drawString(g, acceleratorText, textX, textY, fm,
075: MetalLookAndFeel.getAcceleratorForeground(), -1);
076: }
077:
078: public Dimension getPreferredSize(final JComponent c) {
079: Dimension result = super .getPreferredSize(c);
080:
081: if (!hideAccelerator
082: && tooltip.getComponent() instanceof AbstractButton) {
083: result.width += Utilities.getStringSize(
084: getAcceleratorString(), c.getFontMetrics(c
085: .getFont())).width;
086: result.width += padSpaceBetweenStrings;
087: }
088:
089: return result;
090: }
091:
092: protected boolean isAcceleratorHidden() {
093: return hideAccelerator;
094: }
095:
096: public String getAcceleratorString() {
097: if (!(tooltip.getComponent() instanceof AbstractButton)) {
098: return "";
099: }
100: AbstractButton comp = (AbstractButton) tooltip.getComponent();
101:
102: if (comp.getMnemonic() == 0) {
103: return "";
104: }
105:
106: return "Alt-" + Utilities.keyCodeToKeyChar(comp.getMnemonic());
107: }
108: }
|