001/*
002 * $Id: ToolTipHighlighter.java 3676 2010-04-26 15:42:26Z kschaefe $
003 *
004 * Copyright 2010 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.decorator;
022
023import java.awt.Component;
024import java.awt.Font;
025
026import javax.swing.JComponent;
027
028import org.jdesktop.swingx.renderer.StringValue;
029
030/**
031 * A highlighter for setting a tool tip on the component.
032 * 
033 * @author kschaefer
034 */
035public class ToolTipHighlighter extends AbstractHighlighter {
036    private StringValue toolTipValue;
037    
038    /**
039     * Instantiates a ToolTipHighlighter with null StringValue. The Highlighter is 
040     * applied always.
041     */
042    public ToolTipHighlighter() {
043        this((HighlightPredicate) null);
044    }
045
046    /**
047     * Instantiates a ToolTipHighlighter with the specified StringValue. The Highlighter is applied
048     * always.
049     * 
050     * @param toolTipValue
051     *            the StringValue used to create the tool tip
052     */
053    public ToolTipHighlighter(StringValue toolTipValue) {
054        this(null, toolTipValue);
055    }
056
057    /**
058     * Instantiates a ToolTipHighlighter with the specified HighlightPredicate and a null
059     * StringValue.
060     * 
061     * @param predicate
062     *            the HighlightPredicate to use, may be null to default to ALWAYS.
063     */
064    public ToolTipHighlighter(HighlightPredicate predicate) {
065        this(predicate, null);
066    }
067    
068    /**
069     * Instantiates a ToolTipHighlighter with the specified HighlightPredicate and StringValue.
070     * 
071     * @param predicate
072     *            the HighlightPredicate to use, may be null to default to ALWAYS.
073     * @param toolTipValue
074     *            the StringValue used to create the tool tip
075     */
076    public ToolTipHighlighter(HighlightPredicate predicate, StringValue toolTipValue) {
077        super(predicate);
078        
079        this.toolTipValue = toolTipValue;
080    }
081
082    /**
083     * Returns the StringValue used for decoration.
084     * 
085     * @return the StringValue used for decoration
086     * 
087     * @see #setToolTipValue(Font)
088     */
089    public StringValue getToolTipValue() {
090        return toolTipValue;
091    }
092    
093    /**
094     * Sets the StringValue used for decoration. May be null to use default decoration.
095     * 
096     * @param font the Font used for decoration, may be null to use default decoration.
097     * 
098     * @see #getToolTipValue()
099     */
100    public void setToolTipValue(StringValue toolTipValue) {
101        if (areEqual(toolTipValue, getToolTipValue())) return;
102        this.toolTipValue = toolTipValue;
103        fireStateChanged();
104    }
105
106    /**
107     * {@inheritDoc}<p>
108     * 
109     * Implemented to return false if the component is not a JComponent.
110     */
111    @Override
112    protected boolean canHighlight(Component component, ComponentAdapter adapter) {
113        return component instanceof JComponent;
114    }
115
116    /**
117     * {@inheritDoc}
118     */
119    @Override
120    protected Component doHighlight(Component component, ComponentAdapter adapter) {
121        String toolTipText = null;
122        
123        if (toolTipValue == null) {
124            toolTipText = adapter.getString();
125        } else {
126            toolTipText = toolTipValue.getString(adapter.getValue());
127        }
128        
129        ((JComponent) component).setToolTipText(toolTipText);
130        
131        return component;
132    }
133}