    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ---------------
    028     * ArrowPanel.java
    029     * ---------------
    030     * (C) Copyright 2002-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: ArrowPanel.java,v 1.4 2005/10/18 13:18:34 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 25-Sep-2002 : Version 1 (DG);
    040     * 13-Oct-2002 : Added Javadocs (DG);
    041     *
    042     */
    043
    044    package org.jfree.ui;
    045
    046    import java.awt.Dimension;
    047    import java.awt.Graphics;
    048    import java.awt.Graphics2D;
    049    import java.awt.Insets;
    050    import java.awt.Polygon;
    051    import java.awt.Shape;
    052    import java.awt.geom.Rectangle2D;
    053
    054    import javax.swing.JPanel;
    055
    056    /**
    057     * A basic panel that displays a small up or down arrow.
    058     *
    059     * @author David Gilbert
    060     */
    061    public class ArrowPanel extends JPanel {
    062
    063        /** A constant for the up arrow. */
    064        public static final int UP = 0;
    065
    066        /** A constant for the down arrow. */
    067        public static final int DOWN = 1;
    068
    069        /** The arrow type. */
    070        private int type = UP;
    071
    072        /** The available area. */
    073        private Rectangle2D available = new Rectangle2D.Float();
    074
    075        /**
    076         * Creates a new arrow panel.
    077         *
    078         * @param type  the arrow type.
    079         */
    080        public ArrowPanel(final int type) {
    081            this.type = type;
    082            setPreferredSize(new Dimension(14, 9));
    083        }
    084
    085        /**
    086         * Paints the arrow panel.
    087         *
    088         * @param g  the graphics device for drawing on.
    089         */
    090        public void paintComponent(final Graphics g) {
    091
    092            super.paintComponent(g);
    093            final Graphics2D g2 = (Graphics2D) g;
    094
    095            // first determine the size of the drawing area...
    096            final Dimension size = getSize();
    097            final Insets insets = getInsets();
    098            this.available.setRect(insets.left, insets.top,
    099                                   size.getWidth() - insets.left - insets.right,
    100                                   size.getHeight() - insets.top - insets.bottom);
    101            g2.translate(insets.left, insets.top);
    102            g2.fill(getArrow(this.type));
    103
    104        }
    105
    106        /**
    107         * Returns a shape for the arrow.
    108         *
    109         * @param t  the arrow type.
    110         *
    111         * @return the arrow shape.
    112         */
    113        private Shape getArrow(final int t) {
    114            switch (t) {
    115                case UP : return getUpArrow();
    116                case DOWN : return getDownArrow();
    117                default : return getUpArrow();
    118            }
    119        }
    120
    121        /**
    122         * Returns an up arrow.
    123         *
    124         * @return an up arrow.
    125         */
    126        private Shape getUpArrow() {
    127            final Polygon result = new Polygon();
    128            result.addPoint(7, 2);
    129            result.addPoint(2, 7);
    130            result.addPoint(12, 7);
    131            return result;
    132        }
    133
    134        /**
    135         * Returns a down arrow.
    136         *
    137         * @return a down arrow.
    138         */
    139        private Shape getDownArrow() {
    140            final Polygon result = new Polygon();
    141            result.addPoint(7, 7);
    142            result.addPoint(2, 2);
    143            result.addPoint(12, 2);
    144            return result;
    145        }
    146
    147    }