001package jmri.jmrit.operations.locations.divisions;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.OperationsFrame;
010import jmri.jmrit.operations.OperationsXml;
011import jmri.jmrit.operations.setup.Control;
012import jmri.jmrit.operations.setup.Setup;
013import jmri.util.swing.JmriJOptionPane;
014
015/**
016 * Frame for user edit of a division
017 *
018 * @author Dan Boudreau Copyright (C) 2021
019 */
020public class DivisionEditFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
021
022    protected Division _division;
023    DivisionManager divisionManager;
024
025    // major buttons
026    JButton saveDivisionButton = new JButton(Bundle.getMessage("SaveDivision"));
027    JButton deleteDivisionButton = new JButton(Bundle.getMessage("DeleteDivision"));
028    JButton addDivisionButton = new JButton(Bundle.getMessage("AddDivision"));
029
030    // text field
031    JTextField divisionNameTextField = new JTextField(20);
032    JTextField commentTextField = new JTextField(35);
033
034    public static final int MAX_NAME_LENGTH = Control.max_len_string_location_name;
035    public static final String NAME = Bundle.getMessage("Name");
036    public static final String DISPOSE = "dispose"; // NOI18N
037
038    public DivisionEditFrame(Division division) {
039        super(Bundle.getMessage("AddDivision"));
040
041        _division = division;
042        // load manager
043        divisionManager = InstanceManager.getDefault(DivisionManager.class);
044        initPanel();
045    }
046        
047    private void initPanel() {
048
049        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
050
051        divisionNameTextField.setName("DivisionNameField"); // NOI18N for UI Test
052        commentTextField.setName("CommentField"); // NOI18N for UI Test
053
054        // Layout the panel by rows
055        JPanel p1 = new JPanel();
056        p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
057
058        JScrollPane p1Pane = new JScrollPane(p1);
059        p1Pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
060        p1Pane.setMinimumSize(new Dimension(300, 3 * divisionNameTextField.getPreferredSize().height));
061        p1Pane.setMaximumSize(new Dimension(2000, 200));
062
063        // row 1a name
064        JPanel pName = new JPanel();
065        pName.setLayout(new GridBagLayout());
066        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Name")));
067        addItem(pName, divisionNameTextField, 0, 0);
068
069        // row 1b comment
070        JPanel pC = new JPanel();
071        pC.setLayout(new GridBagLayout());
072        pC.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Comment")));
073        addItem(pC, commentTextField, 0, 0);
074
075        p1.add(pName);
076        p1.add(pC);
077
078        // row 11 buttons
079        JPanel pB = new JPanel();
080        pB.setLayout(new GridBagLayout());
081        pB.setBorder(BorderFactory.createTitledBorder(""));
082
083        // row 13
084        addItem(pB, deleteDivisionButton, 0, 0);
085        addItem(pB, addDivisionButton, 1, 0);
086        addItem(pB, saveDivisionButton, 3, 0);
087
088        getContentPane().add(p1Pane);
089        getContentPane().add(pB);
090
091        // set up buttons
092        addButtonAction(deleteDivisionButton);
093        addButtonAction(addDivisionButton);
094        addButtonAction(saveDivisionButton);
095
096        enableButtons(_division != null);
097
098        if (_division != null) {
099            setTitle(Bundle.getMessage("EditDivision"));
100            divisionNameTextField.setText(_division.getName());
101            commentTextField.setText(_division.getComment());
102        }
103
104        // build menu
105        setJMenuBar(new JMenuBar());
106        addHelpMenu("package.jmri.jmrit.operations.Operations_LocationDivision", true); // NOI18N
107
108        // set frame size and division for display
109        initMinimumSize(new Dimension(Control.panelWidth700, Control.panelHeight200));
110    }
111
112    // Save, Delete, Add
113    @Override
114    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
115        if (ae.getSource() == saveDivisionButton) {
116            log.debug("division save button activated");
117            Division division = divisionManager.getDivisionByName(divisionNameTextField.getText());
118            if (_division == null && division == null) {
119                addDivision(); // GUI currently prevents this by disabling the save button
120            } else {
121                if (division != null && division != _division) {
122                    reportDivisionExists(Bundle.getMessage("save"));
123                    return;
124                }
125                saveDivision();
126            }
127            if (Setup.isCloseWindowOnSaveEnabled()) {
128                dispose();
129            }
130        }
131        if (ae.getSource() == deleteDivisionButton) {
132            log.debug("division delete button activated");
133            Division division = divisionManager.getDivisionByName(divisionNameTextField.getText());
134            if (division == null) {
135                return;
136            }
137            if (JmriJOptionPane.showConfirmDialog(this,
138                    Bundle.getMessage("DoYouWantToDeleteDivision",
139                            divisionNameTextField.getText()),
140                    Bundle.getMessage("DeleteDivision"), JmriJOptionPane.YES_NO_OPTION) != JmriJOptionPane.YES_OPTION) {
141                return;
142            }
143            divisionManager.deregister(division);
144            _division = null;
145
146            enableButtons(false);
147            OperationsXml.save();
148        }
149        if (ae.getSource() == addDivisionButton) {
150            Division division = divisionManager.getDivisionByName(divisionNameTextField.getText());
151            if (division != null) {
152                reportDivisionExists(Bundle.getMessage("add"));
153                return;
154            }
155            addDivision();
156        }
157    }
158
159    private void addDivision() {
160        if (!checkName(Bundle.getMessage("add"))) {
161            return;
162        }
163        _division = divisionManager.newDivision(divisionNameTextField.getText());
164        enableButtons(true);
165        saveDivision();
166    }
167
168    private void saveDivision() {
169        if (!checkName(Bundle.getMessage("save"))) {
170            return;
171        }
172        _division.setName(divisionNameTextField.getText());
173        _division.setComment(commentTextField.getText());
174        OperationsXml.save();
175    }
176
177    /**
178     *
179     * @return true if name is less than 26 characters
180     */
181    private boolean checkName(String s) {
182        if (divisionNameTextField.getText().trim().isEmpty()) {
183            return false;
184        }
185        if (divisionNameTextField.getText().length() > MAX_NAME_LENGTH) {
186            JmriJOptionPane.showMessageDialog(this,
187                    Bundle.getMessage("DivisionNameLengthMax", Integer.toString(MAX_NAME_LENGTH + 1)),
188                    Bundle.getMessage("CanNotDivision", s), JmriJOptionPane.ERROR_MESSAGE);
189            return false;
190        }
191        return true;
192    }
193
194    private void reportDivisionExists(String s) {
195        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ReportDivisionExists"),
196                Bundle.getMessage("CanNotDivision", s), JmriJOptionPane.ERROR_MESSAGE);
197    }
198
199    private void enableButtons(boolean enabled) {
200        saveDivisionButton.setEnabled(enabled);
201        deleteDivisionButton.setEnabled(enabled);
202    }
203
204    @Override
205    public void propertyChange(java.beans.PropertyChangeEvent e) {
206        if (Control.SHOW_PROPERTY) {
207            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
208                    e.getNewValue());
209        }
210    }
211
212    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DivisionEditFrame.class.getName());
213}