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