1 package edu.jiangxin.apktoolbox.file.checksum.panel; 2 3 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel; 4 import edu.jiangxin.apktoolbox.utils.Constants; 5 import org.apache.commons.lang3.StringUtils; 6 7 import javax.swing.*; 8 import java.awt.*; 9 10 public class VerifyChecksumPanel extends EasyChildTabbedPanel { 11 private JPanel fileSumPanel; 12 private JTextArea fileSumTextArea; 13 14 private JPanel compareSumPanel; 15 private JTextArea compareSumTextArea; 16 17 private JPanel operationPanel; 18 19 private JTextField resultTextField; 20 21 22 @Override 23 public void createUI() { 24 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS); 25 setLayout(boxLayout); 26 27 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 28 29 createFileSumPanel(); 30 add(fileSumPanel); 31 32 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 33 34 createCompareSumPanel(); 35 add(compareSumPanel); 36 37 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 38 39 createOperationPanel(); 40 add(operationPanel); 41 } 42 43 private void createFileSumPanel() { 44 fileSumPanel = new JPanel(); 45 BoxLayout boxLayout = new BoxLayout(fileSumPanel, BoxLayout.Y_AXIS); 46 fileSumPanel.setLayout(boxLayout); 47 48 JLabel fileSumLabel = new JLabel("Paste calculated file summary from \"File Checksum\" tabbed panel here:"); 49 fileSumTextArea = new JTextArea(5, 4); 50 fileSumTextArea.setLineWrap(true); 51 JScrollPane fileSumScrollPanel = new JScrollPane(fileSumTextArea); 52 53 fileSumPanel.add(fileSumLabel); 54 fileSumPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 55 fileSumPanel.add(fileSumScrollPanel); 56 } 57 58 private void createCompareSumPanel() { 59 compareSumPanel = new JPanel(); 60 BoxLayout boxLayout = new BoxLayout(compareSumPanel, BoxLayout.Y_AXIS); 61 compareSumPanel.setLayout(boxLayout); 62 63 JLabel compareSumLabel = new JLabel("Paste your compared file summary here:"); 64 compareSumTextArea = new JTextArea(5, 4); 65 compareSumTextArea.setLineWrap(true); 66 JScrollPane compareSumScrollPanel = new JScrollPane(compareSumTextArea); 67 68 compareSumPanel.add(compareSumLabel); 69 compareSumPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 70 compareSumPanel.add(compareSumScrollPanel); 71 } 72 73 private void createOperationPanel() { 74 operationPanel = new JPanel(); 75 76 JButton compareButton = new JButton("Compare"); 77 compareButton.setFocusPainted(false); 78 compareButton.addActionListener(event -> { 79 String fileSum = fileSumTextArea.getText(); 80 if (!isValidSummary(fileSum)) { 81 resultTextField.setText("File summary is not valid"); 82 resultTextField.setForeground(Color.YELLOW); 83 return; 84 } 85 86 String compareSum = compareSumTextArea.getText(); 87 if (!isValidSummary(compareSum)) { 88 resultTextField.setText("Compared summary is not valid"); 89 resultTextField.setForeground(Color.YELLOW); 90 return; 91 } 92 93 if (StringUtils.equalsIgnoreCase(fileSum, compareSum)) { 94 resultTextField.setText("File summary is same with compared summary"); 95 resultTextField.setForeground(Color.GREEN); 96 return; 97 } 98 99 resultTextField.setText("File summary is different from compared summary"); 100 resultTextField.setForeground(Color.RED); 101 }); 102 103 resultTextField = new JTextField("To be comparing"); 104 resultTextField.setPreferredSize(new Dimension(100, 25)); 105 resultTextField.setEditable(false); 106 resultTextField.setBorder(null); 107 Font font = new Font(null, Font.BOLD, 16); 108 resultTextField.setFont(font); 109 resultTextField.setForeground(Color.YELLOW); 110 111 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS)); 112 operationPanel.add(compareButton); 113 operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 114 operationPanel.add(resultTextField); 115 operationPanel.add(Box.createHorizontalGlue()); 116 } 117 118 private boolean isValidSummary(String summary) { 119 if (StringUtils.isEmpty(summary)) { 120 return false; 121 } 122 return StringUtils.containsOnly(summary, "0123456789ABCDEFabcdef"); 123 } 124 } 125 126