001package jmri.jmrit.operations.automation;
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.setup.Control;
011import jmri.util.swing.JmriJOptionPane;
012
013/**
014 * Frame for making a new copy of a automation.
015 *
016 * @author Bob Jacobsen Copyright (C) 2001
017 * @author Daniel Boudreau Copyright (C) 2016
018 */
019public class AutomationCopyFrame extends OperationsFrame {
020
021    AutomationManager automationManager = InstanceManager.getDefault(AutomationManager.class);
022
023    // labels
024    // text field
025    javax.swing.JTextField automationNameTextField = new javax.swing.JTextField(Control.max_len_string_automation_name);
026
027    // major buttons
028    javax.swing.JButton copyButton = new javax.swing.JButton(Bundle.getMessage("ButtonCopy"));
029
030    // combo boxes
031    JComboBox<Automation> automationBox = InstanceManager.getDefault(AutomationManager.class).getComboBox();
032
033    public AutomationCopyFrame(Automation automation) {
034        // general GUI config
035
036        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
037
038        // Set up the panels
039        // Layout the panel by rows
040        // row 1
041        JPanel pName = new JPanel();
042        pName.setLayout(new GridBagLayout());
043        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Name")));
044        addItem(pName, automationNameTextField, 0, 0);
045
046        // row 2
047        JPanel pCopy = new JPanel();
048        pCopy.setLayout(new GridBagLayout());
049        pCopy.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectAutomation")));
050        addItem(pCopy, automationBox, 0, 0);
051
052        automationBox.setSelectedItem(automation);
053
054        // row 4
055        JPanel pButton = new JPanel();
056        pButton.add(copyButton);
057
058        getContentPane().add(pName);
059        getContentPane().add(pCopy);
060        getContentPane().add(pButton);
061
062        // add help menu to window
063        addHelpMenu("package.jmri.jmrit.operations.Operations_CopyAutomation", true); // NOI18N
064
065        setTitle(Bundle.getMessage("TitleAutomationCopy"));
066
067        // setup buttons
068        addButtonAction(copyButton);
069        
070        initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight200));
071    }
072
073    @Override
074    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
075        if (ae.getSource() == copyButton) {
076            log.debug("copy automation button activated");
077            if (!checkName()) {
078                return;
079            }
080
081            Automation newAutomation = automationManager.getAutomationByName(automationNameTextField.getText());
082            if (newAutomation != null) {
083                reportAutomationExists();
084                return;
085            }
086            if (automationBox.getSelectedItem() == null) {
087                reportAutomationDoesNotExist();
088                return;
089            }
090            Automation oldAutomation = (Automation) automationBox.getSelectedItem();
091            if (oldAutomation == null) {
092                reportAutomationDoesNotExist();
093                return;
094            }
095
096            // now copy
097            newAutomation = automationManager.copyAutomation(oldAutomation, automationNameTextField.getText());
098            new AutomationTableFrame(newAutomation);
099        }
100    }
101
102    private void reportAutomationExists() {
103        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ReportExists"), Bundle
104                .getMessage("CanNotCopyAutomation"), JmriJOptionPane.ERROR_MESSAGE);
105    }
106
107    private void reportAutomationDoesNotExist() {
108        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("SelectAutomation"), Bundle
109                .getMessage("CanNotCopyAutomation"), JmriJOptionPane.ERROR_MESSAGE);
110    }
111
112    /**
113     *
114     * @return true if name isn't too long
115     */
116    private boolean checkName() {
117        if (automationNameTextField.getText().trim().isEmpty()) {
118            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("EnterAutomationName"), Bundle
119                    .getMessage("CanNotCopyAutomation"), JmriJOptionPane.ERROR_MESSAGE);
120            return false;
121        }
122        if (automationNameTextField.getText().length() > Control.max_len_string_automation_name) {
123            JmriJOptionPane.showMessageDialog(this,
124                    Bundle.getMessage("AutomationNameLengthMax", Control.max_len_string_automation_name),
125                    Bundle.getMessage("CanNotCopyAutomation"), JmriJOptionPane.ERROR_MESSAGE);
126            return false;
127        }
128        return true;
129    }
130
131    @Override
132    public void dispose() {
133        super.dispose();
134    }
135
136    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AutomationCopyFrame.class);
137
138}