001    /*
002     * $Id: IconBorder.java,v 1.5 2005/10/24 14:00:37 kleopatra Exp $
003     *
004     * Copyright 2004 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    
022    package org.jdesktop.swingx.border;
023    
024    import java.awt.Component;
025    import java.awt.ComponentOrientation;
026    import java.awt.Graphics;
027    import java.awt.Insets;
028    import java.awt.Rectangle;
029    
030    import javax.swing.Icon;
031    import javax.swing.SwingConstants;
032    import javax.swing.border.Border;
033    
034    /**
035     * @author Amy Fowler
036     * @version 1.0
037     */
038    public class IconBorder implements Border {
039    
040        private static final int PAD = 4;
041        private Icon icon;
042        private int iconPosition;
043        private Rectangle iconBounds = new Rectangle();
044        public IconBorder() {
045            this(null);
046        }
047    
048        public IconBorder(Icon validIcon) {
049            this(validIcon, SwingConstants.TRAILING);
050        }
051    
052        public IconBorder(Icon validIcon, int iconPosition) {
053            this.icon = validIcon;
054            this.iconPosition = iconPosition;
055        }
056    
057        public Insets getBorderInsets(Component c) {
058            int horizontalInset = icon.getIconWidth() + (2 * PAD);
059            int iconPosition = bidiDecodeLeadingTrailing(c.getComponentOrientation(), this.iconPosition);
060            if (iconPosition == SwingConstants.EAST) {
061                return new Insets(0, 0, 0, horizontalInset);
062            }
063            return new Insets(0, horizontalInset, 0, 0);
064        }
065    
066        public void setIcon(Icon validIcon) {
067            this.icon = validIcon;
068        }
069        
070        public boolean isBorderOpaque() {
071            return false;
072        }
073    
074        public void paintBorder(Component c, Graphics g, int x, int y, int width,
075            int height) {
076            int iconPosition = bidiDecodeLeadingTrailing(c.getComponentOrientation(), this.iconPosition);
077            if (iconPosition == SwingConstants.NORTH_EAST) {
078                iconBounds.y = y + PAD;
079                iconBounds.x = x + width - PAD - icon.getIconWidth();
080            } else if (iconPosition == SwingConstants.EAST) {    // EAST
081                iconBounds.y = y
082                    + ((height - icon.getIconHeight()) / 2);
083                iconBounds.x = x + width - PAD - icon.getIconWidth();
084            } else if (iconPosition == SwingConstants.WEST) {
085                iconBounds.y = y
086                    + ((height - icon.getIconHeight()) / 2);
087                iconBounds.x = x + PAD;
088            }
089            iconBounds.width = icon.getIconWidth();
090            iconBounds.height = icon.getIconHeight();
091            icon.paintIcon(c, g, iconBounds.x, iconBounds.y);
092        }
093    
094        /**
095         * Returns EAST or WEST depending on the ComponentOrientation and 
096         * the given postion LEADING/TRAILING this method has no effect for other
097         * position values
098         */
099        private int bidiDecodeLeadingTrailing(ComponentOrientation c, int position) {
100            if(position == SwingConstants.TRAILING) {
101                if(!c.isLeftToRight()) {
102                    return SwingConstants.WEST;
103                }
104                return SwingConstants.EAST;
105            }
106            if(position == SwingConstants.LEADING) {
107                if(!c.isLeftToRight()) {
108                    return SwingConstants.WEST;
109                }
110                return SwingConstants.EAST;
111            }
112            return position;
113        }
114    
115    }