001package jmri.jmrit.operations.setup.backup;
002
003import java.awt.*;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import java.io.IOException;
007import java.text.MessageFormat;
008
009import javax.swing.*;
010import javax.swing.border.EmptyBorder;
011import javax.swing.event.DocumentEvent;
012import javax.swing.event.DocumentListener;
013
014import jmri.InstanceManager;
015import jmri.jmrit.operations.OperationsXml;
016import jmri.jmrit.operations.setup.Setup;
017import jmri.util.swing.*;
018
019public class BackupDialog extends JDialog {
020
021    
022
023    private final JPanel contentPanel = new JPanel();
024    private JLabel captionLabel;
025    private JTextField setNameTextField;
026    private JLabel infoLabel1;
027    private JLabel infoLabel2;
028    private JButton backupButton;
029    // private JButton helpButton;
030
031    private DefaultBackup backup;
032
033    /**
034     * Create the dialog.
035     */
036    public BackupDialog() {
037        backup = InstanceManager.getDefault(DefaultBackup.class);
038
039        initComponents();
040    }
041
042    private void initComponents() {
043        setModalityType(ModalityType.DOCUMENT_MODAL);
044        setModal(true);
045        setTitle(Bundle.getMessage("BackupDialog.this.title"));
046        setBounds(100, 100, 395, 199);
047        getContentPane().setLayout(new BorderLayout());
048        {
049            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
050
051            GridBagLayout gbl = new GridBagLayout();
052            gbl.columnWidths = new int[]{0, 0};
053            gbl.rowHeights = new int[]{0, 0, 0, 0, 0};
054            gbl.columnWeights = new double[]{1.0, Double.MIN_VALUE};
055            gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0,
056                Double.MIN_VALUE};
057            contentPanel.setLayout(gbl);
058            getContentPane().add(contentPanel, BorderLayout.CENTER);
059            {
060                captionLabel = new JLabel(
061                        Bundle.getMessage("BackupDialog.nameLabel.text"));
062                GridBagConstraints gbc_captionLabel = new GridBagConstraints();
063                gbc_captionLabel.anchor = GridBagConstraints.WEST;
064                gbc_captionLabel.insets = new Insets(0, 0, 5, 0);
065                gbc_captionLabel.gridx = 0;
066                gbc_captionLabel.gridy = 0;
067                contentPanel.add(captionLabel, gbc_captionLabel);
068            }
069            {
070                setNameTextField = new JTextField();
071                setNameTextField.setText(backup.suggestBackupSetName());
072
073                setNameTextField.getDocument().addDocumentListener(
074                        new DocumentListener() {
075
076                            // These should probably pass the document to
077                            // enableBackupButton....
078                            @Override
079                            public void removeUpdate(DocumentEvent arg0) {
080                                enableBackupButton();
081                            }
082
083                            @Override
084                            public void insertUpdate(DocumentEvent arg0) {
085                                enableBackupButton();
086                            }
087
088                    @Override
089                    public void changedUpdate(DocumentEvent arg0) {
090                        enableBackupButton();
091                    }
092                });
093
094                GridBagConstraints gbc_setNameTextField = new GridBagConstraints();
095                gbc_setNameTextField.insets = new Insets(0, 0, 5, 0);
096                gbc_setNameTextField.fill = GridBagConstraints.HORIZONTAL;
097                gbc_setNameTextField.gridx = 0;
098                gbc_setNameTextField.gridy = 1;
099                contentPanel.add(setNameTextField, gbc_setNameTextField);
100                setNameTextField.setColumns(10);
101            }
102            {
103                infoLabel1 = new JLabel(Bundle.getMessage("BackupDialog.notesLabel1.text"));
104                GridBagConstraints gbc_infoLabel1 = new GridBagConstraints();
105                gbc_infoLabel1.insets = new Insets(0, 0, 5, 0);
106                gbc_infoLabel1.anchor = GridBagConstraints.NORTHWEST;
107                gbc_infoLabel1.gridx = 0;
108                gbc_infoLabel1.gridy = 2;
109                contentPanel.add(infoLabel1, gbc_infoLabel1);
110            }
111            {
112                infoLabel2 = new JLabel(Bundle.getMessage("BackupDialog.notesLabel2.text"));
113                GridBagConstraints gbc_infoLabel2 = new GridBagConstraints();
114                gbc_infoLabel2.anchor = GridBagConstraints.WEST;
115                gbc_infoLabel2.gridx = 0;
116                gbc_infoLabel2.gridy = 3;
117                contentPanel.add(infoLabel2, gbc_infoLabel2);
118            }
119        }
120        {
121            JPanel buttonPane = new JPanel();
122            FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.CENTER);
123            buttonPane.setLayout(fl_buttonPane);
124            getContentPane().add(buttonPane, BorderLayout.SOUTH);
125            {
126                backupButton = new JButton(Bundle.getMessage("BackupDialog.backupButton.text"));
127                backupButton.addActionListener(new ActionListener() {
128                    @Override
129                    public void actionPerformed(ActionEvent e) {
130                        do_backupButton_actionPerformed(e);
131                    }
132                });
133                buttonPane.add(backupButton);
134                getRootPane().setDefaultButton(backupButton);
135            }
136            {
137                JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel"));
138                cancelButton.addActionListener(new ActionListener() {
139                    @Override
140                    public void actionPerformed(ActionEvent arg0) {
141                        do_cancelButton_actionPerformed(arg0);
142                    }
143                });
144                cancelButton.setActionCommand("Cancel"); // NOI18N
145                buttonPane.add(cancelButton);
146            }
147            // Help button isn't used yet
148            //   {
149            //    helpButton = new JButton(Bundle.getMessage("BackupDialog.helpButton.text"));
150            //    helpButton.addActionListener(new ActionListener() {
151            //     public void actionPerformed(ActionEvent e) {
152            //      do_helpButton_actionPerformed(e);
153            //     }
154            //    });
155            //    helpButton.setEnabled(false);
156            //    buttonPane.add(helpButton);
157            //   }
158        }
159    }
160
161    protected void do_backupButton_actionPerformed(ActionEvent e) {
162        // Do the backup of the files...
163        String setName = null;
164
165        try {
166            log.debug("backup button activated");
167
168            setName = setNameTextField.getText();
169
170            if (!OperationsXml.checkFileName(setName)) { // NOI18N
171                log.error("Back up set name must not contain reserved characters");
172                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("NameResChar") + "\n" // NOI18N
173                        + Bundle.getMessage("ReservedChar"), Bundle.getMessage("CanNotUseName"),
174                        JmriJOptionPane.ERROR_MESSAGE);
175                return;
176            }
177
178            // check to see if files are dirty
179            if (OperationsXml.areFilesDirty()) {
180                if (Setup.isAutoSaveEnabled()) {
181                    OperationsXml.save();
182                }
183                else if (JmriJOptionPane
184                        .showConfirmDialog(
185                                this,
186                                Bundle.getMessage("OperationsFilesModified"),
187                                Bundle.getMessage("SaveOperationFiles"),
188                                JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) {
189                    OperationsXml.save();
190                }
191            }
192
193            // check to see if directory already exists
194            if (backup.checkIfBackupSetExists(setName)) {
195                int result = JmriJOptionPane.showConfirmDialog(this, MessageFormat
196                        .format(Bundle.getMessage("DirectoryAreadyExists"),
197                                new Object[]{setName}),
198                        Bundle.getMessage("OverwriteBackupDirectory"),
199                        JmriJOptionPane.OK_CANCEL_OPTION);
200
201                if (result != JmriJOptionPane.OK_OPTION) {
202                    return;
203                }
204            }
205
206            backup.backupFilesToSetName(setName);
207            dispose();
208        } catch (IOException ex) {
209            ExceptionContext context = new ExceptionContext(
210                    ex,
211                    Bundle.getMessage("BackupDialog.BackingUp") + " " + setName,
212                    Bundle.getMessage("BackupDialog.Ensure"));
213            ExceptionDisplayFrame.displayExceptionDisplayFrame(this, context);
214
215        } catch (RuntimeException ex) {
216            log.error("Doing backup...", ex);
217
218            UnexpectedExceptionContext context = new UnexpectedExceptionContext(
219                    ex, Bundle.getMessage("BackupDialog.BackingUp") + " " + setName);
220
221            ExceptionDisplayFrame.displayExceptionDisplayFrame(this, context);
222        } catch (Exception ex) {
223            log.error("Doing backup...", ex);
224
225            UnexpectedExceptionContext context = new UnexpectedExceptionContext(
226                    ex, Bundle.getMessage("BackupDialog.BackingUp") + " " + setName);
227
228            ExceptionDisplayFrame.displayExceptionDisplayFrame(this, context);
229        }
230    }
231
232    protected void do_cancelButton_actionPerformed(ActionEvent arg0) {
233        dispose();
234    }
235
236    private void enableBackupButton() {
237        // Enable only if we have something in the text field.
238        // Still need to check for a string of blanks......
239        String s = setNameTextField.getText();
240        backupButton.setEnabled(s.length() > 0);
241    }
242
243    protected void do_helpButton_actionPerformed(ActionEvent e) {
244        // Not implemented yet.
245    }
246
247    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BackupDialog.class);
248
249}