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.panelWidth400, 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        if (s.isBlank()) {
097            return; // done
098        }
099        if (ae.getSource() == addButton) {
100            trainScheduleManager.newSchedule(s);
101        }
102        if (ae.getSource() == replaceButton && comboBox.getSelectedItem() != null) {
103            TrainSchedule ts = ((TrainSchedule) comboBox.getSelectedItem());
104            ts.setName(s);
105        }
106    }
107
108    @Override
109    public void dispose() {
110        trainScheduleManager.removePropertyChangeListener(this);
111        super.dispose();
112    }
113
114    @Override
115    public void propertyChange(java.beans.PropertyChangeEvent e) {
116        if (Control.SHOW_PROPERTY) {
117            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e
118                    .getNewValue());
119        }
120        trainScheduleManager.updateComboBox(comboBox);
121    }
122
123    private static final Logger log = LoggerFactory.getLogger(TrainsScheduleEditFrame.class);
124}