001package jmri.jmrit.beantable.turnout;
002
003
004import javax.annotation.Nonnull;
005import javax.swing.*;
006
007import jmri.Turnout;
008import jmri.TurnoutOperation;
009import static jmri.jmrit.beantable.turnout.TurnoutTableDataModel.editingOps;
010import jmri.jmrit.turnoutoperations.TurnoutOperationConfig;
011import jmri.util.swing.JmriJOptionPane;
012
013/**
014 * Display a TurnoutOperationConfig Dialog for the turnout.
015 * 
016 * Code originally within TurnoutTableAction.
017 * 
018 * @author Bob Jacobsen Copyright (C) 2003, 2004, 2007
019 * @author Egbert Broerse Copyright (C) 2017
020 * @author Steve Young Copyright (C) 2021
021 */
022public class TurnoutOperationEditorDialog extends JDialog {
023
024    private TurnoutOperation myOp;
025    final Turnout myTurnout;
026    final TurnoutOperationEditorDialog self;
027
028    /**
029     * Pop up a TurnoutOperationConfig Dialog for the turnout.
030     *
031     * @param op TunoutOperation to edit, not null.
032     * @param t  The turnout.
033     * @param window  Parent Window for the Dialog, can be null.
034     */
035    TurnoutOperationEditorDialog( @Nonnull TurnoutOperation op, @Nonnull Turnout t,
036            @javax.annotation.CheckForNull java.awt.Window window) {
037        super(window);
038        self = this;
039        myOp = op;
040        myTurnout = t;
041        init();
042    }
043
044    private void init() {
045
046        myOp.addPropertyChangeListener(evt -> {
047            if (evt.getPropertyName().equals("Deleted")) {
048                setVisible(false);
049            }
050        });
051
052        TurnoutOperationConfig config = TurnoutOperationConfig.getConfigPanel(myOp);
053        setTitle();
054        log.debug("TurnoutOpsEditDialog title set");
055        if (config != null) {
056            log.debug("OpsEditDialog opening");
057            Box outerBox = Box.createVerticalBox();
058            outerBox.add(config);
059            Box buttonBox = Box.createHorizontalBox();
060            JButton nameButton = new JButton(Bundle.getMessage("NameSetting"));
061            nameButton.addActionListener(e -> {
062                String newName = JmriJOptionPane.showInputDialog(self, Bundle.getMessage("NameParameterSetting"),"");
063                if (newName != null && !newName.isEmpty()) {
064                    if (!myOp.rename(newName)) {
065                        JmriJOptionPane.showMessageDialog(self, Bundle.getMessage("TurnoutErrorDuplicate"),
066                                Bundle.getMessage("WarningTitle"), JmriJOptionPane.ERROR_MESSAGE);
067                    }
068                    setTitle();
069                    myTurnout.setTurnoutOperation(null);
070                    myTurnout.setTurnoutOperation(myOp); // no-op but updates display - have to <i>change</i> value
071                }
072            });
073            JButton okButton = new JButton(Bundle.getMessage("ButtonOK"));
074            okButton.addActionListener(e -> {
075                config.endConfigure();
076                if (myOp.isNonce() && myOp.equivalentTo(myOp.getDefinitive())) {
077                    myTurnout.setTurnoutOperation(null);
078                    myOp.dispose();
079                    myOp = null;
080                }
081                self.setVisible(false);
082                editingOps.set(false);
083            });
084            JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel"));
085            cancelButton.addActionListener(e -> {
086                self.setVisible(false);
087                editingOps.set(false);
088            });
089            buttonBox.add(Box.createHorizontalGlue());
090            if (!myOp.isDefinitive()) {
091                buttonBox.add(nameButton);
092            }
093            buttonBox.add(okButton);
094            buttonBox.add(cancelButton);
095            outerBox.add(buttonBox);
096            getContentPane().add(outerBox);
097            this.addWindowListener(new java.awt.event.WindowAdapter() {
098                @Override
099                public void windowClosing(java.awt.event.WindowEvent e) {
100                    editingOps.set(false);
101                }
102            });
103            
104        } else {
105            log.error("Error opening Turnout automation edit pane");
106        }
107        pack();
108    }
109
110    private void setTitle() {
111        String title = Bundle.getMessage("TurnoutOperationTitle") + " \"" + myOp.getName() + "\"";
112        if (myOp.isNonce()) {
113            title = Bundle.getMessage("TurnoutOperationForTurnout") + " " + myTurnout.getSystemName();
114        }
115        setTitle(title);
116    }
117
118    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TurnoutOperationEditorDialog.class);
119
120}
121