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