001package jmri.jmrit.throttle.panels; 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 * <hr> 023 * This file is part of JMRI. 024 * <p> 025 * JMRI is free software; you can redistribute it and/or modify it under the 026 * terms of version 2 of the GNU General Public License as published by the Free 027 * Software Foundation. See the "COPYING" file for a copy of this license. 028 * <p> 029 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 030 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 031 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 032 * 033 * @author Paul Bender Copyright (C) 2005 034 */ 035public class ControlPanelPropertyEditor extends JDialog { 036 037 private final ControlPanel control; 038 039 private JRadioButton displaySlider; // display slider from 0 to 100 040 private JRadioButton displaySliderContinuous; // display slider from -100 to 0 to 100 041 private JRadioButton displaySteps; 042 private JCheckBox trackBox; 043 private JCheckBox speedStepBoxVisibleBox; 044 private JTextField functionSwitchSlider; 045 046 private int _displaySlider; 047 048 /** 049 * Constructor. Create it and pack it. 050 * @param panel control panel. 051 */ 052 public ControlPanelPropertyEditor(ControlPanel panel) { 053 control = panel; 054 initGUI(); 055 resetProperties(); 056 } 057 058 /** 059 * Create, and place the GUI objects. 060 */ 061 private void initGUI() { 062 this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); 063 this.setTitle(Bundle.getMessage("TitleEditSpeedControlPanel")); 064 JPanel mainPanel = new JPanel(); 065 this.setContentPane(mainPanel); 066 mainPanel.setLayout(new BorderLayout()); 067 068 JPanel propertyPanel = new JPanel(); 069 propertyPanel.setLayout(new GridBagLayout()); 070 GridBagConstraints constraints = new GridBagConstraints(); 071 constraints.anchor = GridBagConstraints.WEST; 072 constraints.fill = GridBagConstraints.HORIZONTAL; 073 constraints.gridheight = 1; 074 constraints.gridwidth = 1; 075 constraints.ipadx = 0; 076 constraints.ipady = 0; 077 Insets insets = new Insets(2, 2, 2, 2); 078 constraints.insets = insets; 079 constraints.weightx = 1; 080 constraints.weighty = 1; 081 constraints.gridx = 0; 082 constraints.gridy = 0; 083 084 ButtonGroup modeSelectionButtons = new ButtonGroup(); 085 086 displaySlider = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSlider")); 087 displaySliderContinuous = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSliderContinuous")); 088 displaySteps = new JRadioButton(Bundle.getMessage("ButtonDisplaySpeedSteps")); 089 090 modeSelectionButtons.add(displaySlider); 091 modeSelectionButtons.add(displaySteps); 092 modeSelectionButtons.add(displaySliderContinuous); 093 094 constraints.anchor = GridBagConstraints.CENTER; 095 constraints.gridy = 1; 096 propertyPanel.add(displaySlider, constraints); 097 098 constraints.gridy = 2; 099 propertyPanel.add(displaySteps, constraints); 100 101 constraints.gridy = 3; 102 propertyPanel.add(displaySliderContinuous, constraints); 103 104 trackBox = new JCheckBox(Bundle.getMessage("CheckBoxTrackSliderInRealTime")); 105 constraints.gridy = 4; 106 propertyPanel.add(trackBox, constraints); 107 108 speedStepBoxVisibleBox = new JCheckBox(Bundle.getMessage("CheckBoxHideSpeedStepSelector")); 109 constraints.gridy = 5; 110 propertyPanel.add(speedStepBoxVisibleBox, constraints); 111 112 JLabel functionSwitchLabel = new JLabel(Bundle.getMessage("SwitchSliderOnFunction")); 113 functionSwitchSlider = new JTextField(4); 114 constraints.gridy = 6; 115 constraints.gridx = 0; 116 propertyPanel.add(functionSwitchLabel, constraints); 117 constraints.gridx = 1; 118 propertyPanel.add(functionSwitchSlider, constraints); 119 120 displaySlider.addActionListener((ActionEvent e) -> { 121 displaySlider.setSelected(true); 122 displaySteps.setSelected(false); 123 displaySliderContinuous.setSelected(false); 124 _displaySlider = ControlPanel.SLIDERDISPLAY; 125 }); 126 127 displaySteps.addActionListener((ActionEvent e) -> { 128 displaySlider.setSelected(false); 129 displaySteps.setSelected(true); 130 displaySliderContinuous.setSelected(false); 131 _displaySlider = ControlPanel.STEPDISPLAY; 132 }); 133 134 displaySliderContinuous.addActionListener((ActionEvent e) -> { 135 displaySlider.setSelected(false); 136 displaySteps.setSelected(false); 137 displaySliderContinuous.setSelected(true); 138 _displaySlider = ControlPanel.SLIDERDISPLAYCONTINUOUS; 139 }); 140 141 JPanel buttonPanel = new JPanel(); 142 buttonPanel.setLayout(new GridLayout(1, 2, 4, 4)); 143 144 JButton applyButton = new JButton(Bundle.getMessage("ButtonApply")); 145 applyButton.addActionListener((ActionEvent e) -> { 146 saveProperties(); 147 }); 148 149 JButton resetButton = new JButton(Bundle.getMessage("ButtonReset")); 150 resetButton.addActionListener((ActionEvent e) -> { 151 resetProperties(); 152 }); 153 154 JButton closeButton = new JButton(Bundle.getMessage("ButtonClose")); 155 closeButton.addActionListener((ActionEvent e) -> { 156 finishEdit(); 157 }); 158 159 buttonPanel.add(resetButton); 160 buttonPanel.add(closeButton); 161 buttonPanel.add(applyButton); 162 163 mainPanel.add(propertyPanel, BorderLayout.CENTER); 164 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 165 166 pack(); 167 } 168 169 /** 170 * Save the user-modified properties back to the FunctionButton. 171 */ 172 private void saveProperties() { 173 if (isDataValid()) { 174 control.setTrackSlider(trackBox.isSelected()); 175 control.setSwitchSliderFunction(functionSwitchSlider.getText()); 176 control.setSpeedController(_displaySlider); 177 control.setHideSpeedStep(speedStepBoxVisibleBox.isSelected()); 178 } 179 } 180 181 /** 182 * Finish the editing process. Hide the dialog. 183 */ 184 private void finishEdit() { 185 this.setVisible(false); 186 } 187 188 /** 189 * Update values from the controlPanel 190 */ 191 public void resetProperties() { 192 // type of slider 193 _displaySlider = control.getDisplaySlider(); 194 displaySlider.setSelected(_displaySlider == ControlPanel.SLIDERDISPLAY); 195 displaySteps.setSelected(_displaySlider == ControlPanel.STEPDISPLAY); 196 displaySliderContinuous.setSelected(_displaySlider == ControlPanel.SLIDERDISPLAYCONTINUOUS); 197 // disable the speed controls if the control panel says they aren't possible 198 displaySlider.setEnabled(control.isSpeedControllerAvailable(ControlPanel.SLIDERDISPLAY)); 199 displaySteps.setEnabled(control.isSpeedControllerAvailable(ControlPanel.STEPDISPLAY)); 200 displaySliderContinuous.setEnabled(control.isSpeedControllerAvailable(ControlPanel.SLIDERDISPLAYCONTINUOUS)); 201 // other propertiess 202 trackBox.setSelected(control.getTrackSlider()); 203 speedStepBoxVisibleBox.setSelected(control.getHideSpeedStep() ); 204 functionSwitchSlider.setText(control.getSwitchSliderFunction()); 205 } 206 207 /** 208 * Verify the data on the dialog. If invalid, notify user of errors. This 209 * only needs to do something if we add something other than speed control 210 * selection to this panel. 211 */ 212 private boolean isDataValid() { 213 return true; 214 } 215 216 // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ControlPanelPropertyEditor.class); 217}