001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2009, 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     * StrokeChooserPanel.java
029     * -----------------------
030     * (C) Copyright 2000-2009, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   Dirk Zeitz;
034     *
035     * $Id: StrokeChooserPanel.java,v 1.8 2009/02/27 13:58:41 mungady Exp $
036     *
037     * Changes (from 26-Oct-2001)
038     * --------------------------
039     * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
040     * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041     * 16-Mar-2004 : Fix for focus problems (DZ);
042     * 27-Feb-2009 : Fixed bug 2612649, NullPointerException (DG);
043     *
044     */
045    
046    package org.jfree.ui;
047    
048    import java.awt.BorderLayout;
049    import java.awt.Stroke;
050    import java.awt.event.ActionEvent;
051    import java.awt.event.ActionListener;
052    
053    import javax.swing.DefaultComboBoxModel;
054    import javax.swing.JComboBox;
055    import javax.swing.JPanel;
056    
057    /**
058     * A component for choosing a stroke from a list of available strokes.  This
059     * class needs work.
060     *
061     * @author David Gilbert
062     */
063    public class StrokeChooserPanel extends JPanel {
064    
065        /** A combo for selecting the stroke. */
066        private JComboBox selector;
067    
068        /**
069         * Creates a panel containing a combo-box that allows the user to select
070         * one stroke from a list of available strokes.
071         *
072         * @param current  the current stroke sample.
073         * @param available  an array of 'available' stroke samples.
074         */
075        public StrokeChooserPanel(StrokeSample current, StrokeSample[] available) {
076            setLayout(new BorderLayout());
077            // we've changed the behaviour here to populate the combo box
078            // with Stroke objects directly - ideally we'd change the signature
079            // of the constructor too...maybe later.
080            DefaultComboBoxModel model = new DefaultComboBoxModel();
081            for (int i = 0; i < available.length; i++) {
082                model.addElement(available[i].getStroke());
083            }
084            this.selector = new JComboBox(model);
085            this.selector.setSelectedItem(current.getStroke());
086            this.selector.setRenderer(new StrokeSample(null));
087            add(this.selector);
088            // Changes due to focus problems!! DZ
089            this.selector.addActionListener(new ActionListener() {
090                public void actionPerformed(final ActionEvent evt) {
091                    getSelector().transferFocus();
092                }
093            });
094        }
095    
096    
097        /**
098         * Returns the selector component.
099         *
100         * @return Returns the selector.
101         */
102        protected final JComboBox getSelector() {
103            return this.selector;
104        }
105    
106        /**
107         * Returns the selected stroke.
108         *
109         * @return The selected stroke (possibly <code>null</code>).
110         */
111        public Stroke getSelectedStroke() {
112            return (Stroke) this.selector.getSelectedItem();
113        }
114    
115    }