001package jmri.jmrit.conditional;
002
003import java.awt.Component;
004import java.awt.Container;
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007
008import javax.swing.*;
009import javax.swing.border.Border;
010
011import jmri.*;
012import jmri.NamedBean.DisplayOptions;
013import jmri.util.JmriJFrame;
014import jmri.util.swing.JmriJOptionPane;
015
016/**
017 *
018 * <p>
019 * Compare with the other Conditional Edit tool {@link ConditionalTreeEdit}
020 * and {@link ConditionalListEdit}
021 *
022 * @author Pete Cressman Copyright (C) 2020
023 */
024public class ConditionalListCopy extends ConditionalList {
025
026    /**
027     * Create a new Conditional List View editor.
028     *
029     * @param srcLogixName name of the Logix being copied
030     * @param targetLogix Logix where Conditional copies are placed
031     */
032    public ConditionalListCopy(String srcLogixName, Logix targetLogix) {
033        super(srcLogixName);
034        _targetLogix = targetLogix;
035        makeEditLogixWindow();
036    }
037
038    public ConditionalListCopy() {
039    }
040
041    // ------------ Logix Variables ------------
042    Logix _targetLogix;
043    ConditionalListModel _conditionalListModel;
044    JList<Conditional> _conditionalList;  // picklist of logix's conditionals
045    JRadioButton _fullEditButton;
046
047    // ------------ Conditional Variables ------------
048
049
050    void makeEditLogixWindow() {
051        _editLogixFrame = new JmriJFrame(Bundle.getMessage("TitleCopyFromLogix",
052                _curLogix.getDisplayName(DisplayOptions.QUOTED_USERNAME_SYSTEMNAME)));  // NOI18N
053        _editLogixFrame.addHelpMenu(
054                "package.jmri.jmrit.conditional.ConditionalCopy", true);  // NOI18N
055        Container contentPane = _editLogixFrame.getContentPane();
056        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
057        contentPane.add(Box.createVerticalStrut(10));
058
059        JPanel topPanel = new  JPanel();
060        topPanel.add(new JLabel(Bundle.getMessage("SelectCopyConditional", _targetLogix.getDisplayName())));
061        contentPane.add(topPanel);
062        contentPane.add(Box.createVerticalStrut(10));
063
064        JPanel listPanel = new JPanel();
065        listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
066        _conditionalListModel = new ConditionalListModel(_curLogix);
067        _conditionalList = new JList<>(_conditionalListModel);
068        _conditionalList.setCellRenderer(new ConditionalCellRenderer());
069        _conditionalList.setVisibleRowCount(6);
070        _conditionalList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
071        listPanel.add(new JScrollPane(_conditionalList));
072        Border listPanelBorder = BorderFactory.createEtchedBorder();
073        Border listPanelTitled = BorderFactory.createTitledBorder(
074                listPanelBorder, Bundle.getMessage("TitleConditionalList"));  // NOI18N
075        listPanel.setBorder(listPanelTitled);
076        contentPane.add(listPanel);
077        contentPane.add(Box.createVerticalStrut(10));
078
079        JPanel panel = new JPanel();
080        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
081        ButtonGroup bGroup = new ButtonGroup();
082        _fullEditButton = new JRadioButton(Bundle.getMessage("fullEditButton"));  // NOI18N
083        _fullEditButton.setToolTipText(Bundle.getMessage("HintFullEditButton"));  // NOI18N
084        bGroup.add(_fullEditButton);
085        panel.add(_fullEditButton);
086        JRadioButton itemsOnlyButton = new JRadioButton(Bundle.getMessage("itemsOnlyButton"));  // NOI18N
087        itemsOnlyButton.setToolTipText(Bundle.getMessage("HintItemsOnlyButton"));  // NOI18N
088        bGroup.add(itemsOnlyButton);
089        panel.add(itemsOnlyButton);
090        itemsOnlyButton.setSelected(true);
091        JPanel p =  new JPanel();
092        p.add(panel);
093        p.add(Box.createVerticalStrut(10));
094        contentPane.add(p);
095
096        panel = new JPanel();
097        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
098
099        JButton editButton = new JButton(Bundle.getMessage("CopyConditionalButton"));  // NOI18N
100        editButton.setToolTipText(Bundle.getMessage("HintCopyConditionalButton", _targetLogix.getDisplayName()));  // NOI18N
101        panel.add(editButton);
102        editButton.addActionListener(new ActionListener() {
103            @Override
104            public void actionPerformed(ActionEvent e) {
105                editButtonPressed();
106            }
107        });
108        panel.add(Box.createHorizontalStrut(10));
109
110        JButton exitButton = new JButton(Bundle.getMessage("ButtonDone"));  // NOI18N
111        exitButton.setToolTipText(Bundle.getMessage("HintExitButton"));  // NOI18N
112        panel.add(exitButton);
113        exitButton.addActionListener(new ActionListener() {
114            @Override
115            public void actionPerformed(ActionEvent e) {
116                donePressed(e);
117            }
118        });
119        p =  new JPanel();
120        p.add(panel);
121        p.add(Box.createVerticalStrut(10));
122        p.add(panel);
123        p.add(Box.createVerticalStrut(10));
124        contentPane.add(p);
125
126        _editLogixFrame.addWindowListener(new java.awt.event.WindowAdapter() {
127            @Override
128            public void windowClosing(java.awt.event.WindowEvent e) {
129                donePressed(null);
130            }
131        });
132        _editLogixFrame.pack();
133        _editLogixFrame.setVisible(true);
134
135    }
136
137    void editButtonPressed() {
138        Conditional conditional = _conditionalList.getSelectedValue();
139        if (conditional == null) {
140            JmriJOptionPane.showMessageDialog(_editLogixFrame,
141                    Bundle.getMessage("SelectCopyConditional"),
142                    Bundle.getMessage("ReminderTitle"), // NOI18N
143                    JmriJOptionPane.INFORMATION_MESSAGE);
144            return;
145        }
146        // make copy
147        _curConditional = makeNewConditional(_targetLogix);
148        _curConditional.setStateVariables(conditional.getCopyOfStateVariables());
149        _curConditional.setAction(conditional.getCopyOfActions());
150        _curConditional.setLogicType(conditional.getLogicType(), conditional.getAntecedentExpression());
151        _curConditional.setUserName(Bundle.getMessage("CopyOf", conditional.getDisplayName(DisplayOptions.DISPLAYNAME)));
152        if (_fullEditButton.isSelected()) {
153            makeEditConditionalWindow(conditional);
154        } else {
155            makeChangeItemNameWindow(conditional);
156        }
157    }
158
159    /**
160     * Copy a Conditional, with full editing capabilities.
161     * @param  srcCond conditional to be copied.
162     */
163    void makeEditConditionalWindow(Conditional srcCond) {
164        log.debug("makeEditConditionalWindow");
165        // deactivate this Logix
166        _curLogix.deActivateLogix();
167        _targetLogix.deActivateLogix();
168
169        _conditionalFrame = new ConditionalEditFrame(
170                Bundle.getMessage("TitleCopyConditional",
171                        srcCond.getDisplayName(DisplayOptions.QUOTED_USERNAME_SYSTEMNAME)),
172                _curConditional, this);  // NOI18N
173
174        _conditionalFrame.pack();
175        _conditionalFrame.setVisible(true);
176        InstanceManager.getDefault(jmri.util.PlaceWindow.class).nextTo(_editLogixFrame, null, _conditionalFrame);
177    }
178
179    /**
180     * Make the bottom panel for _conditionalFrame to hold buttons for
181     * Update/Save, Cancel, Delete/FullEdit
182     *
183     * @return the panel
184     */
185    @Override
186    JPanel makeBottomPanel() {
187        JPanel panel = new JPanel();
188
189        JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));  // NOI18N
190        panel.add(saveButton);
191        saveButton.addActionListener(new ActionListener() {
192            @Override
193            public void actionPerformed(ActionEvent e) {
194                _conditionalFrame.updateConditionalPressed(e);
195            }
196        });
197        saveButton.setToolTipText(Bundle.getMessage("SaveConditionalButtonHint", _targetLogix.getDisplayName()));  // NOI18N
198        // Cancel
199        JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel"));  // NOI18N
200        panel.add(cancelButton);
201        cancelButton.addActionListener(new ActionListener() {
202            @Override
203            public void actionPerformed(ActionEvent e) {
204                _conditionalFrame.cancelConditionalPressed();
205            }
206        });
207        cancelButton.setToolTipText(Bundle.getMessage("CancelSaveButtonHint"));  // NOI18N
208
209        return panel;
210    }
211
212    @Override
213    boolean updateConditional(String uName, Conditional.AntecedentOperator logicType, boolean trigger, String antecedent) {
214        return super.updateConditional(uName, _targetLogix, logicType, trigger, antecedent);
215    }
216
217    @Override
218    void updateConditionalTableModel() {
219        log.debug("updateConditionalTableModel :: not needed for copy process");
220    }
221
222    @Override
223    void closeConditionalFrame() {
224        super.closeConditionalFrame(_targetLogix);
225    }
226
227    /**
228     * Copy a Conditional, but only change item names
229     * @param  srcCond conditional to be copied.
230     */
231    void makeChangeItemNameWindow(Conditional srcCond) {
232        // deactivate this Logix
233        _curLogix.deActivateLogix();
234        _targetLogix.deActivateLogix();
235
236        _conditionalFrame = new ConditionalCopyFrame(
237                Bundle.getMessage("TitleCopyConditional",
238                        srcCond.getDisplayName(DisplayOptions.QUOTED_USERNAME_SYSTEMNAME)),
239                _curConditional, this);  // NOI18N
240
241        _conditionalFrame.pack();
242        _conditionalFrame.setVisible(true);
243        InstanceManager.getDefault(jmri.util.PlaceWindow.class).nextTo(_editLogixFrame, null, _conditionalFrame);
244    }
245
246    /**
247     * Respond to the Done button in the Edit Logix window.
248     *
249     * @param e The event heard
250     */
251    void donePressed(ActionEvent e) {
252        showSaveReminder();
253        _inEditMode = false;
254        if (_targetLogix.getNumConditionals() == 0) {
255            // no conditionals were copied - remove logix
256            _logixManager.deleteLogix(_targetLogix);
257        }
258        closeConditionalFrame();
259        if (_editLogixFrame != null) {
260            _editLogixFrame.setVisible(false);
261            _editLogixFrame.dispose();
262            _editLogixFrame = null;
263        }
264        logixData.clear();
265        logixData.put("Finish", _curLogix.getSystemName());   // NOI18N
266        fireLogixEvent();
267    }
268
269    private static class ConditionalCellRenderer extends JLabel implements ListCellRenderer<Conditional>{
270
271        @Override
272        public Component getListCellRendererComponent(
273                JList<? extends Conditional> list, // the list
274                Conditional value, // value to display
275                int index, // cell index
276                boolean isSelected, // is the cell selected
277                boolean cellHasFocus) // does the cell have focus
278        {
279            String s = value.getDisplayName(DisplayOptions.USERNAME_SYSTEMNAME);
280            setText(s);
281            if (isSelected) {
282                setBackground(list.getSelectionBackground());
283                setForeground(list.getSelectionForeground());
284            } else {
285                setBackground(list.getBackground());
286                setForeground(list.getForeground());
287            }
288            setEnabled(list.isEnabled());
289            setFont(list.getFont());
290            setOpaque(true);
291            return this;
292        }
293    }
294
295    static class ConditionalListModel extends DefaultListModel<Conditional> {
296
297        Logix _srcLogic;
298
299        ConditionalListModel(Logix srcLogic) {
300            _srcLogic = srcLogic;
301            if (srcLogic!=null){
302                for (int i = 0; i < srcLogic.getNumConditionals(); i++) {
303                    addElement(srcLogic.getConditional(srcLogic.getConditionalByNumberOrder(i)));
304                }
305            }
306        }
307    }
308
309    @Override
310    protected String getClassName() {
311        return ConditionalListEdit.class.getName();
312    }
313
314    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConditionalListCopy.class);
315
316}
317