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

175 lines
4.6 KiB
Java
Raw Normal View History

2016-03-29 23:25:18 +02:00
package org.dclermonte.siba.gui;
2016-04-09 11:33:15 +02:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2016-03-29 23:25:18 +02:00
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
2016-04-07 00:54:03 +02:00
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
2016-04-09 11:33:15 +02:00
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
2016-03-29 23:25:18 +02:00
import com.jgoodies.forms.layout.ColumnSpec;
2016-04-09 11:33:15 +02:00
import com.jgoodies.forms.layout.FormLayout;
2016-03-29 23:25:18 +02:00
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
2016-04-09 11:33:15 +02:00
/**
*
* @author papa
*/
2016-03-29 23:25:18 +02:00
public class CheckPanel extends JPanel
{
2016-04-09 11:33:15 +02:00
private static final long serialVersionUID = -1580485684838045920L;
2016-03-29 23:25:18 +02:00
private JTextField textField;
/**
* Create the panel.
*/
public CheckPanel()
{
2016-04-09 11:33:15 +02:00
setLayout(new FormLayout(
new ColumnSpec[] { ColumnSpec.decode("2dlu"),
new ColumnSpec(ColumnSpec.LEFT,
Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true),
Sizes.constant("60dlu", true)),
1),
new ColumnSpec(ColumnSpec.LEFT,
Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true),
Sizes.constant("100dlu", true)),
1),
ColumnSpec.decode("30dlu"), },
new RowSpec[] { FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("25px"), FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC, }));
2016-03-29 23:25:18 +02:00
JLabel lblNewLabel = new JLabel("Sauvegarde à vérifier");
add(lblNewLabel, "2, 2, left, center");
2016-04-09 11:33:15 +02:00
2016-03-29 23:25:18 +02:00
JButton btnNewButton = new JButton("...");
2016-04-09 11:33:15 +02:00
btnNewButton.addMouseListener(new MouseAdapter()
{
2016-03-29 23:25:18 +02:00
@Override
2016-04-09 11:33:15 +02:00
public void mouseClicked(final MouseEvent e)
{
2016-04-07 00:54:03 +02:00
try
{
repertoireChoisi();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
2016-03-29 23:25:18 +02:00
}
});
2016-04-09 11:33:15 +02:00
this.textField = new JTextField();
add(this.textField, "3, 2, fill, default");
this.textField.setColumns(10);
2016-03-29 23:25:18 +02:00
add(btnNewButton, "4, 2, left, top");
2016-04-09 11:33:15 +02:00
2016-03-29 23:25:18 +02:00
JButton btnNewButton_1 = new JButton("Check");
2016-04-09 11:33:15 +02:00
btnNewButton_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent arg0)
{
try
{
check();
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
2016-03-29 23:25:18 +02:00
add(btnNewButton_1, "3, 4");
}
2016-04-09 11:33:15 +02:00
final void check() throws IOException, NoSuchAlgorithmException
{
File file = new File(this.textField.getText());
FileReader fileReader = new FileReader(file);
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
char[] fileToCheck = new char[(int) (file.length()) - 32];
for (int index = 0; index < 32; index++)
{
md5[index] = (char) fileReader.read();
md5byte[index] = (byte) md5[index];
}
fileReader.read();
String fileNameToString = new String();
for (int index = 36; index < (file.length() + 3); index++)
{
fileToCheck[index - 36] = (char) fileReader.read();
}
fileNameToString = String.copyValueOf(fileToCheck);
fileReader.close();
byte[] bytedirectoryToSave = new byte[fileToCheck.length];
FileInputStream fileInputStream = new FileInputStream(file);
for (int index1 = 0; index1 < fileNameToString.length(); index1++)
{
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
}
fileInputStream.close();
byte[] hash = null;
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
if (MessageDigest.isEqual(hash, md5byte))
{
System.out.println("gagné");
}
else
{
System.out.println("Perdu");
}
}
/**
*
* @throws IOException
* @throws NoSuchAlgorithmException
*/
final void repertoireChoisi() throws IOException, NoSuchAlgorithmException
{
2016-03-29 23:25:18 +02:00
final JFileChooser fc = new JFileChooser();
2016-04-09 11:33:15 +02:00
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers md5", "md5");
fc.setAcceptAllFileFilterUsed(false);
2016-04-07 00:54:03 +02:00
fc.addChoosableFileFilter(filter);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
2016-04-09 11:33:15 +02:00
int returnVal = fc.showDialog(CheckPanel.this, "Sauvegarde à vérifier");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
2016-03-29 23:25:18 +02:00
File file = fc.getSelectedFile();
2016-04-09 11:33:15 +02:00
this.textField.setText(file.getName());
2016-03-29 23:25:18 +02:00
}
}
2016-04-09 11:33:15 +02:00
}