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