You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.8 KiB
101 lines
3.8 KiB
package com.fr.password.tool.ui; |
|
|
|
import cn.hutool.core.io.FileUtil; |
|
import cn.hutool.core.util.StrUtil; |
|
import com.fr.password.tool.factory.SecretHelper; |
|
|
|
import javax.swing.*; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.io.File; |
|
|
|
public class FilePanel extends JPanel { |
|
public final static FilePanel INSTANCE = new FilePanel(); |
|
|
|
private JLabel fileLabel = new JLabel(); |
|
private JTextField filePathField = new JTextField(); |
|
private JButton chooseFileButton = new JButton(); |
|
|
|
private EventHandler eventHandler = new EventHandler(); |
|
|
|
private FilePanel() { |
|
chooseFileButton.addActionListener(eventHandler); |
|
|
|
fileLabel.setText(FrameConstants.SAVE_KEY); |
|
add(fileLabel); |
|
|
|
filePathField.setColumns(28); |
|
add(filePathField); |
|
|
|
chooseFileButton.setText(FrameConstants.CHOOSE); |
|
add(chooseFileButton); |
|
} |
|
|
|
public void hide() { |
|
this.fileLabel.setVisible(false); |
|
this.filePathField.setVisible(false); |
|
this.chooseFileButton.setVisible(false); |
|
} |
|
|
|
public void setLeftLabelText(String text) { |
|
this.fileLabel.setText(text); |
|
} |
|
|
|
public void setButtonText(String text) { |
|
this.chooseFileButton.setText(text); |
|
} |
|
|
|
public String getFilePath(){ |
|
return filePathField.getText(); |
|
} |
|
|
|
|
|
class EventHandler implements ActionListener { |
|
|
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
if (!TopPanel.INSTANCE.isChooseEncryption()) { |
|
return; |
|
} |
|
String publicKey = StrUtil.EMPTY; |
|
String privateKey = StrUtil.EMPTY; |
|
JFileChooser fileChooser = new JFileChooser(); |
|
fileChooser.setVisible(true); |
|
// 保存密钥文件 |
|
if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.SAVE_KEY)) { |
|
if (StrUtil.isEmpty(TextPanel.INSTANCE.getLeftText()) || StrUtil.isEmpty(TextPanel.INSTANCE.getRightText())) { |
|
return; |
|
} |
|
fileChooser.setApproveButtonText(FrameConstants.SAVE); |
|
fileChooser.setDialogTitle(FrameConstants.CHOOSE_SAVE_DIR); |
|
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
|
publicKey = TextPanel.INSTANCE.getLeftText(); |
|
privateKey = TextPanel.INSTANCE.getRightText(); |
|
String content = StrUtil.format("{}\r\n{}\r\n{}\r\n{}\r\n{}\r\n{}", |
|
SecretHelper.BEGIN_PUBLIC_KEY, publicKey, SecretHelper.END_PUBLIC_KEY, |
|
SecretHelper.BEGIN_PRIVATE_KEY, privateKey, SecretHelper.END_PRIVATE_KEY); |
|
int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); |
|
if (result == JFileChooser.APPROVE_OPTION) { |
|
String dirPath = fileChooser.getSelectedFile().getAbsolutePath(); |
|
String keyFilePath = StrUtil.format("{}{}{}", dirPath, File.separator, SecretHelper.KEY_FILE_NAME); |
|
filePathField.setText(keyFilePath); |
|
FileUtil.writeUtf8String(content, keyFilePath); |
|
} |
|
} |
|
|
|
// 选择密钥文件 |
|
if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.CHOOSE_FILE)) { |
|
fileChooser.setApproveButtonText(FrameConstants.CONFIRM); |
|
fileChooser.setDialogTitle(FrameConstants.CHOOSE_KEY_DIR); |
|
int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); |
|
if (result == JFileChooser.APPROVE_OPTION) { |
|
String keyFilePath = fileChooser.getSelectedFile().getAbsolutePath(); |
|
filePathField.setText(keyFilePath); |
|
// TODO: 密钥正确性检测 |
|
} |
|
} |
|
|
|
|
|
} |
|
} |
|
}
|
|
|