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