001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.Component;
004import java.awt.Container;
005import java.awt.Dimension;
006import java.awt.FlowLayout;
007import java.awt.event.ActionEvent;
008import java.util.ArrayList;
009import java.util.List;
010
011import javax.swing.*;
012
013import jmri.*;
014import jmri.jmrit.logixng.tools.ImportLogix;
015import jmri.util.JmriJFrame;
016import jmri.util.swing.JmriJOptionPane;
017import jmri.swing.JTitledSeparator;
018
019/**
020 * Imports Logixs to LogixNG
021 *
022 * @author Daniel Bergqvist 2019
023 */
024public final class ImportLogixFrame extends JmriJFrame {
025
026//    private static final int panelWidth700 = 700;
027//    private static final int panelHeight500 = 500;
028
029    private JRadioButton _whichLogix_All;
030    private JRadioButton _whichLogix_AllActive;
031    private JRadioButton _whichLogix_Selected;
032    private JRadioButton _whatToDo_Nothing;
033    private JRadioButton _whatToDo_Disable;
034    private JRadioButton _whatToDo_Delete;
035    private JCheckBox _includeSystemLogixs;
036
037    private JButton _importLogix;
038    private JButton _cancelDone;
039
040    private static final String SYSLOGIX = InstanceManager.getDefault(LogixManager.class).getSystemNamePrefix() + ":SYS";
041    private static final String RTXLOGIX = "RTX";
042    private static final String USSLOGIX = "USS CTC:OsIndicator";
043
044    /**
045     * Construct a LogixNGEditor.
046     */
047    public ImportLogixFrame() {
048        setTitle(Bundle.getMessage("TitleImportLogix"));
049    }
050
051    @Override
052    public void initComponents() {
053        super.initComponents();
054       JMenuBar menuBar = new JMenuBar();
055       setJMenuBar(menuBar);
056       addHelpMenu("package.jmri.jmrit.logixng.LogixImport", true); // NOI18N
057
058        Container contentPanel = getContentPane();
059//        contentPanel.setLayout(new GridLayout( 0, 1));
060        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
061
062        add(new JTitledSeparator(Bundle.getMessage("Import_WhichLogix")));
063        _whichLogix_All = new JRadioButton(Bundle.getMessage("Import_WhichLogix_All"));
064        _whichLogix_AllActive = new JRadioButton(Bundle.getMessage("Import_WhichLogix_AllActive"));
065        _whichLogix_AllActive.setEnabled(false);
066        _whichLogix_Selected = new JRadioButton(Bundle.getMessage("Import_WhichLogix_Selected"));
067        _whichLogix_Selected.setEnabled(false);
068        ButtonGroup buttonGroup = new ButtonGroup();
069        buttonGroup.add(_whichLogix_All);
070        buttonGroup.add(_whichLogix_AllActive);
071        buttonGroup.add(_whichLogix_Selected);
072        _whichLogix_All.setSelected(true);
073//        r2.setSelected(true);
074        contentPanel.add(_whichLogix_All);
075        contentPanel.add(_whichLogix_AllActive);
076        contentPanel.add(_whichLogix_Selected);
077
078        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));    // vertical space.
079
080        add(new JTitledSeparator(Bundle.getMessage("Import_WhatToDo")));
081        _whatToDo_Nothing = new JRadioButton(Bundle.getMessage("Import_WhatToDo_Nothing"));
082        _whatToDo_Disable = new JRadioButton(Bundle.getMessage("Import_WhatToDo_Disable"));
083        _whatToDo_Disable.setEnabled(false);
084        _whatToDo_Delete = new JRadioButton(Bundle.getMessage("Import_WhatToDo_Delete"));
085        _whatToDo_Delete.setEnabled(false);
086        ButtonGroup buttonGroup2 = new ButtonGroup();
087        buttonGroup2.add(_whatToDo_Nothing);
088        buttonGroup2.add(_whatToDo_Disable);
089        buttonGroup2.add(_whatToDo_Delete);
090        _whatToDo_Nothing.setSelected(true);
091//        _whatToDo_Disable.setSelected(true);
092        contentPanel.add(_whatToDo_Nothing);
093        contentPanel.add(_whatToDo_Disable);
094        contentPanel.add(_whatToDo_Delete);
095
096        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));    // vertical space.
097
098        add(new JTitledSeparator(Bundle.getMessage("Import_IncludeSystemLogixs")));
099        _includeSystemLogixs = new JCheckBox(Bundle.getMessage("Import_IncludeSystemLogixs"));
100//        includeSystemLogixs.addItemListener((ItemEvent e) -> {
101//            if (includeSystemLogixs.isSelected()) {
102//                _systemName.setEnabled(false);
103//                _sysNameLabel.setEnabled(false);
104//            } else {
105//                _systemName.setEnabled(true);
106//                _sysNameLabel.setEnabled(true);
107//            }
108//        });
109        add(_includeSystemLogixs);
110
111        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));    // vertical space.
112
113        add(new JTitledSeparator(Bundle.getMessage("Import_SelectLogix")));
114
115        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));    // vertical space.
116
117        add(new JTitledSeparator(Bundle.getMessage("Import_WarningMessage")));
118
119        JLabel warning = new JLabel(Bundle.getMessage("Import_WarningMessage_Long"));
120        JPanel warningPanel = new JPanel();
121        warningPanel.add(warning);
122        contentPanel.add(warningPanel);
123
124        contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));    // vertical space.
125
126
127        // set up import and cancel buttons
128        JPanel panel5 = new JPanel();
129        panel5.setLayout(new FlowLayout());
130
131        // Import
132        _importLogix = new JButton(Bundle.getMessage("Import_ButtonImport"));    // NOI18N
133        panel5.add(_importLogix);
134        _importLogix.addActionListener((ActionEvent e) -> {
135            doImport();
136            _cancelDone.setText(Bundle.getMessage("ButtonDone"));
137//            dispose();
138        });
139//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
140        _importLogix.setToolTipText("ImportLogixButtonHint");      // NOI18N
141//        panel5.setAlignmentX(LEFT_ALIGNMENT);
142        // Cancel
143        _cancelDone = new JButton(Bundle.getMessage("ButtonCancel"));    // NOI18N
144        panel5.add(_cancelDone);
145        _cancelDone.addActionListener((ActionEvent e) -> {
146            dispose();
147        });
148//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
149        _cancelDone.setToolTipText("CancelLogixButtonHint");      // NOI18N
150//        panel5.setAlignmentX(LEFT_ALIGNMENT);
151        contentPanel.add(panel5);
152
153        // Align all components to the left
154        for (Component c : contentPanel.getComponents()) {
155            ((JComponent)c).setAlignmentX(LEFT_ALIGNMENT);
156        }
157
158        pack();
159        setLocationRelativeTo(null);
160        setVisible(true);
161
162//        initMinimumSize(new Dimension(panelWidth700, panelHeight500));
163    }
164
165    public void initMinimumSize(Dimension dimension) {
166        setMinimumSize(dimension);
167        pack();
168        setVisible(true);
169    }
170
171    private void doImport() {
172//        private JRadioButton _whichLogix_All;
173//        private JRadioButton _whichLogix_AllActive;
174//        private JRadioButton _whichLogix_Selected;
175//        private JRadioButton _whatToDo_Nothing;
176//        private JRadioButton _whatToDo_Disable;
177//        private JRadioButton _whatToDo_Delete;
178//        private JCheckBox _includeSystemLogixs;
179        List<Logix> logixs = new ArrayList<>();
180        if (_whichLogix_All.isSelected()) {
181            for (Logix logix : InstanceManager.getDefault(LogixManager.class).getNamedBeanSet()) {
182                boolean isSystemLogix =
183                        logix.getSystemName().equals(SYSLOGIX) ||
184                        logix.getSystemName().contains(RTXLOGIX) ||
185                        logix.getSystemName().contains(USSLOGIX);
186                if (!isSystemLogix || _includeSystemLogixs.isSelected()) {
187                    logixs.add(logix);
188                }
189            }
190        } else if (_whichLogix_All.isSelected()) {
191            throw new RuntimeException("Currently not supported");
192        } else if (_whichLogix_All.isSelected()) {
193            throw new RuntimeException("Currently not supported");
194        } else {
195            throw new RuntimeException("No choice selected");
196        }
197
198        boolean error = false;
199        StringBuilder errorMessage = new StringBuilder("<html><table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">");
200        errorMessage.append("<tr><th>");
201        errorMessage.append(Bundle.getMessage("ColumnSystemName"));
202        errorMessage.append("</th><th>");
203        errorMessage.append(Bundle.getMessage("ColumnUserName"));
204        errorMessage.append("</th><th>");
205        errorMessage.append(Bundle.getMessage("ImportLogixColumnError"));
206        errorMessage.append("</th></tr>");
207
208        for (Logix logix : logixs) {
209            try {
210                ImportLogix importLogix = new ImportLogix(logix, _includeSystemLogixs.isSelected(), true);
211                importLogix.doImport();
212            } catch (JmriException e) {
213                errorMessage.append("<tr><td>");
214                errorMessage.append(logix.getSystemName());
215                errorMessage.append("</td><td>");
216                errorMessage.append(logix.getUserName() != null ? logix.getUserName() : "");
217                errorMessage.append("</td><td>");
218                errorMessage.append(e.getMessage());
219                errorMessage.append("</td></tr>");
220                log.error("Error thrown: {}", e, e);
221                error = true;
222            }
223        }
224
225        if (!error) {
226            for (Logix logix : logixs) {
227                try {
228                    ImportLogix importLogix = new ImportLogix(logix, _includeSystemLogixs.isSelected(), false);
229                    importLogix.doImport();
230                } catch (JmriException e) {
231                    throw new RuntimeException("Unexpected error: "+e.getMessage(), e);
232                }
233            }
234            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("LogixsAreImported"),
235                Bundle.getMessage("TitleLogixsImportSuccess"), JmriJOptionPane.INFORMATION_MESSAGE);
236        } else {
237            errorMessage.append("</table></html>");
238            JmriJOptionPane.showMessageDialog(this, errorMessage.toString(),
239                Bundle.getMessage("TitleLogixImportError"), JmriJOptionPane.ERROR_MESSAGE);
240        }
241    }
242
243
244    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImportLogixFrame.class);
245
246}