001package jmri.jmrit.operations.trains.schedules; 002 003import java.awt.Dimension; 004import java.awt.GridBagLayout; 005 006import javax.swing.*; 007 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import jmri.InstanceManager; 012import jmri.jmrit.operations.OperationsFrame; 013import jmri.jmrit.operations.setup.Control; 014 015/** 016 * Used to edit train schedules. 017 * 018 * @author Daniel Boudreau Copyright (C) 019 * 020 * 021 */ 022public class TrainsScheduleEditFrame extends OperationsFrame implements java.beans.PropertyChangeListener { 023 024 // text box 025 JTextField addTextBox = new JTextField(Control.max_len_string_attibute); 026 027 // combo box 028 private JComboBox<TrainSchedule> comboBox = null; 029 030 // major buttons 031 JButton addButton = new JButton(Bundle.getMessage("Add")); 032 JButton deleteButton = new JButton(Bundle.getMessage("ButtonDelete")); 033 JButton replaceButton = new JButton(Bundle.getMessage("Replace")); 034 035 JButton restoreButton = new JButton(Bundle.getMessage("Restore")); 036 037 TrainScheduleManager trainScheduleManager = null; 038 039 public TrainsScheduleEditFrame() { 040 super(); 041 042 trainScheduleManager = InstanceManager.getDefault(TrainScheduleManager.class); 043 044 // the following code sets the frame's initial state 045 getContentPane().setLayout(new GridBagLayout()); 046 047 trainScheduleManager.addPropertyChangeListener(this); 048 049 initComponents(); 050 } 051 052 @Override 053 public void initComponents() { 054 try { 055 comboBox = trainScheduleManager.getComboBox(); 056 } catch(IllegalArgumentException iae) { 057 comboBox = new JComboBox<>(); 058 trainScheduleManager.updateComboBox(comboBox); 059 } 060 061 // row 1 062 addItem(addTextBox, 2, 2); 063 addItem(addButton, 3, 2); 064 065 // row 3 066 addItem(comboBox, 2, 3); 067 addItem(deleteButton, 3, 3); 068 069 // row 4 070 addItem(replaceButton, 3, 4); 071 072 // row 5 073 addItem(restoreButton, 2, 5); 074 restoreButton.setToolTipText(Bundle.getMessage("RestoreScheduleTip")); 075 076 addButtonAction(addButton); 077 addButtonAction(deleteButton); 078 addButtonAction(replaceButton); 079 addButtonAction(restoreButton); 080 081 setTitle(Bundle.getMessage("MenuItemEditSchedule")); 082 initMinimumSize(new Dimension(Control.panelWidth300, Control.panelHeight200)); 083 084 } 085 086 @Override 087 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 088 if (ae.getSource() == deleteButton && comboBox.getSelectedItem() != null) { 089 trainScheduleManager.deregister((TrainSchedule) comboBox.getSelectedItem()); 090 } 091 if (ae.getSource() == restoreButton) { 092 trainScheduleManager.createDefaultSchedules(); 093 } 094 // check for valid name 095 String s = addTextBox.getText(); 096 s = s.trim(); 097 if (s.isEmpty()) { 098 return; // done 099 } 100 if (ae.getSource() == addButton) { 101 trainScheduleManager.newSchedule(s); 102 } 103 if (ae.getSource() == replaceButton && comboBox.getSelectedItem() != null) { 104 TrainSchedule ts = ((TrainSchedule) comboBox.getSelectedItem()); 105 ts.setName(s); 106 } 107 } 108 109 @Override 110 public void dispose() { 111 trainScheduleManager.removePropertyChangeListener(this); 112 super.dispose(); 113 } 114 115 @Override 116 public void propertyChange(java.beans.PropertyChangeEvent e) { 117 if (Control.SHOW_PROPERTY) { 118 log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e 119 .getNewValue()); 120 } 121 trainScheduleManager.updateComboBox(comboBox); 122 } 123 124 private final static Logger log = LoggerFactory.getLogger(TrainsScheduleEditFrame.class); 125}