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 _importLogix.setToolTipText("ImportLogixButtonHint"); // NOI18N 140// panel5.setAlignmentX(LEFT_ALIGNMENT); 141 // Cancel 142 _cancelDone = new JButton(Bundle.getMessage("ButtonCancel")); // NOI18N 143 panel5.add(_cancelDone); 144 _cancelDone.addActionListener((ActionEvent e) -> { 145 dispose(); 146 }); 147 _cancelDone.setToolTipText(Bundle.getMessage("LogixNG_CancelButtonHint")); // NOI18N 148// panel5.setAlignmentX(LEFT_ALIGNMENT); 149 contentPanel.add(panel5); 150 151 // Align all components to the left 152 for (Component c : contentPanel.getComponents()) { 153 ((JComponent)c).setAlignmentX(LEFT_ALIGNMENT); 154 } 155 156 pack(); 157 setLocationRelativeTo(null); 158 setVisible(true); 159 160// initMinimumSize(new Dimension(panelWidth700, panelHeight500)); 161 } 162 163 public void initMinimumSize(Dimension dimension) { 164 setMinimumSize(dimension); 165 pack(); 166 setVisible(true); 167 } 168 169 private void doImport() { 170// private JRadioButton _whichLogix_All; 171// private JRadioButton _whichLogix_AllActive; 172// private JRadioButton _whichLogix_Selected; 173// private JRadioButton _whatToDo_Nothing; 174// private JRadioButton _whatToDo_Disable; 175// private JRadioButton _whatToDo_Delete; 176// private JCheckBox _includeSystemLogixs; 177 List<Logix> logixs = new ArrayList<>(); 178 if (_whichLogix_All.isSelected()) { 179 for (Logix logix : InstanceManager.getDefault(LogixManager.class).getNamedBeanSet()) { 180 boolean isSystemLogix = 181 logix.getSystemName().equals(SYSLOGIX) || 182 logix.getSystemName().contains(RTXLOGIX) || 183 logix.getSystemName().contains(USSLOGIX); 184 if (!isSystemLogix || _includeSystemLogixs.isSelected()) { 185 logixs.add(logix); 186 } 187 } 188 } else if (_whichLogix_All.isSelected()) { 189 throw new RuntimeException("Currently not supported"); 190 } else if (_whichLogix_All.isSelected()) { 191 throw new RuntimeException("Currently not supported"); 192 } else { 193 throw new RuntimeException("No choice selected"); 194 } 195 196 boolean error = false; 197 StringBuilder errorMessage = new StringBuilder("<html><table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">"); 198 errorMessage.append("<tr><th>"); 199 errorMessage.append(Bundle.getMessage("ColumnSystemName")); 200 errorMessage.append("</th><th>"); 201 errorMessage.append(Bundle.getMessage("ColumnUserName")); 202 errorMessage.append("</th><th>"); 203 errorMessage.append(Bundle.getMessage("ImportLogixColumnError")); 204 errorMessage.append("</th></tr>"); 205 206 for (Logix logix : logixs) { 207 try { 208 ImportLogix importLogix = new ImportLogix(logix, _includeSystemLogixs.isSelected(), true); 209 importLogix.doImport(); 210 } catch (JmriException e) { 211 errorMessage.append("<tr><td>"); 212 errorMessage.append(logix.getSystemName()); 213 errorMessage.append("</td><td>"); 214 errorMessage.append(logix.getUserName() != null ? logix.getUserName() : ""); 215 errorMessage.append("</td><td>"); 216 errorMessage.append(e.getMessage()); 217 errorMessage.append("</td></tr>"); 218 log.error("Error thrown: {}", e, e); 219 error = true; 220 } 221 } 222 223 if (!error) { 224 for (Logix logix : logixs) { 225 try { 226 ImportLogix importLogix = new ImportLogix(logix, _includeSystemLogixs.isSelected(), false); 227 importLogix.doImport(); 228 } catch (JmriException e) { 229 throw new RuntimeException("Unexpected error: "+e.getMessage(), e); 230 } 231 } 232 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("LogixsAreImported"), 233 Bundle.getMessage("TitleLogixsImportSuccess"), JmriJOptionPane.INFORMATION_MESSAGE); 234 } else { 235 errorMessage.append("</table></html>"); 236 JmriJOptionPane.showMessageDialog(this, errorMessage.toString(), 237 Bundle.getMessage("TitleLogixImportError"), JmriJOptionPane.ERROR_MESSAGE); 238 } 239 } 240 241 242 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImportLogixFrame.class); 243 244}