    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.6 2007/11/02 17:50:36 taqua 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    import javax.swing.JPanel;
    054
    055    /**
    056     * A basic panel that displays a small up or down arrow.
    057     *
    058     * @author David Gilbert
    059     */
    060    public class ArrowPanel extends JPanel {
    061
    062        /** A constant for the up arrow. */
    063        public static final int UP = 0;
    064
    065        /** A constant for the down arrow. */
    066        public static final int DOWN = 1;
    067
    068        /** The arrow type. */
    069        private int type = UP;
    070
    071        /** The available area. */
    072        private Rectangle2D available = new Rectangle2D.Float();
    073
    074        /**
    075         * Creates a new arrow panel.
    076         *
    077         * @param type  the arrow type.
    078         */
    079        public ArrowPanel(final int type) {
    080            this.type = type;
    081            setPreferredSize(new Dimension(14, 9));
    082        }
    083
    084        /**
    085         * Paints the arrow panel.
    086         *
    087         * @param g  the graphics device for drawing on.
    088         */
    089        public void paintComponent(final Graphics g) {
    090
    091            super.paintComponent(g);
    092            final Graphics2D g2 = (Graphics2D) g;
    093
    094            // first determine the size of the drawing area...
    095            final Dimension size = getSize();
    096            final Insets insets = getInsets();
    097            this.available.setRect(insets.left, insets.top,
    098                                   size.getWidth() - insets.left - insets.right,
    099                                   size.getHeight() - insets.top - insets.bottom);
    100            g2.translate(insets.left, insets.top);
    101            g2.fill(getArrow(this.type));
    102
    103        }
    104
    105        /**
    106         * Returns a shape for the arrow.
    107         *
    108         * @param t  the arrow type.
    109         *
    110         * @return the arrow shape.
    111         */
    112        private Shape getArrow(final int t) {
    113            switch (t) {
    114                case UP : return getUpArrow();
    115                case DOWN : return getDownArrow();
    116                default : return getUpArrow();
    117            }
    118        }
    119
    120        /**
    121         * Returns an up arrow.
    122         *
    123         * @return an up arrow.
    124         */
    125        private Shape getUpArrow() {
    126            final Polygon result = new Polygon();
    127            result.addPoint(7, 2);
    128            result.addPoint(2, 7);
    129            result.addPoint(12, 7);
    130            return result;
    131        }
    132
    133        /**
    134         * Returns a down arrow.
    135         *
    136         * @return a down arrow.
    137         */
    138        private Shape getDownArrow() {
    139            final Polygon result = new Polygon();
    140            result.addPoint(7, 7);
    141            result.addPoint(2, 2);
    142            result.addPoint(12, 2);
    143            return result;
    144        }
    145
    146    }