001package jmri.jmrit.operations.routes.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.routes.*;
011import jmri.jmrit.operations.setup.Control;
012import jmri.util.swing.JmriJOptionPane;
013
014/**
015 * Frame for copying a route for operations.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001
018 * @author Daniel Boudreau Copyright (C) 2008, 2010
019 */
020public class RouteCopyFrame extends OperationsFrame {
021
022    RouteManager routeManager = InstanceManager.getDefault(RouteManager.class);
023
024    // labels
025    javax.swing.JLabel textCopyRoute = new javax.swing.JLabel(Bundle.getMessage("CopyRoute"));
026    javax.swing.JLabel textRouteName = new javax.swing.JLabel(Bundle.getMessage("RouteName"));
027
028    // text field
029    javax.swing.JTextField routeNameTextField = new javax.swing.JTextField(Control.max_len_string_route_name);
030
031    // check boxes
032    javax.swing.JCheckBox invertCheckBox = new javax.swing.JCheckBox(Bundle.getMessage("Invert"));
033
034    // major buttons
035    javax.swing.JButton copyButton = new javax.swing.JButton(Bundle.getMessage("ButtonCopy"));
036
037    // combo boxes
038    JComboBox<Route> routeBox = InstanceManager.getDefault(RouteManager.class).getComboBox();
039
040    public RouteCopyFrame(Route route) {
041        super(Bundle.getMessage("TitleRouteCopy"));
042        // general GUI config
043
044        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
045
046        // Set up the panels
047        JPanel p1 = new JPanel();
048        p1.setLayout(new GridBagLayout());
049
050        // Layout the panel by rows
051        // row 1 textRouteName
052        addItem(p1, textRouteName, 0, 1);
053        addItemWidth(p1, routeNameTextField, 3, 1, 1);
054
055        // row 2
056        addItem(p1, textCopyRoute, 0, 2);
057        addItemWidth(p1, routeBox, 3, 1, 2);
058
059        // row 4
060        addItem(p1, invertCheckBox, 0, 4);
061        addItem(p1, copyButton, 1, 4);
062
063        getContentPane().add(p1);
064
065        // add help menu to window
066        addHelpMenu("package.jmri.jmrit.operations.Operations_CopyRoute", true); // NOI18N
067
068        initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight200));
069
070        // setup buttons
071        addButtonAction(copyButton);
072
073        routeBox.setSelectedItem(route);
074    }
075
076    @Override
077    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
078        if (ae.getSource() == copyButton) {
079            log.debug("copy route button activated");
080            if (!checkName()) {
081                return;
082            }
083
084            Route newRoute = routeManager.getRouteByName(routeNameTextField.getText());
085            if (newRoute != null) {
086                reportRouteExists(Bundle.getMessage("add"));
087                return;
088            }
089            if (routeBox.getSelectedItem() == null) {
090                reportRouteDoesNotExist();
091                return;
092            }
093
094            // now copy
095            Route oldRoute = (Route) routeBox.getSelectedItem();
096            newRoute = routeManager.copyRoute(oldRoute, routeNameTextField.getText(),
097                    invertCheckBox.isSelected());
098
099            RouteEditFrame f = new RouteEditFrame();
100            f.initComponents(newRoute);
101        }
102    }
103
104    private void reportRouteExists(String s) {
105        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ReportExists"),
106                Bundle.getMessage("CanNotRoute", s),
107                JmriJOptionPane.ERROR_MESSAGE);
108    }
109
110    private void reportRouteDoesNotExist() {
111        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("CopyRoute"),
112                Bundle.getMessage("CopyRoute"), JmriJOptionPane.ERROR_MESSAGE);
113    }
114
115    /**
116     * @return true if name length is okay
117     */
118    private boolean checkName() {
119        if (routeNameTextField.getText().trim().isEmpty()) {
120            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("EnterRouteName"),
121                    Bundle.getMessage("EnterRouteName"), JmriJOptionPane.ERROR_MESSAGE);
122            return false;
123        }
124        if (routeNameTextField.getText().length() > Control.max_len_string_route_name) {
125            JmriJOptionPane.showMessageDialog(this, 
126                    Bundle.getMessage("RouteNameLess",
127                    Control.max_len_string_route_name + 1),
128                    Bundle
129                            .getMessage("CanNotAddRoute"),
130                    JmriJOptionPane.ERROR_MESSAGE);
131            return false;
132        }
133        return true;
134    }
135
136    @Override
137    public void dispose() {
138        super.dispose();
139    }
140
141    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RouteCopyFrame.class);
142}