001package jmri.jmrit.operations.setup.backup;
002
003import java.awt.*;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import java.util.List;
007
008import javax.swing.*;
009import javax.swing.border.EmptyBorder;
010import javax.swing.event.ListSelectionEvent;
011import javax.swing.event.ListSelectionListener;
012
013import jmri.InstanceManager;
014import jmri.util.swing.JmriJOptionPane;
015
016public class ManageBackupsDialog extends JDialog {
017
018    private final JPanel contentPanel = new JPanel();
019    private JLabel selectBackupSetsLabel;
020    private JButton selectAllButton;
021    private JButton clearAllButton;
022    private JScrollPane scrollPane;
023    private JList<BackupSet> setList;
024
025    private JButton deleteButton;
026    // private JButton helpButton;
027
028    private DefaultListModel<BackupSet> model;
029
030    private AutoBackup backup;
031    private Component horizontalGlue;
032    private Component horizontalStrut;
033    private Component horizontalStrut_1;
034    private Component horizontalStrut_2;
035
036    /**
037     * Create the dialog.
038     */
039    public ManageBackupsDialog() {
040        // For now we only support Autobackups, but this can be updated later if
041        // needed.
042        backup = InstanceManager.getDefault(AutoBackup.class);
043
044        initComponents();
045    }
046
047    private void initComponents() {
048        setModalityType(ModalityType.DOCUMENT_MODAL);
049        setModal(true);
050        setTitle(Bundle.getMessage("ManageBackupsDialog.manageBackupSets"));
051        setBounds(100, 100, 461, 431);
052        BorderLayout borderLayout = new BorderLayout();
053        borderLayout.setVgap(5);
054        borderLayout.setHgap(5);
055        getContentPane().setLayout(borderLayout);
056        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
057        getContentPane().add(contentPanel, BorderLayout.CENTER);
058        GridBagLayout gbl_contentPanel = new GridBagLayout();
059        gbl_contentPanel.columnWidths = new int[]{0, 0};
060        gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
061        gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
062        gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
063        contentPanel.setLayout(gbl_contentPanel);
064        {
065            selectBackupSetsLabel = new JLabel(Bundle.getMessage("ManageBackupsDialog.instructionsLabel.text"));
066            GridBagConstraints gbc_selectBackupSetsLabel = new GridBagConstraints();
067            gbc_selectBackupSetsLabel.anchor = GridBagConstraints.WEST;
068            gbc_selectBackupSetsLabel.insets = new Insets(0, 0, 5, 0);
069            gbc_selectBackupSetsLabel.gridx = 0;
070            gbc_selectBackupSetsLabel.gridy = 0;
071            contentPanel.add(selectBackupSetsLabel, gbc_selectBackupSetsLabel);
072        }
073        {
074            scrollPane = new JScrollPane();
075            GridBagConstraints gbc_scrollPane = new GridBagConstraints();
076            gbc_scrollPane.fill = GridBagConstraints.BOTH;
077            gbc_scrollPane.gridx = 0;
078            gbc_scrollPane.gridy = 1;
079            contentPanel.add(scrollPane, gbc_scrollPane);
080            {
081                setList = new JList<BackupSet>();
082                setList.setVisibleRowCount(20);
083
084                model = new DefaultListModel<BackupSet>();
085
086                // Load up the list control with the available BackupSets
087                for (BackupSet bs : backup.getBackupSets()) {
088                    model.addElement(bs);
089                }
090
091                setList.setModel(model);
092
093                // Update button states based on if anything is selected in the
094                // list.
095                setList.addListSelectionListener(new ListSelectionListener() {
096                    @Override
097                    public void valueChanged(ListSelectionEvent e) {
098                        updateButtonStates();
099                    }
100                });
101                scrollPane.setViewportView(setList);
102            }
103        }
104        {
105            JPanel buttonPane = new JPanel();
106            buttonPane.setBorder(new EmptyBorder(5, 5, 5, 5));
107            buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
108            getContentPane().add(buttonPane, BorderLayout.SOUTH);
109            {
110                selectAllButton = new JButton(Bundle.getMessage("ManageBackupsDialog.selectAllButton.text"));
111                selectAllButton.addActionListener(new ActionListener() {
112                    @Override
113                    public void actionPerformed(ActionEvent e) {
114                        do_selectAllButton_actionPerformed(e);
115                    }
116                });
117                buttonPane.add(selectAllButton);
118            }
119            {
120                horizontalStrut = Box.createHorizontalStrut(10);
121                buttonPane.add(horizontalStrut);
122            }
123            {
124                clearAllButton = new JButton(Bundle.getMessage("ManageBackupsDialog.clearAllButton.text"));
125                clearAllButton.addActionListener(new ActionListener() {
126                    @Override
127                    public void actionPerformed(ActionEvent e) {
128                        do_clearAllButton_actionPerformed(e);
129                    }
130                });
131                buttonPane.add(clearAllButton);
132            }
133            {
134                horizontalGlue = Box.createHorizontalGlue();
135                buttonPane.add(horizontalGlue);
136            }
137            {
138                deleteButton = new JButton(Bundle.getMessage("ButtonDelete"));
139                deleteButton.addActionListener(new ActionListener() {
140                    @Override
141                    public void actionPerformed(ActionEvent e) {
142                        do_deleteButton_actionPerformed(e);
143                    }
144                });
145                deleteButton.setActionCommand("");
146                buttonPane.add(deleteButton);
147            }
148            {
149                horizontalStrut_1 = Box.createHorizontalStrut(10);
150                buttonPane.add(horizontalStrut_1);
151            }
152            {
153                JButton closeButton = new JButton(Bundle.getMessage("ButtonCancel"));
154                closeButton.addActionListener(new ActionListener() {
155                    @Override
156                    public void actionPerformed(ActionEvent e) {
157                        do_cancelButton_actionPerformed(e);
158                    }
159                });
160                closeButton.setActionCommand("Cancel"); // NOI18N
161                getRootPane().setDefaultButton(closeButton);
162                buttonPane.add(closeButton);
163            }
164            {
165                horizontalStrut_2 = Box.createHorizontalStrut(10);
166                buttonPane.add(horizontalStrut_2);
167            }
168            // {
169            // helpButton = new JButton(Bundle.getMessage("BackupDialog.helpButton.text"));
170            // helpButton.addActionListener(new ActionListener() {
171            // public void actionPerformed(ActionEvent e) {
172            // do_helpButton_actionPerformed(e);
173            // }
174            // });
175            // helpButton.setEnabled(false);
176            // buttonPane.add(helpButton);
177            // }
178        }
179
180        updateButtonStates();
181    }
182
183    protected void do_cancelButton_actionPerformed(ActionEvent e) {
184        dispose();
185    }
186
187    protected void do_clearAllButton_actionPerformed(ActionEvent e) {
188        setList.clearSelection();
189    }
190
191    protected void do_deleteButton_actionPerformed(ActionEvent e) {
192        // Here we get the selected items from the list
193        List<BackupSet> selectedSets = setList.getSelectedValuesList();
194
195        if (selectedSets.size() > 0) {
196            // Make sure OK to delete backups
197            int result = JmriJOptionPane.showConfirmDialog(this, String.format(Bundle.getMessage("ManageBackupsDialog.aboutToDelete"), selectedSets.size()), 
198                    Bundle.getMessage("ManageBackupsDialog.deletingBackupSets"), JmriJOptionPane.OK_CANCEL_OPTION);
199
200            if (result == JmriJOptionPane.OK_OPTION) {
201                for (BackupSet set : selectedSets) {
202                    model.removeElement(set);
203
204                    // For now, the BackupSet deletes the associated files, but
205                    // we might want to move this into the BackupBase class just
206                    // so that it knows what is happening.
207                    set.delete();
208                }
209            }
210        }
211    }
212
213    protected void do_helpButton_actionPerformed(ActionEvent e) {
214        // Not implemented yet.
215    }
216
217    protected void do_selectAllButton_actionPerformed(ActionEvent e) {
218        setList.setSelectionInterval(0, model.getSize() - 1);
219    }
220
221    private void updateButtonStates() {
222        // Update the various button enabled states based on what is in the list
223        // and what is selected.
224        boolean notEmpty = !setList.isSelectionEmpty();
225
226        deleteButton.setEnabled(notEmpty);
227        clearAllButton.setEnabled(notEmpty);
228
229        // Can only select if we have something to select!
230        int count = model.size();
231        selectAllButton.setEnabled(count > 0);
232    }
233}