001package jmri.jmrit.logixng.expressions.swing; 002 003import java.util.List; 004 005import javax.annotation.CheckForNull; 006import javax.annotation.Nonnull; 007import javax.swing.*; 008 009import jmri.InstanceManager; 010import jmri.jmrit.logixng.*; 011import jmri.jmrit.logixng.expressions.TimeSinceMidnight; 012import jmri.jmrit.logixng.expressions.TimeSinceMidnight.Type; 013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 014import jmri.util.swing.JComboBoxUtil; 015 016/** 017 * Configures an TimeSinceMidnight object with a Swing JPanel. 018 * 019 * @author Daniel Bergqvist Copyright 2021 020 * @author Dave Sand Copyright 2021 021 */ 022public class TimeSinceMidnightSwing extends AbstractAnalogExpressionSwing { 023 024 private JComboBox<Type> _stateComboBox; 025 026 @Override 027 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 028 TimeSinceMidnight expression = (TimeSinceMidnight) object; 029 panel = new JPanel(); 030 031 _stateComboBox = new JComboBox<>(); 032 for (Type e : Type.values()) { 033 _stateComboBox.addItem(e); 034 } 035 JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox); 036 037 if (expression != null) { 038 _stateComboBox.setSelectedItem(expression.getType()); 039 } 040 041 JComponent[] components = new JComponent[]{ 042 _stateComboBox 043 }; 044 045 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 046 Bundle.getMessage("TimeSinceMidnight_Components"), components); 047 048 for (JComponent c : componentList) panel.add(c); 049 } 050 051 /** {@inheritDoc} */ 052 @Override 053 public boolean validate(@Nonnull List<String> errorMessages) { 054 return true; 055 } 056 057 /** {@inheritDoc} */ 058 @Override 059 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 060 TimeSinceMidnight expression = new TimeSinceMidnight(systemName, userName); 061 expression.setType(TimeSinceMidnight.Type.FastClock); 062 return InstanceManager.getDefault(AnalogExpressionManager.class).registerExpression(expression); 063 } 064 065 /** {@inheritDoc} */ 066 @Override 067 public void updateObject(@Nonnull Base object) { 068 if (! (object instanceof TimeSinceMidnight)) { 069 throw new IllegalArgumentException("object must be an TimeSinceMidnight but is a: "+object.getClass().getName()); 070 } 071 TimeSinceMidnight expression = (TimeSinceMidnight) object; 072 expression.setType(_stateComboBox.getItemAt(_stateComboBox.getSelectedIndex())); 073 } 074 075 /** {@inheritDoc} */ 076 @Override 077 public String toString() { 078 return Bundle.getMessage("TimeSinceMidnight_Short"); 079 } 080 081 @Override 082 public void dispose() { 083 } 084 085 086// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrueSwing.class); 087 088}