001package net.kreatious.pianoleopard.keyboardselect; 002 003import java.awt.Container; 004import java.util.prefs.Preferences; 005import java.util.stream.IntStream; 006 007import javax.swing.ImageIcon; 008import javax.swing.JComboBox; 009import javax.swing.JLabel; 010import javax.swing.JPanel; 011 012import com.jgoodies.forms.factories.FormFactory; 013import com.jgoodies.forms.layout.ColumnSpec; 014import com.jgoodies.forms.layout.FormLayout; 015import com.jgoodies.forms.layout.RowSpec; 016 017/** 018 * Provides a GUI element for selecting the options of a lighted keyboard 019 * 020 * @author Jay-R Studer 021 */ 022public class LightedKeyboardSelector { 023 /** 024 * The preference storage location for the navigation channel. 025 * <p> 026 * The channel is stored in the range 0..15. 027 */ 028 public static final String NAV_CHANNEL_PREFERENCE = "navigationChannel"; 029 030 private final JComboBox<Integer> channels = new JComboBox<>(IntStream.rangeClosed(1, 16).boxed() 031 .toArray(Integer[]::new)); 032 033 /** 034 * Constructs a new {@link LightedKeyboardSelector} for selecting the 035 * options of a lighted keyboard 036 */ 037 LightedKeyboardSelector() { 038 } 039 040 /** 041 * Adds this selector to the specified container. 042 * <p> 043 * The container must have a JGoodies FormLayout with 1 row and 5 columns. 044 * 045 * @param container 046 * the container to add to 047 * @param x 048 * the column to add to 049 * @param y 050 * the row to add to 051 * @param width 052 * the number of columns to take up 053 */ 054 void addToContainer(Container container, int x, int y, int width) { 055 final JPanel panel = new JPanel(new FormLayout(new ColumnSpec[] { ColumnSpec.decode("default:grow"), 056 FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, }, 057 new RowSpec[] { FormFactory.DEFAULT_ROWSPEC })); 058 final JLabel lblInfo = new JLabel(new ImageIcon(LightedKeyboardSelector.class.getResource("/help.png"))); 059 lblInfo.setToolTipText("<html>Navigation channels are used by MIDI keyboards with lighted keys.<br>" 060 + "They determine which MIDI channel is used to light up the keys.</html>"); 061 panel.add(channels, "1, 1"); 062 panel.add(lblInfo, "3, 1"); 063 064 container.add(new JLabel("Navigation Channel:"), x + ", " + y + ", right, default"); 065 container.add(panel, x + 2 + ", " + y + ", " + (width - 2) + ", 1, fill, default"); 066 } 067 068 /** 069 * Loads the current settings from the user's preferences. 070 * 071 * @param preferences 072 * the preferences to load from 073 */ 074 void load(Preferences preferences) { 075 channels.setSelectedItem(preferences.getInt(NAV_CHANNEL_PREFERENCE, 3) + 1); 076 } 077 078 /** 079 * Saves the current setting to the user's preferences. 080 * <p> 081 * Any registered preference listeners will be called. 082 * 083 * @param preferences 084 * the preferences to save to 085 */ 086 void save(Preferences preferences) { 087 preferences.put(NAV_CHANNEL_PREFERENCE, Integer.toString((Integer) channels.getSelectedItem() - 1)); 088 } 089}