packagecom.my.ui;importcom.my.bean.User;importcom.my.service.EncryptService;importcom.my.service.PersistenceService;importcom.my.util.Iconst;import javax.swing.*;import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;public class Main extends JFrame implementsActionListener {//保存服务对象
privateUser user;privateEncryptService encryptService;
PersistenceService persistenceService;privateEncryptedFileManagement encryptedFileManagement;//窗体默认大小
private static final int DEFAULT_WIDTH = 416;private static final int DEFAULT_HEIGHT = 145;//全局组件
privateJFileChooser fileChooser;privateJButton buttonEncrypt;privateJButton buttonDecrypt;privateJButton buttonMakeNewKey;privateJButton buttonEncryptedFileManagement;privateJButton buttonExit;privateJTextField textFilePath;privateJTextField textKeyName;//保存当前文件、密匙路径
privateString filePath;privateString keyPath;public voidsetFilePath(String filePath) {this.filePath =filePath;
}public voidsetKeyPath(String keyPath) {this.keyPath =keyPath;
}//窗体初始化
publicMain(User user) {//初始化用户和服务
this.user =user;
encryptService= new EncryptService(this.user);
persistenceService= new PersistenceService(this.user.getUsername());
persistenceService.loadData(this.user);
encryptedFileManagement= new EncryptedFileManagement(this.user);
encryptedFileManagement.setMainFrame(this);//初始化界面
setTitle("用户ID:" +user.getUsername());
setDefaultCloseOperation(0);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
JPanel bigPanel= newJPanel();//布置主要界面
textFilePath = newJTextField();
textKeyName= newJTextField();
JButton buttonChooseFile= newJButton(Iconst.CHOOSE_FILE);
JButton buttonChooseKey= newJButton(Iconst.CHOOSE_KEY);
textFilePath.setEditable(false);
textKeyName.setEditable(false);
buttonChooseKey.setFocusPainted(false);
buttonChooseFile.setFocusPainted(false);
bigPanel.setLayout(new GridLayout(2, 3));
bigPanel.add(new JLabel("文件路径"));
bigPanel.add(textFilePath);
bigPanel.add(buttonChooseFile);
bigPanel.add(new JLabel("密匙名称"));
bigPanel.add(textKeyName);
bigPanel.add(buttonChooseKey);
JPanel buttonPanel= newJPanel();//布置按钮界面
buttonEncrypt = newJButton(Iconst.ENCRYPT);
buttonDecrypt= newJButton(Iconst.DECRYPT);
buttonMakeNewKey= newJButton(Iconst.CREATE_NEW_KEY);
buttonEncryptedFileManagement= newJButton(Iconst.ENCRYPTED_FILE_MANAGEMENT);
buttonExit= newJButton(Iconst.EXIT);
buttonEncrypt.setFocusPainted(false);
buttonDecrypt.setFocusPainted(false);
buttonMakeNewKey.setFocusPainted(false);
buttonEncryptedFileManagement.setFocusPainted(false);
buttonExit.setFocusPainted(false);
buttonPanel.setLayout(newFlowLayout());
buttonPanel.add(buttonEncrypt);
buttonPanel.add(buttonDecrypt);
buttonPanel.add(buttonMakeNewKey);
buttonPanel.add(buttonEncryptedFileManagement);
buttonPanel.add(buttonExit);//布置窗体
setLayout(newBorderLayout());
add(bigPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);//注册事件监听
buttonChooseFile.addActionListener(this);
buttonChooseKey.addActionListener(this);
buttonMakeNewKey.addActionListener(this);
buttonEncrypt.addActionListener(this);
buttonDecrypt.addActionListener(this);
buttonEncryptedFileManagement.addActionListener(this);
buttonExit.addActionListener(this);//创建文件选择器
fileChooser = newJFileChooser();
}
@Overridepublic voidactionPerformed(ActionEvent e) {if (e.getActionCommand().charAt(0) == "■".charAt(0)) {
String filePath= e.getActionCommand().substring(1, e.getActionCommand().length());
setFilePath(filePath);
textFilePath.setText(filePath);
}if (e.getActionCommand().charAt(0) == "▲".charAt(0)) {
String keyFullName= e.getActionCommand().substring(1, e.getActionCommand().length());
setKeyPath(".//user//" + user.getUsername() + "//" +keyFullName);
textKeyName.setText(keyFullName);
}if(e.getActionCommand().equals(Iconst.CHOOSE_FILE)) {
fileChooser.setCurrentDirectory(new File("."));int result = fileChooser.showOpenDialog(null);if (result ==JFileChooser.APPROVE_OPTION) {
filePath=fileChooser.getSelectedFile().getPath();
textFilePath.setText(filePath);
}
}if(e.getActionCommand().equals(Iconst.CHOOSE_KEY)) {
fileChooser.setCurrentDirectory(new File(".//user//" +user.getUsername()));int result = fileChooser.showOpenDialog(null);if (result ==JFileChooser.APPROVE_OPTION) {
keyPath=fileChooser.getSelectedFile().getPath();
textKeyName.setText(newFile(keyPath).getName());
}
}if(e.getActionCommand().equals(Iconst.ENCRYPT)) {if (filePath != null && !"".equals(filePath) && textKeyName.getText() != null && !"".equals(textKeyName.getText())) {
encryptService.encryptFile(filePath, textKeyName.getText());
encryptedFileManagement.addEncryptedFile(filePath, textKeyName.getText());
JOptionPane.showMessageDialog(this, "加密成功");
}else{
JOptionPane.showMessageDialog(this, "文件路径、密匙名称不能为空!");
}
}if(e.getActionCommand().equals(Iconst.DECRYPT)) {if (filePath != null && !"".equals(filePath) && textKeyName.getText() != null && !"".equals(textKeyName.getText()) ) {if(encryptedFileManagement.removeEncryptedFile(filePath, textKeyName.getText())) {
encryptService.decryptFile(filePath, textKeyName.getText());
JOptionPane.showMessageDialog(this, "还原成功");
}else{
JOptionPane.showMessageDialog(this, "该文件未被加密或密匙不匹配");
}
}else{
JOptionPane.showMessageDialog(this, "文件路径、密匙名称不能为空!");
}
}if(e.getActionCommand().equals(Iconst.CREATE_NEW_KEY)) {
String keyName= JOptionPane.showInputDialog(this, "请输入新密匙的名称:");if (keyName != null && !"".equals(keyName)) {
encryptService.makeKey(keyName);
JOptionPane.showMessageDialog(this, "成功创建新的密匙");
}//更新当前密匙路径
setKeyPath(".//user//" + user.getUsername() + "//" + keyName + ".key");
textKeyName.setText(keyName+ ".key");
}if(e.getActionCommand().equals(Iconst.ENCRYPTED_FILE_MANAGEMENT)) {if (user.getEncryptedFileList().size() == 0) {
JOptionPane.showMessageDialog(this, "加密文件为空");
}else{
encryptedFileManagement.setVisible(true);
}
}if(e.getActionCommand().equals(Iconst.EXIT)) {if (JOptionPane.showConfirmDialog(this,"确定退出?") == 0) {
persistenceService.saveData(user);this.dispose();
System.exit(0);
}
}
}
}classMainTest {public static voidmain(String[] args) {
User user= newUser();
user.setUsername("xkfx");
EventQueue.invokeLater(()->{
JFrame frame= newMain(user);
frame.setVisible(true);
});
}
}