Siba/src/org/dclermonte/siba/gui/BackupPanel.java

186 lines
6.4 KiB
Java

/*
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
* Didier Clermonté <dclermonte@april.org>
*
* This file is part of Siba.
*
* Siba is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dclermonte.siba.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.apache.commons.compress.archivers.ArchiveException;
import org.dclermonte.siba.model.SibaManager;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
public class BackupPanel extends JPanel
{
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
private JTextField textField;
public JTextField textField_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Create the panel.
*/
public BackupPanel()
{
setLayout(new BorderLayout(0, 0));
JPanel panel_4 = new JPanel();
add(panel_4, BorderLayout.NORTH);
JLabel lblNewLabel_2 = new JLabel(BUNDLE.getString("BackupPanel.lblNewLabel_2.text")); //$NON-NLS-1$
panel_4.add(lblNewLabel_2);
JLabel lblLeRepertoire = new JLabel(BUNDLE.getString("BackupPanel.lblLeRepertoire.text")); //$NON-NLS-1$
panel_4.add(lblLeRepertoire);
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new FormLayout(
new ColumnSpec[] { FormSpecs.RELATED_GAP_COLSPEC, FormSpecs.DEFAULT_COLSPEC,
FormSpecs.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormSpecs.RELATED_GAP_COLSPEC,
FormSpecs.DEFAULT_COLSPEC, FormSpecs.RELATED_GAP_COLSPEC, },
new RowSpec[] { FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, }));
JLabel lblSourceDirectory = new JLabel(BUNDLE.getString("BackupPanel.lblSourceDirectory.text")); //$NON-NLS-1$
panel.add(lblSourceDirectory, "2, 2");
lblSourceDirectory.setHorizontalAlignment(SwingConstants.LEFT);
lblSourceDirectory.setVerticalAlignment(SwingConstants.TOP);
this.textField = new JTextField();
panel.add(this.textField, "4, 2");
this.textField.setColumns(10);
JButton btnNewButton = new JButton(BUNDLE.getString("BackupPanel.btnNewButton.text")); //$NON-NLS-1$
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showDialog(BackupPanel.this,
BUNDLE.getString("BackupPanel.sourceFileChooserButton.text"));
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
BackupPanel.this.textField.setText(file.getPath());
}
}
});
panel.add(btnNewButton, "6, 2");
JLabel lblTargetDirectory = new JLabel(BUNDLE.getString("BackupPanel.lblTargetDirectory.text")); //$NON-NLS-1$
panel.add(lblTargetDirectory, "2, 4");
lblTargetDirectory.setVerticalAlignment(SwingConstants.TOP);
lblTargetDirectory.setHorizontalAlignment(SwingConstants.LEFT);
this.textField_1 = new JTextField();
panel.add(this.textField_1, "4, 4");
this.textField_1.setColumns(10);
JButton btnNewButton_1 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_1.text")); //$NON-NLS-1$
btnNewButton_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
final JFileChooser fc1 = new JFileChooser();
fc1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc1.showDialog(BackupPanel.this,
BUNDLE.getString("BackupPanel.targetFileChooserButton.text"));
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc1.getSelectedFile();
BackupPanel.this.textField_1.setText(file.getPath());
}
}
});
panel.add(btnNewButton_1, "6, 4");
btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel panel_2 = new JPanel();
add(panel_2, BorderLayout.SOUTH);
panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
Component verticalStrut_1 = Box.createVerticalStrut(5);
panel_2.add(verticalStrut_1);
JPanel panel_1 = new JPanel();
panel_2.add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
Component horizontalGlue = Box.createHorizontalGlue();
panel_1.add(horizontalGlue);
JButton btnNewButton_2 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_2.text")); //$NON-NLS-1$
panel_1.add(btnNewButton_2);
Component horizontalGlue_1 = Box.createHorizontalGlue();
panel_1.add(horizontalGlue_1);
Component verticalStrut = Box.createVerticalStrut(20);
panel_2.add(verticalStrut);
btnNewButton_2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
try
{
File output1 = SibaManager.backup(new File(BackupPanel.this.textField.getText()),
BackupPanel.this.textField_1.getText());
String message = "Votre répertoire a été sauvegardé \n dans le fichier :\n " + output1.getName();
JOptionPane.showMessageDialog(null, message, "Sauvegarde faite", JOptionPane.INFORMATION_MESSAGE);
}
catch (ArchiveException | IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
}