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