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