001    package de.mospace.swing.icon;
002    
003    import java.awt.Color;
004    import java.awt.Component;
005    import java.awt.Graphics;
006    import javax.swing.Icon;
007    import javax.swing.UIManager;
008    
009    /** A java icon with a triangular shadowed arrow pointing up or down.
010    * Original package: jp.gr.java_conf.tame.swing.icon
011    * This class is in the public domain.
012    *
013    * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
014    */
015    public class BevelArrowIcon implements Icon {
016        /** Constant for arrows pointing up. */
017        public static final int UP    = 0;
018    
019        /** Constant for arrows pointing down. */
020        public static final int DOWN  = 1;
021    
022        /** The default size of this icon. */
023        private static final int DEFAULT_SIZE = 11;
024    
025        private Color edge1;
026        private Color edge2;
027        private Color fill;
028        private int size;
029        private int direction;
030    
031        /**
032        * Constructs a default size bevel arrow icon with the specified style
033        * and an arrow that points in the specified direction.
034        *
035        * @param direction the direction of the arrow. One of DIRECTION_UP,
036        * DIRECTION_DOWN
037        * @param isRaisedView whether the icon is raised
038        * @param isPressedView whether the icon is pressed
039        */
040        public BevelArrowIcon(int direction, boolean isRaisedView, boolean isPressedView) {
041            if (isRaisedView) {
042                if (isPressedView) {
043                    init( UIManager.getColor("controlLtHighlight"),
044                    UIManager.getColor("controlDkShadow"),
045                    UIManager.getColor("controlShadow"),
046                    DEFAULT_SIZE, direction);
047                } else {
048                    init( UIManager.getColor("controlHighlight"),
049                    UIManager.getColor("controlShadow"),
050                    UIManager.getColor("control"),
051                    DEFAULT_SIZE, direction);
052                }
053            } else {
054                if (isPressedView) {
055                    init( UIManager.getColor("controlDkShadow"),
056                    UIManager.getColor("controlLtHighlight"),
057                    UIManager.getColor("controlShadow"),
058                    DEFAULT_SIZE, direction);
059                } else {
060                    init( UIManager.getColor("controlShadow"),
061                    UIManager.getColor("controlHighlight"),
062                    UIManager.getColor("control"),
063                    DEFAULT_SIZE, direction);
064                }
065            }
066        }
067    
068        /**
069        * Constructs a new bevel arrow icon with the specified colors, size
070        * and orientation.
071        *
072        * @param direction the direction of the arrow. One of DIRECTION_UP,
073        * DIRECTION_DOWN
074        * @param edge1 the hihglight color for the icon edge
075        * @param edge2 the shadow color for the icon edge
076        * @param fill the background color for the icon
077        * @param size the size in pixels for the icon
078        */
079        public BevelArrowIcon(Color edge1, Color edge2, Color fill,
080        int size, int direction) {
081            init(edge1, edge2, fill, size, direction);
082        }
083    
084        /** @param d one of UP or DOWN **/
085        public void setDirection(int d){
086            if(d!=UP && d!= DOWN){
087                throw new IllegalArgumentException();
088            }
089            direction = d;
090        }
091    
092        /** Paints this icon on the specified graphics object at the
093        * specified location.
094        * @param c ignored, may be <code>null</code>
095        * @param g the graphics object to paint on
096        * @param x the x-offset for this icon
097        * @param y the y-offset for this icon
098        */
099        public void paintIcon(Component c, Graphics g, int x, int y) {
100            switch (direction) {
101                case DOWN: drawDownArrow(g, x, y); break;
102                default: drawUpArrow(g, x, y);   break;
103            }
104        }
105    
106        /**
107        * Gets the width of this bevel arrow icon.
108        *
109        * @return the width of this icon
110        */
111        public int getIconWidth() {
112            return size;
113        }
114    
115        /**
116        * Gets the height of this bevel arrow icon.
117        *
118        * @return the height of this icon
119        */
120        public int getIconHeight() {
121            return size;
122        }
123    
124    
125        private void init(Color edge1, Color edge2, Color fill,
126        int size, int direction) {
127            this.edge1 = edge1;
128            this.edge2 = edge2;
129            this.fill = fill;
130            this.size = size;
131            this.direction = direction;
132        }
133    
134        private void drawDownArrow(Graphics g, int xo, int yo) {
135            g.setColor(edge1);
136            g.drawLine(xo, yo,   xo+size-1, yo);
137            g.drawLine(xo, yo+1, xo+size-3, yo+1);
138            g.setColor(edge2);
139            g.drawLine(xo+size-2, yo+1, xo+size-1, yo+1);
140            int x = xo+1;
141            int y = yo+2;
142            int dx = size-6;
143            while (y+1 < yo+size) {
144                g.setColor(edge1);
145                g.drawLine(x, y,   x+1, y);
146                g.drawLine(x, y+1, x+1, y+1);
147                if (0 < dx) {
148                    g.setColor(fill);
149                    g.drawLine(x+2, y,   x+1+dx, y);
150                    g.drawLine(x+2, y+1, x+1+dx, y+1);
151                }
152                g.setColor(edge2);
153                g.drawLine(x+dx+2, y,   x+dx+3, y);
154                g.drawLine(x+dx+2, y+1, x+dx+3, y+1);
155                x += 1;
156                y += 2;
157                dx -= 2;
158            }
159            g.setColor(edge1);
160            g.drawLine(xo+(size/2), yo+size-1, xo+(size/2), yo+size-1);
161        }
162    
163        private void drawUpArrow(Graphics g, int xo, int yo) {
164            g.setColor(edge1);
165            int x = xo+(size/2);
166            g.drawLine(x, yo, x, yo);
167            x--;
168            int y = yo+1;
169            int dx = 0;
170            while (y+3 < yo+size) {
171                g.setColor(edge1);
172                g.drawLine(x, y,   x+1, y);
173                g.drawLine(x, y+1, x+1, y+1);
174                if (0 < dx) {
175                    g.setColor(fill);
176                    g.drawLine(x+2, y,   x+1+dx, y);
177                    g.drawLine(x+2, y+1, x+1+dx, y+1);
178                }
179                g.setColor(edge2);
180                g.drawLine(x+dx+2, y,   x+dx+3, y);
181                g.drawLine(x+dx+2, y+1, x+dx+3, y+1);
182                x -= 1;
183                y += 2;
184                dx += 2;
185            }
186            g.setColor(edge1);
187            g.drawLine(xo, yo+size-3,   xo+1, yo+size-3);
188            g.setColor(edge2);
189            g.drawLine(xo+2, yo+size-2, xo+size-1, yo+size-2);
190            g.drawLine(xo, yo+size-1, xo+size, yo+size-1);
191        }
192    }