001    /* ===========================================================
002     * JFreeChart : a free chart 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/jfreechart/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     * ExtendedCategoryAxis.java
029     * -------------------------
030     * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: ExtendedCategoryAxis.java,v 1.4.2.1 2005/10/25 20:37:34 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 07-Nov-2003 : Version 1 (DG);
040     * 07-Jan-2004 : Updated the createLabel() method (DG);
041     * 29-Jan-2004 : Added paint attribute (DG);
042     *
043     */
044    
045    package org.jfree.chart.axis;
046    
047    import java.awt.Color;
048    import java.awt.Font;
049    import java.awt.Graphics2D;
050    import java.awt.Paint;
051    import java.util.HashMap;
052    import java.util.Map;
053    
054    import org.jfree.text.TextBlock;
055    import org.jfree.text.TextFragment;
056    import org.jfree.text.TextLine;
057    import org.jfree.ui.RectangleEdge;
058    
059    /**
060     * An extended version of the {@link CategoryAxis} class that supports 
061     * sublabels on the axis.
062     */
063    public class ExtendedCategoryAxis extends CategoryAxis {
064    
065        /** Storage for the sublabels. */
066        private Map sublabels;
067        
068        /** The sublabel font. */
069        private Font sublabelFont;
070        
071        /** The sublabel paint. */
072        private Paint sublabelPaint;
073        
074        /**
075         * Creates a new axis.
076         * 
077         * @param label  the axis label.
078         */
079        public ExtendedCategoryAxis(String label) {
080            super(label);
081            this.sublabels = new HashMap();
082            this.sublabelFont = new Font("SansSerif", Font.PLAIN, 10);
083            this.sublabelPaint = Color.black;
084        }
085        
086        /**
087         * Returns the font for the sublabels.
088         * 
089         * @return The font.
090         */
091        public Font getSubLabelFont() {
092            return this.sublabelFont;
093        }
094        
095        /**
096         * Sets the font for the sublabels.
097         * 
098         * @param font  the font.
099         */
100        public void setSubLabelFont(Font font) {
101            this.sublabelFont = font;
102        }
103        
104        /**
105         * Returns the paint for the sublabels.
106         * 
107         * @return The paint.
108         */
109        public Paint getSubLabelPaint() {
110            return this.sublabelPaint;
111        }
112        
113        /**
114         * Sets the paint for the sublabels.
115         * 
116         * @param paint  the paint.
117         */
118        public void setSubLabelPaint(Paint paint) {
119            this.sublabelPaint = paint;
120        }
121        
122        /**
123         * Adds a sublabel for a category.
124         * 
125         * @param category  the category.
126         * @param label  the label.
127         */
128        public void addSubLabel(Comparable category, String label) {
129            this.sublabels.put(category, label);
130        }
131        
132        /**
133         * Overrides the default behaviour by adding the sublabel to the text 
134         * block that is used for the category label.
135         * 
136         * @param category  the category.
137         * @param width  the width (not used yet).
138         * @param edge  the location of the axis.
139         * @param g2  the graphics device.
140         * 
141         * @return A label.
142         */
143        protected TextBlock createLabel(Comparable category, float width, 
144                                        RectangleEdge edge, Graphics2D g2) {
145            TextBlock label = super.createLabel(category, width, edge, g2);   
146            String s = (String) this.sublabels.get(category);
147            if (s != null) {
148                if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
149                    TextLine line = new TextLine(
150                        s, this.sublabelFont, this.sublabelPaint
151                    );
152                    label.addLine(line);
153                }
154                else if (edge == RectangleEdge.LEFT 
155                        || edge == RectangleEdge.RIGHT) {
156                    TextLine line = label.getLastLine();
157                    if (line != null) {
158                        line.addFragment(
159                            new TextFragment(
160                                "  " + s, this.sublabelFont, this.sublabelPaint
161                            )
162                        );
163                    }
164                }
165            }
166            return label; 
167        }
168        
169    }