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;
009import java.awt.event.ActionListener;
010
011import javax.swing.JButton;
012import javax.swing.JDialog;
013import javax.swing.JLabel;
014import javax.swing.JList;
015import javax.swing.JPanel;
016import javax.swing.JTextField;
017import javax.swing.ListSelectionModel;
018
019import jmri.util.swing.JmriJOptionPane;
020
021/**
022 * A very specific dialog for editing the properties of a ThrottleFrame object.
023 *
024 * @author Original Unknown
025 * @author Ken Cameron, copyright 2008
026 */
027public class ThrottleFramePropertyEditor extends JDialog {
028
029    private ThrottleWindow frame;
030
031    private JTextField titleField;
032
033    private JList<String> titleType;
034
035    private String[] titleTextTypes = {"address", "text", "textAddress", "addressText", "rosterID"};
036    private String[] titleTextTypeNames = {
037        Bundle.getMessage("SelectTitleTypeADDRESS"),
038        Bundle.getMessage("SelectTitleTypeTEXT"),
039        Bundle.getMessage("SelectTitleTypeTEXTADDRESS"),
040        Bundle.getMessage("SelectTitleTypeADDRESSTEXT"),
041        Bundle.getMessage("SelectTitleTypeROSTERID")
042    };
043
044    /*
045     * Constructor
046     */
047    public ThrottleFramePropertyEditor(ThrottleWindow w){
048        setThrottleFrame(w);
049        setLocation(w.getLocationOnScreen());
050        setLocationRelativeTo(w);
051    }
052
053    /**
054     * Create, initialize, and place the GUI objects.
055     */
056    private void initGUI() {
057        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
058        this.setTitle(Bundle.getMessage("EditThrottleFrameTitle"));
059        JPanel mainPanel = new JPanel();
060        this.setContentPane(mainPanel);
061        mainPanel.setLayout(new BorderLayout());
062
063        JPanel propertyPanel = new JPanel();
064        propertyPanel.setLayout(new GridBagLayout());
065        GridBagConstraints constraints = new GridBagConstraints();
066        constraints.anchor = GridBagConstraints.WEST;
067        constraints.fill = GridBagConstraints.HORIZONTAL;
068        constraints.gridheight = 1;
069        constraints.gridwidth = 1;
070        constraints.ipadx = 0;
071        constraints.ipady = 0;
072        Insets insets = new Insets(2, 2, 2, 2);
073        constraints.insets = insets;
074        constraints.weightx = 1;
075        constraints.weighty = 1;
076        constraints.gridx = 0;
077        constraints.gridy = 0;
078
079        titleField = new JTextField();
080        titleField.setColumns(24);
081        titleField.addActionListener(new ActionListener() {
082            @Override
083            public void actionPerformed(ActionEvent e) {
084                titleFieldChanged();
085            }
086        });
087
088        propertyPanel.add(new JLabel(Bundle.getMessage("FrameTitlePrompt")), constraints);
089
090        constraints.anchor = GridBagConstraints.CENTER;
091        constraints.gridx++;
092        propertyPanel.add(titleField, constraints);
093
094        titleType = new JList<String>(titleTextTypeNames);
095        titleType.setVisibleRowCount(titleTextTypeNames.length);
096        titleType.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
097        for (int i = 0; i < titleTextTypes.length; i++) {
098            if (titleTextTypes[i].compareTo(frame.getTitleTextType()) == 0) {
099                titleType.setSelectedIndex(i);
100                break;
101            }
102        }
103        constraints.gridy++;
104        constraints.gridx = 0;
105        propertyPanel.add(new JLabel(Bundle.getMessage("SelectTitleTypePrompt")), constraints);
106        constraints.gridx++;
107        propertyPanel.add(titleType, constraints);
108
109
110        JPanel buttonPanel = new JPanel();
111        buttonPanel.setLayout(new GridLayout(1, 2, 4, 4));
112
113        JButton saveButton = new JButton(Bundle.getMessage("ButtonOK"));
114        saveButton.addActionListener(new ActionListener() {
115            @Override
116            public void actionPerformed(ActionEvent e) {
117                saveProperties();
118            }
119        });
120
121        JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel"));
122        cancelButton.addActionListener(new ActionListener() {
123            @Override
124            public void actionPerformed(ActionEvent e) {
125                finishEdit();
126            }
127        });
128
129        buttonPanel.add(saveButton);
130        buttonPanel.add(cancelButton);
131
132        mainPanel.add(propertyPanel, BorderLayout.CENTER);
133        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
134    }
135
136    /**
137     * Set the ThrottleFrame used here. Does some initialization of the Frame.
138     */
139    private void setThrottleFrame(ThrottleWindow f) {
140        this.frame = f;
141        initGUI();
142        pack();
143        titleField.setText(frame.getTitleText());
144        titleField.selectAll();
145    }
146
147    /**
148     * TItle field has been changed. If it has text, make sure that's displayed.
149     */
150    protected void titleFieldChanged() {
151        if (titleField.getText().equals("")) {
152            return;
153        }
154        if (titleType.getSelectedValue().equals(Bundle.getMessage("SelectTitleTypeADDRESS"))) {
155            titleType.setSelectedValue(Bundle.getMessage("SelectTitleTypeTEXT"), true);
156        }
157    }
158
159    /**
160     * Save the user-modified properties back to the ThrottleFrame.
161     */
162    private void saveProperties() {
163        if (isDataValid()) {
164            frame.setTitleText(titleField.getText());
165            frame.setTitleTextType(titleTextTypes[titleType.getSelectedIndex()]);
166            frame.getCurrentThrottleFrame().setFrameTitle();
167            finishEdit();
168        }
169    }
170
171    /**
172     * Finish the editing process. Hide the dialog.
173     */
174    private void finishEdit() {
175        this.setVisible(false);
176    }
177
178    /**
179     * Verify the data on the dialog. If invalid, notify user of errors.
180     */
181    private boolean isDataValid() {
182        StringBuffer errors = new StringBuffer();
183        int errorNumber = 0;
184
185        if (errorNumber > 0) {
186            JmriJOptionPane.showMessageDialog(this, errors,
187                    Bundle.getMessage("ErrorOnPage"), JmriJOptionPane.ERROR_MESSAGE);
188            return false;
189        }
190        return true;
191    }
192
193}