001package jmri.jmrit.operations.setup; 002 003import java.awt.BorderLayout; 004import java.awt.Color; 005import java.awt.Component; 006import java.awt.FlowLayout; 007import java.awt.event.ActionEvent; 008import java.awt.event.ActionListener; 009import java.awt.event.ItemEvent; 010import java.awt.event.ItemListener; 011import java.io.IOException; 012 013import javax.swing.*; 014import javax.swing.border.EmptyBorder; 015import javax.swing.border.TitledBorder; 016 017import jmri.InstanceManager; 018import jmri.jmrit.operations.OperationsManager; 019import jmri.jmrit.operations.OperationsXml; 020import jmri.util.swing.ExceptionContext; 021import jmri.util.swing.ExceptionDisplayFrame; 022import jmri.util.swing.JmriJOptionPane; 023import jmri.util.swing.UnexpectedExceptionContext; 024 025public class RestoreDialog extends JDialog { 026 027 028 029 private JPanel mainPanel; 030 private JPanel contentPanel; 031 private JRadioButton automaticBackupsRadioButton; 032 private JRadioButton defaultBackupsRadioButton; 033 private JComboBox<BackupSet> comboBox; 034 private JButton restoreButton; 035 //private JButton helpButton; 036 037 private BackupBase backup; 038 private String setName; 039 private JLabel newLabelLabel; 040 041 /** 042 * Create the dialog. 043 */ 044 public RestoreDialog() { 045 initComponents(); 046 } 047 048 private void initComponents() { 049 setModalityType(ModalityType.DOCUMENT_MODAL); 050 setModal(true); 051 setTitle(Bundle.getMessage("RestoreDialog.this.title")); 052 setBounds(100, 100, 378, 251); 053 mainPanel = new JPanel(); 054 mainPanel.setLayout(new BorderLayout()); 055 mainPanel.setBorder(null); 056 setContentPane(mainPanel); 057 { 058 059 contentPanel = new JPanel(); 060 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 061 contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); 062 mainPanel.add(contentPanel, BorderLayout.NORTH); 063 064 { 065 JPanel panel = new JPanel(); 066 panel.setAlignmentX(Component.LEFT_ALIGNMENT); 067 panel.setLayout(new FlowLayout(FlowLayout.LEFT)); 068 panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), // NOI18N 069 Bundle.getMessage("RestoreDialog.label.text"), TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); // NOI18N 070 071 contentPanel.add(panel); 072 ButtonGroup fromGroup = new ButtonGroup(); 073 074 { 075 automaticBackupsRadioButton = new JRadioButton(Bundle.getMessage("RestoreDialog.radioButton.autoBackup")); 076 automaticBackupsRadioButton.addActionListener(new ActionListener() { 077 @Override 078 public void actionPerformed(ActionEvent e) { 079 do_automaticBackupsRadioButton_actionPerformed(e); 080 } 081 }); 082 panel.add(automaticBackupsRadioButton); 083 fromGroup.add(automaticBackupsRadioButton); 084 } 085 { 086 defaultBackupsRadioButton = new JRadioButton(Bundle.getMessage("RestoreDialog.radioButton.defaultBackup")); 087 defaultBackupsRadioButton.addActionListener(new ActionListener() { 088 @Override 089 public void actionPerformed(ActionEvent e) { 090 do_defaultBackupsRadioButton_actionPerformed(e); 091 } 092 }); 093 panel.add(defaultBackupsRadioButton); 094 fromGroup.add(defaultBackupsRadioButton); 095 } 096 } 097 098 { 099 newLabelLabel = new JLabel(Bundle.getMessage("RestoreDialog.label2.text")); 100 newLabelLabel.setBorder(new EmptyBorder(10, 0, 5, 0)); 101 contentPanel.add(newLabelLabel); 102 } 103 { 104 comboBox = new JComboBox<>(); 105 comboBox.addItemListener(new ItemListener() { 106 @Override 107 public void itemStateChanged(ItemEvent arg0) { 108 do_comboBox_itemStateChanged(arg0); 109 } 110 }); 111 comboBox.setAlignmentX(Component.LEFT_ALIGNMENT); 112 contentPanel.add(comboBox); 113 } 114 } 115 116 { 117 JPanel buttonPane = new JPanel(); 118 FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.CENTER); 119 buttonPane.setLayout(fl_buttonPane); 120 mainPanel.add(buttonPane, BorderLayout.SOUTH); 121 { 122 restoreButton = new JButton(Bundle.getMessage("RestoreDialog.retoreButton.text")); 123 restoreButton.addActionListener(new ActionListener() { 124 @Override 125 public void actionPerformed(ActionEvent e) { 126 do_restoreButton_actionPerformed(e); 127 } 128 }); 129 buttonPane.add(restoreButton); 130 } 131 { 132 JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel")); 133 cancelButton.addActionListener(new ActionListener() { 134 @Override 135 public void actionPerformed(ActionEvent arg0) { 136 do_cancelButton_actionPerformed(arg0); 137 } 138 }); 139 buttonPane.add(cancelButton); 140 } 141 } 142 143 // Start out with Default backups 144 defaultBackupsRadioButton.doClick(); 145// pack(); 146// setLocationRelativeTo(null); 147// setVisible(true); 148 } 149 150 // Event handlers 151 protected void do_automaticBackupsRadioButton_actionPerformed(ActionEvent e) { 152 backup = new AutoBackup(); 153 loadComboBox(); 154 } 155 156 protected void do_defaultBackupsRadioButton_actionPerformed(ActionEvent e) { 157 backup = new DefaultBackup(); 158 loadComboBox(); 159 } 160 161 protected void do_cancelButton_actionPerformed(ActionEvent arg0) { 162 dispose(); 163 } 164 165 protected void do_comboBox_itemStateChanged(ItemEvent arg0) { 166 // If something has been selected, enable the Restore 167 // button. 168 // If we only have a deselect, then the button will remain disabled. 169 // When changing between items in the list, the item being left is 170 // deselected first, then the new item is selected. 171 // 172 // Not sure if there can be more than two states, so using the if 173 // statement to be safe. 174 int stateChange = arg0.getStateChange(); 175 176 if (stateChange == ItemEvent.SELECTED) { 177 restoreButton.setEnabled(true); 178 } else if (stateChange == ItemEvent.DESELECTED) { 179 restoreButton.setEnabled(false); 180 } 181 182 } 183 184 protected void do_helpButton_actionPerformed(ActionEvent arg0) { 185 // Not implemented yet. 186 } 187 188 protected void do_restoreButton_actionPerformed(ActionEvent e) { 189 log.debug("restore button activated"); 190 191 // check to see if files are dirty 192 if (OperationsXml.areFilesDirty()) { 193 if (JmriJOptionPane.showConfirmDialog(this, Bundle.getMessage("OperationsFilesModified"), 194 Bundle.getMessage("SaveOperationFiles"), JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) { 195 OperationsXml.save(); 196 } 197 } 198 199 // first backup the users data in case they forgot 200 try { 201 AutoBackup auto = new AutoBackup(); 202 auto.autoBackup(); 203 204 // now delete the current operations files in case the restore isn't a full set of files 205 backup.deleteOperationsFiles(); 206 207 setName = ((BackupSet) comboBox.getSelectedItem()).getSetName(); 208 209 // The restore method should probably be overloaded to accept a 210 // BackupSet instead of a simple string. Later. 211 backup.restoreFilesFromSetName(setName); 212 213 // now deregister shut down task 214 // If Trains window was opened, then task is active 215 // otherwise it is normal to not have the task running 216 InstanceManager.getDefault(OperationsManager.class).setShutDownTask(null); 217 218 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("YouMustRestartAfterRestore"), 219 Bundle.getMessage("RestoreSuccessful"), JmriJOptionPane.INFORMATION_MESSAGE); 220 dispose(); 221 222 try { 223 InstanceManager.getDefault(jmri.ShutDownManager.class).restart(); 224 } catch (Exception er) { 225 log.error("Continuing after error in handleRestart", er); 226 } 227 } // These may need to be enhanced to show the backup store being used, 228 // auto or default. 229 catch (IOException ex) { 230 ExceptionContext context = new ExceptionContext(ex, Bundle.getMessage("RestoreDialog.restoring") 231 + " " + setName, "Hint about checking valid names, etc."); // NOI18N 232 ExceptionDisplayFrame.displayExceptionDisplayFrame(this, context); 233 234 } catch (Exception ex) { 235 log.error("Doing restore from {}", setName, ex); 236 237 UnexpectedExceptionContext context = new UnexpectedExceptionContext(ex, 238 Bundle.getMessage("RestoreDialog.restoring") + " " + setName); 239 240 ExceptionDisplayFrame.displayExceptionDisplayFrame(this, context); 241 } 242 } 243 244 /** 245 * Adds the names of the backup sets that are available in either the 246 * Automatic or the Default backup store. 247 */ 248 private void loadComboBox() { 249 // Get the Backup Sets from the currently selected backup store. 250 // Called after the radio button selection has changed 251 252 // Disable the Restore button in case there is nothing loaded into the ComboBox 253 restoreButton.setEnabled(false); 254 255 comboBox.removeAllItems(); 256 257 BackupSet[] sets = backup.getBackupSets(); 258 ComboBoxModel<BackupSet> model = new DefaultComboBoxModel<>(sets); 259 260 // Clear any current selection so that the state change will fire when 261 // we set a selection. 262 model.setSelectedItem(null); 263 264 comboBox.setModel(model); 265 266 // Position at the last item, which is usually the most recent 267 // This is ugly code, but it works.... 268 if (model.getSize() > 0) { 269 comboBox.setSelectedIndex(model.getSize() - 1); 270 } 271 } 272 273 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RestoreDialog.class); 274 275}