001/*
002 * $Id$
003 *
004 * Copyright 2009 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 *
021 */
022package org.jdesktop.swingx.decorator;
023
024import java.awt.Component;
025
026import javax.swing.AbstractButton;
027import javax.swing.JLabel;
028import javax.swing.JTextField;
029import javax.swing.SwingConstants;
030
031/**
032 * A Highlighter which sets the horizontal alignment.
033 * 
034 * @author Jeanette Winzenburg (slight cleanup)
035 * @author original contributed by swingx member martinm1000
036 */
037public class AlignmentHighlighter extends AbstractHighlighter {
038    private static final int defaultAlignment = SwingConstants.LEADING; 
039    private int alignment;
040
041    
042    /**
043     * Instantiates a AlignmentHighlighter with default alignment LEADING. The Highlighter is 
044     * applied always.
045     */
046    public AlignmentHighlighter() {
047        this(defaultAlignment);
048    }
049    
050    
051    /**
052     * Instantiates a AlignmentHighlighter with the specified alignment. The Highlighter is 
053     * applied always.
054     * 
055     * @param alignment the horizontal alignment to use.
056     * @throws IllegalArgumentException if not one of the constants allowed as horizontal alignment,
057     * that is one of LEADING, LEFT, CENTER, RIGHT, TRAILING
058     */
059    public AlignmentHighlighter(int alignment) {
060        this(null, alignment);
061    }
062
063
064    /**
065     * Instantiates a FontHighlighter with the given HighlightPredicate and default 
066     * horizontal alignement.
067     * 
068     * @param predicate the HighlightPredicate to use, may be null to default to ALWAYS.
069     */
070    public AlignmentHighlighter(HighlightPredicate predicate) {
071        this(predicate, defaultAlignment);
072    }
073    
074
075    /**
076     * Instantiates a FontHighlighter with the given HighlightPredicate and null Font.
077     * 
078     * @param predicate the HighlightPredicate to use, may be null to default to ALWAYS.
079     * @param alignment the horizontal alignment to use.
080     * @throws IllegalArgumentException if not one of the constants allowed as horizontal alignment,
081     * that is one of LEADING, LEFT, CENTER, RIGHT, TRAILING
082     */
083    public AlignmentHighlighter(HighlightPredicate predicate, int alignment) {
084        super(predicate);
085        this.alignment = checkHorizontalAlignment(alignment);
086    }
087
088    /**
089     * Returns the alignment which is applied.
090     * @return the alignment
091     */
092    public int getHorizontalAlignment() {
093        return alignment;
094    }
095
096    
097    /**
098     * Sets the horizontal alignment to apply.
099     * 
100     * @param alignment the horizontal alignment to set
101     * @throws IllegalArgumentException if not one of the constants allowed as horizontal alignment,
102     * that is one of LEADING, LEFT, CENTER, RIGHT, TRAILING
103     */
104    public void setHorizontalAlignment(int alignment) {
105        if (getHorizontalAlignment() == alignment) return;
106        this.alignment = checkHorizontalAlignment(alignment);
107        fireStateChanged();
108    }
109
110
111    /**
112     * Checks if the horizontal alignment is valid.
113     * 
114     * @param alignment the horizontal alignment to check
115     * @throws IllegalArgumentException if not one of the constants allowed as horizontal alignment,
116     * that is one of LEADING, LEFT, CENTER, RIGHT, TRAILING
117     */
118    private int checkHorizontalAlignment(int alignment) {
119        if ((alignment == SwingConstants.LEFT) ||
120                (alignment == SwingConstants.CENTER) ||
121                (alignment == SwingConstants.RIGHT) ||
122                (alignment == SwingConstants.LEADING) ||
123                (alignment == SwingConstants.TRAILING)) {
124                return alignment;
125            }
126            else {
127                throw new IllegalArgumentException("invalid horizontal alignment, expected one of "
128                      + SwingConstants.LEFT + " / " + SwingConstants.CENTER + 
129                      " / " + SwingConstants.RIGHT + " / " + SwingConstants.LEADING + 
130                      " / " + SwingConstants.TRAILING + " but was: " + alignment);
131            }
132    }
133
134    /**
135     * {@inheritDoc} <p>
136     * 
137     * Implemented to set the horizontal alignement of the rendering component.
138     */
139    @Override
140    protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
141        if (renderer instanceof JLabel) {
142            ((JLabel) renderer).setHorizontalAlignment(getHorizontalAlignment());
143        } else if (renderer instanceof AbstractButton ) {
144            ((AbstractButton) renderer).setHorizontalAlignment(getHorizontalAlignment());
145        } else {
146            ((JTextField) renderer).setHorizontalAlignment(getHorizontalAlignment());
147        }
148        return renderer;
149    }
150
151    /**
152     * {@inheritDoc} <p>
153     * 
154     * Implemented to return true for components of type JLabel, AbstractButton or JTextField, 
155     * false otherwise.
156     */
157    @Override
158    protected boolean canHighlight(Component component, ComponentAdapter adapter) {
159        return (component instanceof JLabel) 
160            || (component instanceof AbstractButton)
161            || (component instanceof JTextField)
162            ;
163    }
164}