001package jmri.util.swing; 002 003/** 004 * Wrap an object for easier null handling in a JComboBox. ("JCB" refers to 005 * JComboBox) 006 * 007 * Define a {@code JComboBox<JCBHandle<Foo>>}, then fill it with a new 008 * {@code JCBHandle("None string")} and your new {@code JCBHandle(foo)} entries. 009 * 010 * @param <T> the class accepted by the JComboBox 011 */ 012public class JCBHandle<T> { 013 014 T item = null; 015 String label; 016 017 /** 018 * Create a handle with a handled object. 019 * 020 * @param t the class accepted by the JComboBox 021 */ 022 public JCBHandle(T t) { 023 item = t; 024 } 025 026 /** 027 * Create a handle without a handled object, just a display label. 028 * 029 * @param l label for handle 030 */ 031 public JCBHandle(String l) { 032 label = l; 033 } 034 035 /** 036 * Display the handled item, or if there isn't one, the null-case label. 037 * 038 * @return the item's String representation or the default label 039 */ 040 @Override 041 public String toString() { 042 if (item != null) { 043 return item.toString(); 044 } else { 045 return label; 046 } 047 } 048 049 /** 050 * Retrieve the handled object for this handle 051 * @return the object 052 */ 053 public T item() { 054 return item; 055 } 056 057}