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.schedules.Schedule;
011import jmri.jmrit.operations.locations.schedules.ScheduleManager;
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
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    public ScheduleCopyFrame() {
035        this(null);
036    }
037
038    public ScheduleCopyFrame(Schedule schedule) {
039        super(Bundle.getMessage("MenuItemCopySchedule"));
040
041        // general GUI config
042        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
043
044        // Set up the panels
045        // Layout the panel by rows
046        // row 1
047        JPanel pName = new JPanel();
048        pName.setLayout(new GridBagLayout());
049        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("ScheduleName")));
050        addItem(pName, scheduleNameTextField, 0, 0);
051
052        // row 2
053        JPanel pCopy = new JPanel();
054        pCopy.setLayout(new GridBagLayout());
055        pCopy.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectScheduleToCopy")));
056        addItem(pCopy, scheduleBox, 0, 0);
057
058        // row 4
059        JPanel pButton = new JPanel();
060        pButton.setLayout(new GridBagLayout());
061        addItem(pButton, copyButton, 0, 0);
062
063        getContentPane().add(pName);
064        getContentPane().add(pCopy);
065        getContentPane().add(pButton);
066
067        // get notified if combo box gets modified
068        scheduleManager.addPropertyChangeListener(this);
069
070        // add help menu to window
071        addHelpMenu("package.jmri.jmrit.operations.Operations_Schedules", true); // NOI18N
072
073        // set up buttons
074        addButtonAction(copyButton);
075 
076        scheduleBox.setSelectedItem(schedule);
077        
078        initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight250));
079    }
080
081    @Override
082    protected void buttonActionPerformed(java.awt.event.ActionEvent ae) {
083        if (ae.getSource() == copyButton) {
084            log.debug("copy Schedule button activated");
085            if (!checkName()) {
086                return;
087            }
088
089            if (scheduleBox.getSelectedItem() == null) {
090                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("SelectScheduleToCopy"),
091                        Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
092                        JmriJOptionPane.ERROR_MESSAGE);
093                return;
094            }
095
096            Schedule schedule = (Schedule) scheduleBox.getSelectedItem();
097            scheduleManager.copySchedule(schedule, scheduleNameTextField.getText());
098        }
099    }
100
101    protected void updateComboBoxes() {
102        log.debug("update Schedule combobox");
103        Object item = scheduleBox.getSelectedItem(); // remember which object was selected
104        scheduleManager.updateComboBox(scheduleBox);
105        scheduleBox.setSelectedItem(item);
106    }
107
108    /**
109     *
110     * @return true if name entered and isn't too long
111     */
112    protected boolean checkName() {
113        if (scheduleNameTextField.getText().trim().isEmpty()) {
114            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustEnterName"),
115                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
116                    JmriJOptionPane.ERROR_MESSAGE);
117            return false;
118        }
119        if (scheduleNameTextField.getText().length() > Control.max_len_string_location_name) {
120            JmriJOptionPane.showMessageDialog(this,
121                    Bundle.getMessage("ScheduleNameLengthMax",
122                            Integer.toString(Control.max_len_string_location_name + 1)),
123                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
124                    JmriJOptionPane.ERROR_MESSAGE);
125            return false;
126        }
127        Schedule check = scheduleManager.getScheduleByName(scheduleNameTextField.getText());
128        if (check != null) {
129            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ScheduleAlreadyExists"),
130                    Bundle.getMessage("CanNotSchedule", Bundle.getMessage("ButtonCopy")),
131                    JmriJOptionPane.ERROR_MESSAGE);
132            return false;
133        }
134        return true;
135    }
136
137    @Override
138    public void dispose() {
139        scheduleManager.removePropertyChangeListener(this);
140        super.dispose();
141    }
142
143    @Override
144    public void propertyChange(java.beans.PropertyChangeEvent e) {
145        log.debug("PropertyChange ({}) new: ({})", e.getPropertyName(), e.getNewValue());
146        if (e.getPropertyName().equals(ScheduleManager.LISTLENGTH_CHANGED_PROPERTY)) {
147            updateComboBoxes();
148        }
149    }
150
151    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleCopyFrame.class);
152}