001package jmri.jmrit.display.layoutEditor.LayoutEditorDialogs;
002
003import java.awt.*;
004
005import javax.annotation.Nonnull;
006import javax.swing.*;
007import javax.swing.border.TitledBorder;
008
009import jmri.BlockManager;
010import jmri.jmrit.display.layoutEditor.LayoutEditor;
011import jmri.jmrit.display.layoutEditor.LayoutTrackView;
012
013/**
014 * MVC Editor component for LayoutXOver objects.
015 *
016 * @author Bob Jacobsen  Copyright (c) 2020
017 * 
018 */
019public class LayoutXOverEditor extends LayoutTurnoutEditor {
020
021    /**
022     * constructor method.
023     * @param layoutEditor main layout editor.
024     */
025    public LayoutXOverEditor(@Nonnull LayoutEditor layoutEditor) {
026        super(layoutEditor);
027    }
028
029    /**
030     * Edit a XOver
031     */
032    @Override
033    public void editLayoutTrack(@Nonnull LayoutTrackView layoutTrackView) {
034        log.trace("LayoutXOverEditor.editLayoutTrack({}) of a {}", layoutTrackView, layoutTrackView.getClass());
035        super.editLayoutTrack(layoutTrackView);
036    }
037    
038
039    // not used by crossover
040    @Override
041    protected void extendAddContinuingStateChoice(Container contentPane) {
042    }
043
044    @Override
045    protected void extendBlockBCDSetup(Container contentPane) {
046        JPanel panel21 = new JPanel();
047        panel21.setLayout(new FlowLayout());
048        TitledBorder borderblk2 = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black));
049        borderblk2.setTitle(Bundle.getMessage("BeanNameBlock") + " 2");  // NOI18N
050        panel21.setBorder(borderblk2);
051        LayoutEditor.setupComboBox(editLayoutTurnoutBlockBNameComboBox, false, true, true);
052        editLayoutTurnoutBlockBNameComboBox.setToolTipText(Bundle.getMessage("EditBlockBNameHint"));  // NOI18N
053        panel21.add(editLayoutTurnoutBlockBNameComboBox);
054
055        panel21.add(editLayoutTurnoutBlockBButton = new JButton(Bundle.getMessage("CreateEdit")));  // NOI18N
056        editLayoutTurnoutBlockBButton.addActionListener(this::editLayoutTurnoutEditBlockBPressed);
057        editLayoutTurnoutBlockBButton.setToolTipText(Bundle.getMessage("EditBlockHint", "2"));  // NOI18N
058        contentPane.add(panel21);
059
060        JPanel panel22 = new JPanel();
061        panel22.setLayout(new FlowLayout());
062        TitledBorder borderblk3 = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black));
063        borderblk3.setTitle(Bundle.getMessage("BeanNameBlock") + " 3");  // NOI18N
064        panel22.setBorder(borderblk3);
065        LayoutEditor.setupComboBox(editLayoutTurnoutBlockCNameComboBox, false, true, true);
066        editLayoutTurnoutBlockCNameComboBox.setToolTipText(Bundle.getMessage("EditBlockCNameHint"));  // NOI18N
067        panel22.add(editLayoutTurnoutBlockCNameComboBox);
068        panel22.add(editLayoutTurnoutBlockCButton = new JButton(Bundle.getMessage("CreateEdit")));  // NOI18N
069        editLayoutTurnoutBlockCButton.addActionListener(this::editLayoutTurnoutEditBlockCPressed);
070        editLayoutTurnoutBlockCButton.setToolTipText(Bundle.getMessage("EditBlockHint", "3"));  // NOI18N
071        contentPane.add(panel22);
072
073        JPanel panel23 = new JPanel();
074        panel23.setLayout(new FlowLayout());
075        TitledBorder borderblk4 = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black));
076        borderblk4.setTitle(Bundle.getMessage("BeanNameBlock") + " 4");  // NOI18N
077        panel23.setBorder(borderblk4);
078        LayoutEditor.setupComboBox(editLayoutTurnoutBlockDNameComboBox, false, true, true);
079        editLayoutTurnoutBlockDNameComboBox.setToolTipText(Bundle.getMessage("EditBlockDNameHint"));  // NOI18N
080        panel23.add(editLayoutTurnoutBlockDNameComboBox);
081        panel23.add(editLayoutTurnoutBlockDButton = new JButton(Bundle.getMessage("CreateEdit")));  // NOI18N
082        editLayoutTurnoutBlockDButton.addActionListener(this::editLayoutTurnoutEditBlockDPressed);
083        editLayoutTurnoutBlockDButton.setToolTipText(Bundle.getMessage("EditBlockHint", "4"));  // NOI18N
084        contentPane.add(panel23);
085    }
086    
087    @Override
088    protected void configureCheckBoxes(BlockManager bm) {
089        editLayoutTurnoutBlockBNameComboBox.getEditor().setItem(bm.getBlock(layoutTurnout.getBlockBName()));
090        editLayoutTurnoutBlockCNameComboBox.getEditor().setItem(bm.getBlock(layoutTurnout.getBlockCName()));
091        editLayoutTurnoutBlockDNameComboBox.getEditor().setItem(bm.getBlock(layoutTurnout.getBlockDName()));
092        editLayoutTurnoutBlockBNameComboBox.setEnabled(!hasNxSensorPairs(layoutTurnout.getLayoutBlockB()));
093        editLayoutTurnoutBlockCNameComboBox.setEnabled(!hasNxSensorPairs(layoutTurnout.getLayoutBlockC()));
094        editLayoutTurnoutBlockDNameComboBox.setEnabled(!hasNxSensorPairs(layoutTurnout.getLayoutBlockD()));
095    }
096
097    // Set up for Edit
098    @Override
099    protected void setUpForEdit() {
100            editLayoutTurnoutFrame.setTitle(Bundle.getMessage("EditXover"));
101            editLayoutTurnoutHiddenCheckBox.setText(Bundle.getMessage("HideXover"));
102    }
103
104    @Override
105    protected void setUpContinuingSense() {}
106
107    @Override
108    protected void donePressedSecondTurnoutName(String newName) {
109        // turnout has changed
110        if (layoutEditor.validatePhysicalTurnout(
111                newName, editLayoutTurnoutFrame)) {
112            layoutTurnout.setSecondTurnout(newName);
113        } else {
114            editLayoutTurnout2ndTurnoutCheckBox.setSelected(false);
115            layoutTurnout.setSecondTurnout("");
116            editLayoutTurnout2ndTurnoutComboBox.setSelectedItem(null);
117        }
118        editLayoutTurnoutNeedRedraw = true;
119    }
120
121    // set the continuing route Turnout State
122    @Override
123    protected void setContinuingRouteTurnoutState() {
124        // this had content in LayoutTurnoutEditor superclass, which we don't want to do here.
125    }
126
127    @Override
128     protected void checkBlock234Changed() {
129        String newName;
130        // check if Block 2 changed
131        newName = editLayoutTurnoutBlockBNameComboBox.getSelectedItemDisplayName();
132        if (newName == null) {
133            newName = "";
134        }
135        if (!layoutTurnout.getBlockBName().equals(newName)) {
136            // get new block, or null if block has been removed
137            layoutTurnoutView.setLayoutBlockB(layoutEditor.provideLayoutBlock(newName));
138            editLayoutTurnoutNeedRedraw = true;
139            editLayoutTurnoutNeedsBlockUpdate = true;
140        }
141        // check if Block 3 changed
142        newName = editLayoutTurnoutBlockCNameComboBox.getSelectedItemDisplayName();
143        if (newName == null) {
144            newName = "";
145        }
146        if (!layoutTurnout.getBlockCName().equals(newName)) {
147            // get new block, or null if block has been removed
148            layoutTurnoutView.setLayoutBlockC(layoutEditor.provideLayoutBlock(newName));
149            editLayoutTurnoutNeedRedraw = true;
150            editLayoutTurnoutNeedsBlockUpdate = true;
151        }
152        // check if Block 4 changed
153        newName = editLayoutTurnoutBlockDNameComboBox.getSelectedItemDisplayName();
154        if (newName == null) {
155            newName = "";
156        }
157        if (!layoutTurnout.getBlockDName().equals(newName)) {
158            // get new block, or null if block has been removed
159            layoutTurnoutView.setLayoutBlockD(layoutEditor.provideLayoutBlock(newName));
160            editLayoutTurnoutNeedRedraw = true;
161            editLayoutTurnoutNeedsBlockUpdate = true;
162        }
163    }
164
165    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LayoutXOverEditor.class);
166}