001package jmri.jmrit.beantable.routetable;
002
003import jmri.InstanceManager;
004import jmri.Route;
005import javax.swing.*;
006import java.awt.*;
007import java.awt.event.ActionEvent;
008
009/**
010 * Edit frame for the Route Table.
011 *
012 * Split from {@link jmri.jmrit.beantable.RouteTableAction}
013 *
014 * @author Dave Duchamp Copyright (C) 2004
015 * @author Bob Jacobsen Copyright (C) 2007
016 * @author Simon Reader Copyright (C) 2008
017 * @author Pete Cressman Copyright (C) 2009
018 * @author Egbert Broerse Copyright (C) 2016
019 * @author Paul Bender Copyright (C) 2020
020 */
021public class RouteEditFrame extends AbstractRouteAddEditFrame {
022
023    private final String systemName;
024
025    public RouteEditFrame() {
026        this(Bundle.getMessage("TitleEditRoute"));
027    }
028
029    public RouteEditFrame(String systemName) {
030        this(Bundle.getMessage("TitleEditRoute"), systemName);
031    }
032
033    public RouteEditFrame(String name, String systemName) {
034        this(name,false,true, systemName);
035    }
036
037    public RouteEditFrame(String name, boolean saveSize, boolean savePosition, String systemName) {
038        super(name, saveSize, savePosition);
039        this.systemName = systemName;
040        initComponents();
041    }
042
043    @Override
044    public final void initComponents() {
045        super.initComponents();
046        _systemName.setText(systemName);
047        // identify the Route with this name if it already exists
048        String sName = _systemName.getText();
049        Route g = InstanceManager.getDefault(jmri.RouteManager.class).getBySystemName(sName);
050        if (g == null) {
051            sName = _userName.getText();
052            g = InstanceManager.getDefault(jmri.RouteManager.class).getByUserName(sName);
053            if (g == null) {
054                // Route does not exist, so cannot be edited
055                status1.setText(Bundle.getMessage("RouteAddStatusErrorNotFound"));
056                return;
057            }
058        }
059        // Route was found, make its system name not changeable
060        curRoute = g;
061        _systemName.setVisible(true);
062        _systemName.setText(sName);
063        _systemName.setEnabled(false);
064        nameLabel.setEnabled(true);
065        _autoSystemName.setVisible(false);
066        // deactivate this Route
067        curRoute.deActivateRoute();
068        // get information for this route
069        _userName.setText(g.getUserName());
070
071        setPageContent(g);
072
073        // begin with showing all Turnouts
074        // set up buttons and notes
075        status1.setText(Bundle.getMessage("RouteAddStatusInitial3", Bundle.getMessage("ButtonUpdate")));
076        status2.setText(Bundle.getMessage("RouteAddStatusInitial4", Bundle.getMessage("ButtonCancelEdit", Bundle.getMessage("ButtonEdit"))));
077        status2.setVisible(true);
078        setTitle(Bundle.getMessage("TitleEditRoute"));
079        editMode = true;
080    }
081
082    @Override
083    protected JPanel getButtonPanel() {
084        final JButton cancelEditButton = new JButton(Bundle.getMessage("ButtonCancelEdit", Bundle.getMessage("ButtonEdit"))); // I18N for word sequence "Cancel Edit"
085        final JButton deleteButton = new JButton(Bundle.getMessage("ButtonDelete") + " " + Bundle.getMessage("BeanNameRoute")); // I18N "Delete Route"
086        final JButton updateButton = new JButton(Bundle.getMessage("ButtonUpdate"));
087        final JButton exportButton = new JButton(Bundle.getMessage("ButtonExport"));
088        // add Buttons panel
089        JPanel pb = new JPanel();
090        pb.setLayout(new FlowLayout(FlowLayout.TRAILING));
091        // CancelEdit button
092        pb.add(cancelEditButton);
093        cancelEditButton.addActionListener(this::cancelPressed);
094        cancelEditButton.setToolTipText(Bundle.getMessage("TooltipCancelRoute"));
095        // Delete Route button
096        pb.add(deleteButton);
097        deleteButton.addActionListener(this::deletePressed);
098        deleteButton.setToolTipText(Bundle.getMessage("TooltipDeleteRoute"));
099        // Update Route button
100        pb.add(updateButton);
101        updateButton.addActionListener((ActionEvent e1) -> updatePressed(false));
102        updateButton.setToolTipText(Bundle.getMessage("TooltipUpdateRoute"));
103        // Export button
104        pb.add(exportButton);
105        exportButton.addActionListener(this::exportButtonPressed);
106        exportButton.setToolTipText(Bundle.getMessage("TooltipExportRoute"));
107
108        // Show the initial buttons, and hide the others
109        deleteButton.setVisible(true);
110        cancelEditButton.setVisible(true);
111        updateButton.setVisible(true);
112        exportButton.setVisible(true);
113        return pb;
114    }
115
116    /**
117     * Respond to the export button.
118     *
119     * @param e the action event
120     */
121    private void exportButtonPressed(ActionEvent e){
122        new RouteExportToLogix(_systemName.getText()).export();
123        status1.setText(Bundle.getMessage("BeanNameRoute")
124                + "\"" + _systemName.getText() + "\" " +
125                Bundle.getMessage("RouteAddStatusExported") + " ("
126                + get_includedTurnoutList().size() +
127                Bundle.getMessage("Turnouts") + ", " +
128                get_includedSensorList().size() + " " + Bundle.getMessage("Sensors") + ")");
129        finishUpdate();
130        closeFrame();
131    }
132
133    /**
134     * Respond to the CancelEdit button.
135     *
136     * @param e the action event
137     */
138    private void cancelPressed(ActionEvent e) {
139        cancelEdit();
140    }
141
142    /**
143     * Respond to the Delete button.
144     *
145     * @param e the action event
146     */
147    private void deletePressed(ActionEvent e) {
148        // route is already deactivated, just delete it
149        routeManager.deleteRoute(curRoute);
150
151        curRoute = null;
152        finishUpdate();
153        closeFrame();
154    }
155
156}