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

215 lines
7.0 KiB
Java

/*
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
*
* 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.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ResourceBundle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.dclermonte.siba.SibaException;
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;
/**
*
* This class is the panel for backup.
*
* @author dclermonte
*
*/
public class BackupPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 4714383090458639282L;
/**
*
*/
static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
private JTextField textField;
private JTextField textField_1;
/**
* This is the constructor for this panel.
*/
public BackupPanel() throws SibaException
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
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);
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.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
File file = SourceDirectorySelector.showSelectorDialog(null, null);
if (file == null)
{
return;
}
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.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnNewButton_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
File file = TargetDirectorySelector.showSelectorDialog(null, null);
if (file == null)
{
return;
}
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$
btnNewButton_2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
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 directoryToSave1 = new File(BackupPanel.this.textField.getText());
File targetDirectory = new File(BackupPanel.this.textField_1.getText());
if (directoryToSave1.exists() && targetDirectory.exists())
{
File output1 = SibaManager.backup(directoryToSave1, targetDirectory);
String message = BUNDLE.getString("BackupPanel.confirmDialogMessage.text") + output1.getName();
String titre = BUNDLE.getString("BackupPanel.confirmDialogTitle.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
}
else
{
String dataMissing = BUNDLE.getString("BackupPanel.dataMissing.text");
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
}
}
catch (SibaException sibaException)
{
String dataMissing = sibaException.getMessage();
if (!(sibaException.getCause() == null))
{
dataMissing.concat(sibaException.getCause().getMessage());
}
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}