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