001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.ui;
030
031import java.awt.BorderLayout;
032import java.awt.Stroke;
033import java.awt.event.ActionEvent;
034import java.awt.event.ActionListener;
035import javax.swing.DefaultComboBoxModel;
036import javax.swing.JComboBox;
037import javax.swing.JPanel;
038
039/**
040 * A component for choosing a stroke from a list of available strokes.  This
041 * class needs work.
042 */
043public class StrokeChooserPanel extends JPanel {
044
045    /** A combo for selecting the stroke. */
046    private JComboBox selector;
047
048    /**
049     * Creates a panel containing a combo-box that allows the user to select
050     * one stroke from a list of available strokes.
051     *
052     * @param current  the current stroke sample.
053     * @param available  an array of 'available' stroke samples.
054     */
055    public StrokeChooserPanel(StrokeSample current, StrokeSample[] available) {
056        setLayout(new BorderLayout());
057        // we've changed the behaviour here to populate the combo box
058        // with Stroke objects directly - ideally we'd change the signature
059        // of the constructor too...maybe later.
060        DefaultComboBoxModel model = new DefaultComboBoxModel();
061        for (int i = 0; i < available.length; i++) {
062            model.addElement(available[i].getStroke());
063        }
064        this.selector = new JComboBox(model);
065        this.selector.setSelectedItem(current.getStroke());
066        this.selector.setRenderer(new StrokeSample(null));
067        add(this.selector);
068        // Changes due to focus problems!! DZ
069        this.selector.addActionListener(new ActionListener() {
070            @Override
071            public void actionPerformed(ActionEvent evt) {
072                getSelector().transferFocus();
073            }
074        });
075    }
076
077
078    /**
079     * Returns the selector component.
080     *
081     * @return Returns the selector.
082     */
083    protected final JComboBox getSelector() {
084        return this.selector;
085    }
086
087    /**
088     * Returns the selected stroke.
089     *
090     * @return The selected stroke (possibly {@code null}).
091     */
092    public Stroke getSelectedStroke() {
093        return (Stroke) this.selector.getSelectedItem();
094    }
095
096}
097