001package jmri.jmrit.throttle;
002
003import java.awt.BorderLayout;
004import java.awt.GridBagConstraints;
005import java.awt.GridBagLayout;
006import java.awt.GridLayout;
007import java.awt.Insets;
008import java.awt.event.ActionEvent;
009
010import javax.swing.ButtonGroup;
011import javax.swing.JButton;
012import javax.swing.JCheckBox;
013import javax.swing.JDialog;
014import javax.swing.JLabel;
015import javax.swing.JPanel;
016import javax.swing.JRadioButton;
017import javax.swing.JTextField;
018
019/**
020 * A very specific dialog for editing the properties of a ControlPanel object.
021 *
022 * @author Paul Bender Copyright (C) 2005
023 */
024public class ControlPanelPropertyEditor extends JDialog {
025
026    private final ControlPanel control;
027
028    private JRadioButton displaySlider; // display slider from 0 to 100
029    private JRadioButton displaySliderContinuous; // display slider from -100 to 0 to 100
030    private JRadioButton displaySteps;
031    private JCheckBox trackBox;
032    private JCheckBox speedStepBoxVisibleBox;
033    private JTextField functionSwitchSlider;
034
035    private int _displaySlider;
036
037    /**
038     * Constructor. Create it and pack it.
039     * @param panel control panel.
040     */
041    public ControlPanelPropertyEditor(ControlPanel panel) {
042        control = panel;
043        initGUI();
044        resetProperties();        
045    }
046
047    /**
048     * Create, and place the GUI objects.
049     */
050    private void initGUI() {
051        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
052        this.setTitle(Bundle.getMessage("TitleEditSpeedControlPanel"));
053        JPanel mainPanel = new JPanel();
054        this.setContentPane(mainPanel);
055        mainPanel.setLayout(new BorderLayout());
056
057        JPanel propertyPanel = new JPanel();
058        propertyPanel.setLayout(new GridBagLayout());
059        GridBagConstraints constraints = new GridBagConstraints();
060        constraints.anchor = GridBagConstraints.WEST;
061        constraints.fill = GridBagConstraints.HORIZONTAL;
062        constraints.gridheight = 1;
063        constraints.gridwidth = 1;
064        constraints.ipadx = 0;
065        constraints.ipady = 0;
066        Insets insets = new Insets(2, 2, 2, 2);
067        constraints.insets = insets;
068        constraints.weightx = 1;
069        constraints.weighty = 1;
070        constraints.gridx = 0;
071        constraints.gridy = 0;
072
073        ButtonGroup modeSelectionButtons = new ButtonGroup();
074
075        displaySlider = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSlider"));
076        displaySliderContinuous = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSliderContinuous"));
077        displaySteps = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSteps"));
078
079        modeSelectionButtons.add(displaySlider);
080        modeSelectionButtons.add(displaySteps);
081        modeSelectionButtons.add(displaySliderContinuous);
082
083        constraints.anchor = GridBagConstraints.CENTER;
084        constraints.gridy = 1;
085        propertyPanel.add(displaySlider, constraints);
086
087        constraints.gridy = 2;
088        propertyPanel.add(displaySteps, constraints);
089
090        constraints.gridy = 3;
091        propertyPanel.add(displaySliderContinuous, constraints);
092
093        trackBox = new JCheckBox(Bundle.getMessage("CheckBoxTrackSliderInRealTime"));
094        constraints.gridy = 4;
095        propertyPanel.add(trackBox, constraints);
096        
097        speedStepBoxVisibleBox = new JCheckBox(Bundle.getMessage("CheckBoxHideSpeedStepSelector"));
098        constraints.gridy = 5;
099        propertyPanel.add(speedStepBoxVisibleBox, constraints);                
100
101        JLabel functionSwitchLabel = new JLabel(Bundle.getMessage("SwitchSliderOnFunction"));
102        functionSwitchSlider = new JTextField(4);
103        constraints.gridy = 6;
104        constraints.gridx = 0;
105        propertyPanel.add(functionSwitchLabel, constraints);
106        constraints.gridx = 1;
107        propertyPanel.add(functionSwitchSlider, constraints);
108
109        displaySlider.addActionListener((ActionEvent e) -> {
110            displaySlider.setSelected(true);
111            displaySteps.setSelected(false);
112            displaySliderContinuous.setSelected(false);
113            _displaySlider = ControlPanel.SLIDERDISPLAY;
114        });
115
116        displaySteps.addActionListener((ActionEvent e) -> {
117            displaySlider.setSelected(false);
118            displaySteps.setSelected(true);
119            displaySliderContinuous.setSelected(false);
120            _displaySlider = ControlPanel.STEPDISPLAY;
121        });
122
123        displaySliderContinuous.addActionListener((ActionEvent e) -> {
124            displaySlider.setSelected(false);
125            displaySteps.setSelected(false);
126            displaySliderContinuous.setSelected(true);
127            _displaySlider = ControlPanel.SLIDERDISPLAYCONTINUOUS;
128        });
129        
130        JPanel buttonPanel = new JPanel();
131        buttonPanel.setLayout(new GridLayout(1, 2, 4, 4));
132
133        JButton applyButton = new JButton(Bundle.getMessage("ButtonApply"));
134        applyButton.addActionListener((ActionEvent e) -> {
135            saveProperties();
136        });
137                
138        JButton resetButton = new JButton(Bundle.getMessage("ButtonReset"));
139        resetButton.addActionListener((ActionEvent e) -> {
140            resetProperties();           
141        });        
142        
143        JButton closeButton = new JButton(Bundle.getMessage("ButtonClose"));
144        closeButton.addActionListener((ActionEvent e) -> {
145            finishEdit();
146        });
147
148        buttonPanel.add(resetButton);
149        buttonPanel.add(closeButton);
150        buttonPanel.add(applyButton);
151        
152        mainPanel.add(propertyPanel, BorderLayout.CENTER);
153        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
154        
155        pack();        
156    }
157
158    /**
159     * Save the user-modified properties back to the FunctionButton.
160     */
161    private void saveProperties() {
162        if (isDataValid()) {
163            control.setTrackSlider(trackBox.isSelected());
164            control.setSwitchSliderFunction(functionSwitchSlider.getText());
165            control.setSpeedController(_displaySlider);
166            control.setHideSpeedStep(speedStepBoxVisibleBox.isSelected());
167        }
168    }
169
170    /**
171     * Finish the editing process. Hide the dialog.
172     */
173    private void finishEdit() {
174        this.setVisible(false);
175    }
176
177    /**
178     * Update values from the controlPanel
179     */
180    public void resetProperties() {
181        // type of slider
182        _displaySlider = control.getDisplaySlider();        
183        displaySlider.setSelected(_displaySlider == ControlPanel.SLIDERDISPLAY);
184        displaySteps.setSelected(_displaySlider == ControlPanel.STEPDISPLAY);
185        displaySliderContinuous.setSelected(_displaySlider == ControlPanel.SLIDERDISPLAYCONTINUOUS);
186        // disable the speed controls if the control panel says they aren't possible
187        displaySlider.setEnabled(control.isSpeedControllerAvailable(ControlPanel.SLIDERDISPLAY));
188        displaySteps.setEnabled(control.isSpeedControllerAvailable(ControlPanel.STEPDISPLAY));
189        displaySliderContinuous.setEnabled(control.isSpeedControllerAvailable(ControlPanel.SLIDERDISPLAYCONTINUOUS));
190        // other propertiess
191        trackBox.setSelected(control.getTrackSlider());
192        speedStepBoxVisibleBox.setSelected(control.getHideSpeedStep() );
193        functionSwitchSlider.setText(control.getSwitchSliderFunction());
194    }
195        
196    /**
197     * Verify the data on the dialog. If invalid, notify user of errors. This
198     * only needs to do something if we add something other than speed control
199     * selection to this panel.
200     */
201    private boolean isDataValid() {
202        return true;
203    }
204
205    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ControlPanelPropertyEditor.class);    
206}