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