001package jmri.jmrit.turnoutoperations;
002
003import java.awt.Dimension;
004import javax.swing.Box;
005import javax.swing.JLabel;
006import javax.swing.JSpinner;
007import javax.swing.SpinnerNumberModel;
008import jmri.CommonTurnoutOperation;
009import jmri.TurnoutOperation;
010
011/**
012 * Extension of TurnoutOperationConfig to handle config for common aspects of
013 * some subclasses.
014 *
015 * @author John Harper Copyright 2005
016 */
017public class CommonTurnoutOperationConfig extends TurnoutOperationConfig {
018    JSpinner intervalSpinner;
019    JSpinner maxTriesSpinner;
020    CommonTurnoutOperation myOp;
021
022    /**
023     * Create the config JPanel, if there is one, to configure this operation
024     * type.
025     * @param op turnout operation.
026     */
027    public CommonTurnoutOperationConfig(TurnoutOperation op) {
028        super(op);
029        myOp = (CommonTurnoutOperation) op;
030
031        maxTriesSpinner = new JSpinner();
032        intervalSpinner = new JSpinner();
033        Box vbox = Box.createVerticalBox();
034        Box headerBox = Box.createHorizontalBox();
035        Box hbox1 = Box.createHorizontalBox();
036        Box hbox2 = Box.createHorizontalBox();
037
038        // Show Operator Tooltip in header
039        headerBox.add(new JLabel(myOp.getToolTip()));
040        headerBox.setMinimumSize(new Dimension(new JLabel(myOp.getToolTip()).getMinimumSize()));
041
042        vbox.add(headerBox);
043        vbox.add(Box.createVerticalGlue());
044
045        vbox.add(hbox2); //Show TimesToTry first, keeping to the order of the help text at right
046        vbox.add(hbox1); //Show Interval next
047        vbox.add(Box.createVerticalGlue());
048        hbox1.add(new JLabel(Bundle.getMessage("Interval")));
049        hbox1.add(Box.createHorizontalGlue());
050        intervalSpinner.setMinimumSize(new Dimension(100, 20));
051
052        intervalSpinner.setModel(
053                new SpinnerNumberModel(myOp.getInterval(),
054                        CommonTurnoutOperation.minInterval, CommonTurnoutOperation.maxInterval,
055                        CommonTurnoutOperation.intervalStepSize)); // val, min, max, step
056
057        hbox1.add(intervalSpinner);
058        hbox2.add(new JLabel(Bundle.getMessage("TimesToTry")));
059        hbox2.add(Box.createHorizontalGlue());
060        maxTriesSpinner.setMinimumSize(new Dimension(100, 20));
061
062        maxTriesSpinner.setModel(
063                new SpinnerNumberModel(myOp.getMaxTries(),
064                        CommonTurnoutOperation.minMaxTries, CommonTurnoutOperation.maxMaxTries, 1)); // val, min, max, step
065
066        hbox2.add(maxTriesSpinner);
067
068        Box hbox3 = Box.createHorizontalBox();
069        hbox3.add(Box.createHorizontalStrut(150));
070        vbox.add(hbox3);
071        add(vbox);
072    }
073
074    /**
075     * Called when OK button pressed in config panel, to retrieve and set new
076     * values.
077     */
078    @Override
079    public void endConfigure() {
080        int newInterval = ((Integer) intervalSpinner.getValue());
081        myOp.setInterval(newInterval);
082        int newMaxTries = ((Integer) maxTriesSpinner.getValue());
083        myOp.setMaxTries(newMaxTries);
084    }
085
086}