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

215 lines
7.0 KiB
Java
Raw Normal View History

2016-04-19 22:34:25 +02:00
/*
2016-04-26 23:02:26 +02:00
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
2016-04-19 22:34:25 +02:00
* 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/>.
*/
2016-03-29 23:25:18 +02:00
package org.dclermonte.siba.gui;
2016-04-19 22:34:25 +02:00
import java.awt.BorderLayout;
import java.awt.Component;
2016-04-26 23:02:26 +02:00
import java.awt.Cursor;
2016-03-29 23:25:18 +02:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
2016-04-19 22:34:25 +02:00
import java.util.ResourceBundle;
2016-03-29 23:25:18 +02:00
2016-04-19 22:34:25 +02:00
import javax.swing.Box;
import javax.swing.BoxLayout;
2016-03-29 23:25:18 +02:00
import javax.swing.JButton;
import javax.swing.JLabel;
2016-04-19 22:34:25 +02:00
import javax.swing.JOptionPane;
2016-03-29 23:25:18 +02:00
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
2016-05-19 18:40:00 +02:00
import org.dclermonte.siba.SibaException;
2016-04-19 22:34:25 +02:00
import org.dclermonte.siba.model.SibaManager;
2016-03-29 23:25:18 +02:00
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
/**
*
2016-05-19 18:40:00 +02:00
* This class is the panel for backup.
*
2016-05-22 21:47:41 +02:00
* @author dclermonte
*
*/
2016-03-29 23:25:18 +02:00
public class BackupPanel extends JPanel
{
2016-04-26 23:02:26 +02:00
/**
*
*/
private static final long serialVersionUID = 4714383090458639282L;
/**
*
*/
static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
2016-03-29 23:25:18 +02:00
private JTextField textField;
2016-04-26 23:02:26 +02:00
private JTextField textField_1;
2016-03-29 23:25:18 +02:00
/**
2016-05-19 18:40:00 +02:00
* This is the constructor for this panel.
2016-03-29 23:25:18 +02:00
*/
2016-04-19 22:34:25 +02:00
2016-05-19 18:40:00 +02:00
public BackupPanel() throws SibaException
2016-03-29 23:25:18 +02:00
{
2016-04-26 23:02:26 +02:00
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2016-04-07 00:54:03 +02:00
setLayout(new BorderLayout(0, 0));
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JPanel panel_4 = new JPanel();
add(panel_4, BorderLayout.NORTH);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JLabel lblNewLabel_2 = new JLabel(BUNDLE.getString("BackupPanel.lblNewLabel_2.text")); //$NON-NLS-1$
panel_4.add(lblNewLabel_2);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JPanel panel = new JPanel();
add(panel);
2016-04-19 22:34:25 +02:00
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);
2016-04-07 00:54:03 +02:00
JButton btnNewButton = new JButton(BUNDLE.getString("BackupPanel.btnNewButton.text")); //$NON-NLS-1$
2016-04-26 23:02:26 +02:00
btnNewButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-19 22:34:25 +02:00
btnNewButton.addActionListener(new ActionListener()
{
2016-04-07 00:54:03 +02:00
@Override
2016-04-19 22:34:25 +02:00
public void actionPerformed(final ActionEvent e)
{
2016-04-26 23:02:26 +02:00
File file = SourceDirectorySelector.showSelectorDialog(null, null);
if (file == null)
2016-04-19 22:34:25 +02:00
{
2016-04-26 23:02:26 +02:00
return;
2016-04-07 00:54:03 +02:00
}
2016-04-26 23:02:26 +02:00
BackupPanel.this.textField.setText(file.getPath());
2016-04-07 00:54:03 +02:00
}
});
2016-04-19 22:34:25 +02:00
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);
2016-04-07 00:54:03 +02:00
JButton btnNewButton_1 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_1.text")); //$NON-NLS-1$
2016-04-26 23:02:26 +02:00
btnNewButton_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-19 22:34:25 +02:00
btnNewButton_1.addActionListener(new ActionListener()
{
2016-03-29 23:25:18 +02:00
@Override
2016-04-19 22:34:25 +02:00
public void actionPerformed(final ActionEvent e)
{
2016-04-26 23:02:26 +02:00
File file = TargetDirectorySelector.showSelectorDialog(null, null);
if (file == null)
2016-04-19 22:34:25 +02:00
{
2016-04-26 23:02:26 +02:00
return;
2016-03-29 23:25:18 +02:00
}
2016-04-26 23:02:26 +02:00
BackupPanel.this.textField_1.setText(file.getPath());
2016-03-29 23:25:18 +02:00
}
});
2016-04-19 22:34:25 +02:00
panel.add(btnNewButton_1, "6, 4");
2016-04-07 00:54:03 +02:00
btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JPanel panel_2 = new JPanel();
2016-04-26 23:02:26 +02:00
2016-04-07 00:54:03 +02:00
add(panel_2, BorderLayout.SOUTH);
panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
Component verticalStrut_1 = Box.createVerticalStrut(5);
panel_2.add(verticalStrut_1);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JPanel panel_1 = new JPanel();
panel_2.add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
Component horizontalGlue = Box.createHorizontalGlue();
panel_1.add(horizontalGlue);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
JButton btnNewButton_2 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_2.text")); //$NON-NLS-1$
2016-04-26 23:02:26 +02:00
btnNewButton_2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-07 00:54:03 +02:00
panel_1.add(btnNewButton_2);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
Component horizontalGlue_1 = Box.createHorizontalGlue();
panel_1.add(horizontalGlue_1);
2016-04-19 22:34:25 +02:00
2016-04-07 00:54:03 +02:00
Component verticalStrut = Box.createVerticalStrut(20);
panel_2.add(verticalStrut);
2016-04-19 22:34:25 +02:00
btnNewButton_2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
2016-03-29 23:25:18 +02:00
try
{
2016-05-02 22:28:35 +02:00
File directoryToSave1 = new File(BackupPanel.this.textField.getText());
File targetDirectory = new File(BackupPanel.this.textField_1.getText());
if (directoryToSave1.exists() && targetDirectory.exists())
2016-04-26 23:02:26 +02:00
{
2016-05-08 22:43:19 +02:00
File output1 = SibaManager.backup(directoryToSave1, targetDirectory);
2016-05-02 22:28:35 +02:00
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);
2016-04-26 23:02:26 +02:00
}
2016-03-29 23:25:18 +02:00
}
2016-05-25 18:10:34 +02:00
catch (SibaException sibaException)
2016-03-29 23:25:18 +02:00
{
2016-05-25 18:10:34 +02:00
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);
2016-03-29 23:25:18 +02:00
}
2016-05-19 18:40:00 +02:00
2016-03-29 23:25:18 +02:00
}
});
}
}