001/**
002 * ChooseUnitPanel -- A JPanel that allows the user to select a length unit to be used.
003 *
004 * Copyright (C) 2009-2015, by Joseph A. Huwaldt. All rights reserved.
005 *
006 * This library is free software; you can redistribute it and/or modify it under the terms
007 * of the GNU Lesser General Public License as published by the Free Software Foundation;
008 * either version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
012 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License along with
015 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place -
016 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html
017 */
018package geomss.app;
019
020import java.util.Objects;
021import static java.util.Objects.requireNonNull;
022import javax.measure.quantity.Quantity;
023import javax.measure.unit.Unit;
024import javax.swing.*;
025
026/**
027 * A panel the allows the user to select a length unit to be used.
028 *
029 * <p> Modified by: Joseph A. Huwaldt </p>
030 *
031 * @author Joseph A. Huwaldt, Date: April 5, 2009
032 * @version November 23, 2015
033 *
034 * @param <Q> The Quantity (unit type) of the unit chosen.
035 */
036@SuppressWarnings("serial")
037public class ChooseUnitPanel<Q extends Quantity> extends JPanel {
038
039    //  The combo-boxe that contain the units.
040    private final JComboBox _unitCBox;
041
042    /**
043     * Construct a panel that allows the user to select a length of unit to be used.
044     *
045     * @param message     A message to display telling the user what to do. May not be null.
046     * @param typeStr     A string indicating the unit type (such as "Length Units: ", or
047     *                    "Time Units: "). May not be null.
048     * @param unitOptions The list of units being edited. May not be null.
049     * @param initial     The initially selected unit (or null for none).
050     */
051    public ChooseUnitPanel(String message, String typeStr, Unit<Q>[] unitOptions, Unit<Q> initial) {
052        requireNonNull(message);
053        requireNonNull(typeStr);
054        requireNonNull(unitOptions);
055        
056        //  Layout the panel.
057        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
058
059        //  Add instructions at the top.
060        Box topPanel = Box.createHorizontalBox();
061        this.add(topPanel);
062
063        topPanel.add(Box.createHorizontalStrut(10));
064        topPanel.add(new JLabel(message));
065
066        //  Create a panel to display in the center.
067        Box centerPanel = Box.createHorizontalBox();
068        centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
069        this.add(centerPanel);
070
071        //  Create a combo-box that allow the user to choose the desired unit.
072        centerPanel.add(new JLabel(typeStr, JLabel.RIGHT));
073        _unitCBox = new JComboBox(unitOptions);
074        _unitCBox.setMaximumSize(_unitCBox.getPreferredSize());
075        centerPanel.add(_unitCBox);
076
077        if (Objects.nonNull(initial))
078            _unitCBox.setSelectedItem(initial);
079    }
080
081    /**
082     * @return the selected units.
083     */
084    public Unit<Q> getSelected() {
085        return (Unit<Q>)_unitCBox.getSelectedItem();
086    }
087
088}