001/*
002 * $Id: TextCrossingPainter.java 3100 2008-10-14 22:33:10Z rah003 $
003 *
004 * Copyright 2008 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.plaf.basic;
022
023import java.awt.Color;
024import java.awt.Graphics2D;
025import java.awt.Insets;
026import java.awt.Rectangle;
027
028import javax.swing.JComponent;
029import javax.swing.JLabel;
030import javax.swing.SwingUtilities;
031
032import org.jdesktop.swingx.painter.AbstractPainter;
033
034/**
035 * Painter used to cross-out unselectable dates.
036 * 
037 * PENDING JW: subclass (or maybe even use?) one of the painter subclasses. 
038 * 
039 * @author Jeanette Winzenburg
040 */
041class TextCrossingPainter<T extends JComponent> extends AbstractPainter<T> {
042    Rectangle paintIconR = new Rectangle();
043    Rectangle paintViewR = new Rectangle();
044    Rectangle paintTextR = new Rectangle();
045    Insets insetss = new Insets(0, 0, 0, 0);
046    Color crossColor;
047    /**
048     * {@inheritDoc} <p>
049     * 
050     *  Paints a diagonal cross over the text if the comp is of type JLabel, 
051     *  does nothing otherwise.
052     */
053    @Override
054    protected void doPaint(Graphics2D g, JComponent comp, int width,
055            int height) {
056        if (!(comp instanceof JLabel)) return;
057        JLabel label = (JLabel) comp;
058        Insets insets = label.getInsets(insetss);
059        paintViewR.x = insets.left;
060        paintViewR.y = insets.top;
061        paintViewR.width = width - (insets.left + insets.right);
062        paintViewR.height = height - (insets.top + insets.bottom);
063        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
064        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
065        SwingUtilities.layoutCompoundLabel(label, 
066                label.getFontMetrics(label.getFont()), label.getText(), null,
067                label.getVerticalAlignment(), label.getHorizontalAlignment(), 
068                label.getVerticalTextPosition(), label.getHorizontalTextPosition(),
069                paintViewR, paintIconR, paintTextR, label.getIconTextGap());
070        doPaint(g, paintTextR);
071    }
072    
073    private void doPaint(Graphics2D g, Rectangle r) {
074        Color old = g.getColor();
075        g.setColor(getForeground());
076        g.drawLine(r.x, r.y, r.x + r.width, r.y + r.height);
077        g.drawLine(r.x + 1, r.y, r.x + r.width + 1, r.y + r.height);
078        g.drawLine(r.x + r.width, r.y, r.x, r.y + r.height);
079        g.drawLine(r.x + r.width - 1, r.y, r.x - 1, r.y + r.height);
080        g.setColor(old);
081        
082    }
083
084    /**
085     * 
086     * @param crossColor the color to paint the cross with
087     */
088    public void setForeground(Color crossColor) {
089        Color old = getForeground();
090        this.crossColor = crossColor;
091        firePropertyChange("foreground", old, getForeground());
092    }
093
094    /**
095     * Returns the color to use for painting the cross.
096     * 
097     * @return the color used for painting.
098     */
099    public Color getForeground() {
100        return crossColor;
101    }
102}