001package jmri.util.swing; 002 003import java.awt.FlowLayout; 004import java.util.function.Predicate; 005 006import javax.annotation.Nonnull; 007import javax.swing.BoxLayout; 008import javax.swing.JPanel; 009 010import jmri.Manager; 011import jmri.NamedBean; 012import jmri.NamedBean.DisplayOptions; 013import jmri.NamedBeanHandle; 014import jmri.swing.NamedBeanComboBox; 015 016/** 017 * Create a JPanel containing a NamedBeanComboBox. The default display option is 018 * DISPLAYNAME. JComboBoxUtil.setupComboBoxMaxRows() will be invoked. 019 * 020 * @param <E> the type of NamedBean 021 * 022 * @author Dave Sand Copyright 2021 023 */ 024public class BeanSelectPanel<E extends NamedBean> extends JPanel { 025 026 E _selection; 027 NamedBeanComboBox<E> _beanComboBox; 028 DisplayOptions _display; 029 030 public BeanSelectPanel(@Nonnull Manager<E> manager, E selection) { 031 this(manager, selection, DisplayOptions.DISPLAYNAME); 032 } 033 034 public BeanSelectPanel(@Nonnull Manager<E> manager, E selection, DisplayOptions display) { 035 this(manager, selection, display, true); 036 } 037 038 /** 039 * Create a JPanel that contains a named bean combo box. 040 * 041 * @param manager The bean manager 042 * @param selection The bean that is selected, null for no selection. 043 * @param display The bean display option, null for default DISPLAYNAME. 044 * @param maxRows Should max rows be enabled; if false the JComboBox default of 8 will be used. 045 */ 046 public BeanSelectPanel(@Nonnull Manager<E> manager, E selection, DisplayOptions display, boolean maxRows) { 047 this(manager, selection, display, maxRows, null); 048 } 049 050 /** 051 * Create a JPanel that contains a named bean combo box. 052 * 053 * @param manager The bean manager 054 * @param selection The bean that is selected, null for no selection. 055 * @param display The bean display option, null for default DISPLAYNAME. 056 * @param maxRows Should max rows be enabled; if false the JComboBox default of 8 will be used. 057 * @param filter The filter or null if no filter 058 */ 059 public BeanSelectPanel(@Nonnull Manager<E> manager, E selection, DisplayOptions display, boolean maxRows, Predicate<E> filter) { 060 _selection = selection; 061 _display = display == null ? DisplayOptions.DISPLAYNAME : display; 062 063 _beanComboBox = new NamedBeanComboBox<>(manager, selection, _display, filter); 064 _beanComboBox.setAllowNull(true); 065 if (maxRows) JComboBoxUtil.setupComboBoxMaxRows(_beanComboBox); 066 067 JPanel bean = new JPanel(); 068 bean.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0)); 069 bean.add(_beanComboBox); 070 super.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 071 super.add(bean); 072 } 073 074 /* 075 * Provide the actual combo box object to provide direct access, such as adding listeners. 076 * @return the combo box 077 */ 078 public NamedBeanComboBox<E> getBeanCombo() { 079 return _beanComboBox; 080 } 081 082 /** 083 * Get the named bean that has been selected. 084 * @return the selected bean which may be null if the first row is selected. 085 */ 086 public E getNamedBean() { 087 return _beanComboBox.getSelectedItem(); 088 } 089 090 /** 091 * Set the default selected item in the combo box. 092 * @param nBean the bean that is selected by default 093 */ 094 public void setDefaultNamedBean(E nBean) { 095 _selection = nBean; 096 _beanComboBox.setSelectedItem(_selection); 097 } 098 099 /** 100 * Set the default selected item in the combo box. 101 * @param nBeanHandle the bean that is selected by default 102 */ 103 public void setDefaultNamedBean(NamedBeanHandle<E> nBeanHandle) { 104 if (nBeanHandle != null) { 105 _selection = nBeanHandle.getBean(); 106 _beanComboBox.setSelectedItem(_selection); 107 } else { 108 _selection = null; 109 _beanComboBox.setSelectedItem(null); 110 } 111 } 112 113 /** 114 * Check that the user selected something in this BeanSelectPanel. 115 * @return true if nothing selected 116 */ 117 public boolean isEmpty() { 118 return _beanComboBox.getSelectedIndex() < 1; 119 } 120 121 public void dispose() { 122 _beanComboBox.dispose(); 123 } 124 125 126 //initialize logging 127// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BeanSelectPanel.class); 128}