001package jmri.jmrit.operations.locations.schedules.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.OperationsFrame;
010import jmri.jmrit.operations.locations.Track;
011import jmri.jmrit.operations.locations.schedules.*;
012import jmri.jmrit.operations.setup.Control;
013import jmri.util.swing.JmriJOptionPane;
014
015/**
016 * Frame for copying a schedule for operations.
017 *
018 * @author Bob Jacobsen Copyright (C) 2001
019 * @author Daniel Boudreau Copyright (C) 2015, 2025
020 */
021public class ScheduleCopyFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
022
023    ScheduleManager scheduleManager = InstanceManager.getDefault(ScheduleManager.class);
024
025    // text field
026    JTextField scheduleNameTextField = new javax.swing.JTextField(Control.max_len_string_location_name);
027
028    // major buttons
029    JButton copyButton = new javax.swing.JButton(Bundle.getMessage("ButtonCopy"));
030
031    // combo boxes
032    JComboBox<Schedule> scheduleBox = scheduleManager.getComboBox();
033    
034    Track _track = null;
035
036    public ScheduleCopyFrame() {
037        this(null, null);
038    }
039
040    public ScheduleCopyFrame(Schedule schedule, Track track) {
041        super(Bundle.getMessage("MenuItemCopySchedule"));
042
043        _track = track;
044
045        // general GUI config
046        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
047
048        // Set up the panels
049        // Layout the panel by rows
050        // row 1
051        JPanel pName = new JPanel();
052        pName.setLayout(new GridBagLayout());
053        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("ScheduleName")));
054        addItem(pName, scheduleNameTextField, 0, 0);
055
056        // row 2
057        JPanel pCopy = new JPanel();
058        pCopy.setLayout(new GridBagLayout());
059        pCopy.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectScheduleToCopy")));
060        addItem(pCopy, scheduleBox, 0, 0);
061
062        // row 4
063        JPanel pButton = new JPanel();
064        pButton.setLayout(new GridBagLayout());
065        addItem(pButton, copyButton, 0, 0);
066
067        getContentPane().add(pName);
068        getContentPane().add(pCopy);
069        getContentPane().add(pButton);
070
071        // get notified if combo box gets modified
072        scheduleManager.addPropertyChangeListener(this);
073
074        // add help menu to window
075        addHelpMenu("package.jmri.jmrit.operations.Operations_Schedules", true); // NOI18N
076
077        // set up buttons
078        addButtonAction(copyButton);
079 
080        scheduleBox.setSelectedItem(schedule);
081        
082        initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight250));
083    }
084
085    @Override
086    protected void buttonActionPerformed(java.awt.event.ActionEvent ae) {
087        if (ae.getSource() == copyButton) {
088            log.debug("copy Schedule button activated");
089            if (!checkName()) {
090                return;
091            }
092
093            if (scheduleBox.getSelectedItem() == null) {
094                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("SelectScheduleToCopy"),
095                        Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
096                        JmriJOptionPane.ERROR_MESSAGE);
097                return;
098            }
099
100            Schedule schedule = (Schedule) scheduleBox.getSelectedItem();
101            Schedule copiedSchedule = scheduleManager.copySchedule(schedule, scheduleNameTextField.getText());
102            new ScheduleEditFrame(copiedSchedule, _track);
103        }
104    }
105
106    protected void updateComboBoxes() {
107        log.debug("update Schedule combobox");
108        Object item = scheduleBox.getSelectedItem(); // remember which object was selected
109        scheduleManager.updateComboBox(scheduleBox);
110        scheduleBox.setSelectedItem(item);
111    }
112
113    /**
114     *
115     * @return true if name entered and isn't too long
116     */
117    protected boolean checkName() {
118        if (scheduleNameTextField.getText().trim().isEmpty()) {
119            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustEnterName"),
120                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
121                    JmriJOptionPane.ERROR_MESSAGE);
122            return false;
123        }
124        if (scheduleNameTextField.getText().length() > Control.max_len_string_location_name) {
125            JmriJOptionPane.showMessageDialog(this,
126                    Bundle.getMessage("ScheduleNameLengthMax",
127                            Integer.toString(Control.max_len_string_location_name + 1)),
128                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
129                    JmriJOptionPane.ERROR_MESSAGE);
130            return false;
131        }
132        Schedule check = scheduleManager.getScheduleByName(scheduleNameTextField.getText());
133        if (check != null) {
134            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ScheduleAlreadyExists"),
135                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
136                    JmriJOptionPane.ERROR_MESSAGE);
137            return false;
138        }
139        return true;
140    }
141
142    @Override
143    public void dispose() {
144        scheduleManager.removePropertyChangeListener(this);
145        super.dispose();
146    }
147
148    @Override
149    public void propertyChange(java.beans.PropertyChangeEvent e) {
150        log.debug("PropertyChange ({}) new: ({})", e.getPropertyName(), e.getNewValue());
151        if (e.getPropertyName().equals(ScheduleManager.LISTLENGTH_CHANGED_PROPERTY)) {
152            updateComboBoxes();
153        }
154    }
155
156    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleCopyFrame.class);
157}