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.basic;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.Graphics;
027:
028: import javax.swing.AbstractButton;
029: import javax.swing.JComponent;
030: import javax.swing.JToolTip;
031: import javax.swing.LookAndFeel;
032: import javax.swing.UIManager;
033: import javax.swing.plaf.ComponentUI;
034: import javax.swing.plaf.ToolTipUI;
035:
036: import org.apache.harmony.x.swing.Utilities;
037:
038: public class BasicToolTipUI extends ToolTipUI {
039: private static BasicToolTipUI toolTipUI;
040:
041: private JToolTip tooltip;
042: private Color foregroundInactiveColor;
043: private Color backgroundInactiveColor;
044:
045: public static ComponentUI createUI(final JComponent c) {
046: if (toolTipUI == null) {
047: toolTipUI = new BasicToolTipUI();
048: }
049:
050: return toolTipUI;
051: }
052:
053: public void installUI(final JComponent c) {
054: tooltip = (JToolTip) c;
055:
056: installDefaults(tooltip);
057: installListeners(tooltip);
058: }
059:
060: public void uninstallUI(final JComponent c) {
061: installDefaults(tooltip);
062: installListeners(tooltip);
063:
064: tooltip = null;
065: }
066:
067: protected void installDefaults(final JComponent c) {
068: LookAndFeel.installColorsAndFont(c, "ToolTip.background",
069: "ToolTip.foreground", "ToolTip.font");
070: LookAndFeel.installBorder(c, "ToolTip.border");
071:
072: backgroundInactiveColor = UIManager
073: .getColor("ToolTip.backgroundInactive");
074: foregroundInactiveColor = UIManager
075: .getColor("ToolTip.foregroundInactive");
076: }
077:
078: protected void uninstallDefaults(final JComponent c) {
079: Utilities.uninstallColorsAndFont(c);
080: LookAndFeel.uninstallBorder(c);
081:
082: backgroundInactiveColor = null;
083: foregroundInactiveColor = null;
084: }
085:
086: protected void installListeners(final JComponent c) {
087: }
088:
089: protected void uninstallListeners(final JComponent c) {
090: }
091:
092: public void paint(final Graphics g, final JComponent c) {
093: JComponent component = tooltip.getComponent();
094: if (component != null && component.isEnabled()
095: || backgroundInactiveColor == null) {
096: g.setColor(tooltip.getBackground());
097: LookAndFeel.installBorder(c, "ToolTip.border");
098: } else {
099: g.setColor(backgroundInactiveColor);
100: LookAndFeel.installBorder(c, "ToolTip.borderInactive");
101: }
102: g.fillRect(0, 0, tooltip.getWidth(), tooltip.getHeight());
103:
104: String tipText = tooltip.getTipText();
105: FontMetrics fm = Utilities.getFontMetrics(tooltip);
106: Dimension stringSize = Utilities.getStringSize(tipText, fm);
107: int textX = component instanceof AbstractButton
108: && ((AbstractButton) component).getMnemonic() != 0 ? 4
109: : (tooltip.getWidth() - stringSize.width) / 2;
110: int textY = fm.getAscent();
111: Color foreground = component != null && component.isEnabled()
112: || foregroundInactiveColor == null ? tooltip
113: .getForeground() : foregroundInactiveColor;
114: Utilities.drawString(g, tipText, textX, textY, fm, foreground,
115: -1);
116: }
117:
118: public Dimension getPreferredSize(final JComponent c) {
119: if (tooltip == null) {
120: tooltip = (JToolTip) c;
121: }
122: Font f = c.getFont();
123: if (f == null) {
124: return new Dimension();
125: }
126: Dimension result = Utilities.getStringSize(
127: tooltip.getTipText(), c.getFontMetrics(f));
128: result.width += 6;
129:
130: return Utilities.addInsets(result, tooltip.getInsets());
131: }
132:
133: public Dimension getMinimumSize(final JComponent c) {
134: return getPreferredSize(c);
135: }
136:
137: public Dimension getMaximumSize(final JComponent c) {
138: return getPreferredSize(c);
139: }
140: }
|