001    /*
002     * $Id: SortArrowIcon.java,v 1.3 2005/10/10 18:01:39 rbair 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.icon;
023    
024    import java.awt.Color;
025    import java.awt.Component;
026    import java.awt.Graphics;
027    
028    import javax.swing.Icon;
029    import javax.swing.plaf.UIResource;
030    
031    public class SortArrowIcon implements Icon, UIResource {
032        private boolean ascending = true;
033        //REMIND(aim): support more configurable sizes
034        private int width = 8;
035        private int height = 8;
036    
037        public SortArrowIcon(boolean ascending) {
038            this.ascending = ascending;
039        }
040    
041        public int getIconWidth() {
042            return width;
043        }
044    
045        public int getIconHeight() {
046            return height;
047        }
048    
049        public void paintIcon(Component c, Graphics g, int x, int y) {
050            Color base = c.getBackground();
051            Color shadow = base.darker().darker();
052            Color highlight = Color.white;
053    
054            if (ascending) {
055                g.setColor(shadow);
056                int y1 = height-1;
057                for(int x1=0; x1 < width/2 ; x1++) {
058                    g.drawLine(x + x1, y + y1, x + x1, y + y1 - 1);
059                    y1 -= ((x1+1 == (width/2)-1)? 1 : 2);
060                }
061                g.setColor(highlight);
062                y1 = height-1;
063                for (int x1 = width-1; x1 >= width / 2; x1--) {
064                    g.drawLine(x + x1, y + y1, x + x1, y + y1 - 1);
065                    y1 -= ( (x1 - 1 == (width / 2)) ? 1 : 2);
066                }
067                g.drawLine(x + 1, y + height-1, x + width - 1, y + height-1);
068            } else {
069                // descending
070                g.setColor(shadow);
071                int y1 = 1;
072                for (int x1 = 0; x1 < width/2 ; x1++) {
073                    g.drawLine(x + x1, y + y1, x + x1, y + y1 + 1);
074                    y1 += (x1+1 == (width/2-1))? 1 : 2;
075                }
076                g.setColor(highlight);
077                y1 = 1;
078                for (int x1 = width - 1; x1 >= width/2; x1--) {
079                    g.drawLine(x + x1, y + y1, x + x1, y + y1 + 1);
080                    y1 += (x1-1 == width/2)? 1 : 2;
081                }
082                g.setColor(shadow);
083                g.drawLine(x + 1, y + 1, x + width - 1, y + 1);
084            }
085        }
086    }