001package jmri.jmrit.logixng.expressions.swing; 002 003import java.time.LocalTime; 004import java.time.format.DateTimeFormatter; 005 006import java.util.List; 007 008import javax.annotation.CheckForNull; 009import javax.annotation.Nonnull; 010import javax.swing.*; 011 012import jmri.InstanceManager; 013import jmri.jmrit.logixng.*; 014import jmri.jmrit.logixng.expressions.ExpressionClock; 015import jmri.jmrit.logixng.expressions.ExpressionClock.Type; 016import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 017import jmri.util.swing.JComboBoxUtil; 018 019/** 020 * Configures an ExpressionClock object with a Swing JPanel. 021 * 022 * @author Daniel Bergqvist Copyright 2021 023 * @author Dave Sand Copyright 2021 024 */ 025public class ExpressionClockSwing extends AbstractDigitalExpressionSwing { 026 027 private JComboBox<Type> _stateComboBox; 028 private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox; 029 private JTextField _beginTextField; 030 private JTextField _endTextField; 031 032 private int _beginMinutes; 033 private int _endMinutes; 034 035 @Override 036 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 037 ExpressionClock expression = (ExpressionClock) object; 038 panel = new JPanel(); 039 040 _stateComboBox = new JComboBox<>(); 041 for (Type e : Type.values()) { 042 _stateComboBox.addItem(e); 043 } 044 JComboBoxUtil.setupComboBoxMaxRows(_stateComboBox); 045 046 _is_IsNot_ComboBox = new JComboBox<>(); 047 for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) { 048 _is_IsNot_ComboBox.addItem(e); 049 } 050 JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox); 051 052 _beginTextField = new JTextField(4); 053 _beginTextField.setText(ExpressionClock.formatTime(0)); 054 055 056 _endTextField = new JTextField(4); 057 _endTextField.setText(ExpressionClock.formatTime(0)); 058 059 if (expression != null) { 060 _stateComboBox.setSelectedItem(expression.getType()); 061 _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot()); 062 _beginTextField.setText(ExpressionClock.formatTime(expression.getBeginTime())); 063 _endTextField.setText(ExpressionClock.formatTime(expression.getEndTime())); 064 } 065 066 JComponent[] components = new JComponent[]{ 067 _stateComboBox, 068 _is_IsNot_ComboBox, 069 _beginTextField, 070 _endTextField 071 }; 072 073 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 074 Bundle.getMessage("ExpressionClock_Components"), components); 075 076 for (JComponent c : componentList) panel.add(c); 077 } 078 079 /** {@inheritDoc} */ 080 @Override 081 public boolean validate(@Nonnull List<String> errorMessages) { 082 _beginMinutes = validateTime(errorMessages, _beginTextField); 083 _endMinutes = validateTime(errorMessages, _endTextField); 084 return errorMessages.isEmpty(); 085 } 086 087 private int validateTime(List<String> errorMessages, JTextField timeField) { 088 int minutes = 0; 089 try { 090 LocalTime newHHMM = LocalTime.parse(timeField.getText().trim(), DateTimeFormatter.ofPattern("H:mm")); 091 092 minutes = newHHMM.getHour() * 60 + newHHMM.getMinute(); 093 if (minutes < 0 || minutes > 1439) { 094 errorMessages.add(Bundle.getMessage("Clock_RangeError")); 095 } 096 } catch (java.time.format.DateTimeParseException ex) { 097 errorMessages.add(Bundle.getMessage("Clock_ParseError", ex.getParsedString())); 098 } 099 return minutes; 100 } 101 102 /** {@inheritDoc} */ 103 @Override 104 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 105 ExpressionClock expression = new ExpressionClock(systemName, userName); 106 updateObject(expression); 107 return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression); 108 } 109 110 /** {@inheritDoc} */ 111 @Override 112 public void updateObject(@Nonnull Base object) { 113 if (! (object instanceof ExpressionClock)) { 114 throw new IllegalArgumentException("object must be an ExpressionClock but is a: "+object.getClass().getName()); 115 } 116 ExpressionClock expression = (ExpressionClock) object; 117 expression.setType(_stateComboBox.getItemAt(_stateComboBox.getSelectedIndex())); 118 expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem()); 119 expression.setRange(_beginMinutes, _endMinutes); 120 } 121 122 /** {@inheritDoc} */ 123 @Override 124 public String toString() { 125 return Bundle.getMessage("Clock_Short"); 126 } 127 128 @Override 129 public void dispose() { 130 } 131 132 133// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrueSwing.class); 134 135}