1 package edu.jiangxin.apktoolbox.file.checksum.panel; 2 3 import edu.jiangxin.apktoolbox.file.checksum.CalculateType; 4 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel; 5 import edu.jiangxin.apktoolbox.swing.extend.filepanel.FilePanel; 6 import edu.jiangxin.apktoolbox.utils.Constants; 7 import org.apache.commons.codec.digest.DigestUtils; 8 import org.apache.commons.io.FileUtils; 9 10 import javax.swing.*; 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.FileNotFoundException; 14 import java.io.IOException; 15 16 public class FileChecksumPanel extends EasyChildTabbedPanel { 17 private static final long serialVersionUID = 63924900336217723L; 18 19 private FilePanel filePanel; 20 21 private JPanel optionPanel; 22 23 private JTextField sizeTextField; 24 private JTextField lastModifiedTimeTextField; 25 private JCheckBox md5CheckBox; 26 private JTextField md5TextField; 27 private JCheckBox sha1CheckBox; 28 private JTextField sha1TextField; 29 private JCheckBox sha256CheckBox; 30 private JTextField sha256TextField; 31 private JCheckBox sha384CheckBox; 32 private JTextField sha384TextField; 33 private JCheckBox sha512CheckBox; 34 private JTextField sha512TextField; 35 private JCheckBox crc32CheckBox; 36 private JTextField crc32TextField; 37 38 private JPanel operationPanel; 39 private JProgressBar progressBar; 40 41 @Override 42 public void createUI() { 43 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS); 44 setLayout(boxLayout); 45 46 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 47 createFileNamePanel(); 48 add(filePanel); 49 50 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 51 createOptionPanel(); 52 add(optionPanel); 53 54 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 55 createOperationPanel(); 56 add(operationPanel); 57 } 58 59 private void createFileNamePanel() { 60 filePanel = new FilePanel("Choose File"); 61 filePanel.setFileReadyCallback(file -> { 62 if (file == null) { 63 logger.error("file is null"); 64 return; 65 } 66 calculate(file); 67 }); 68 } 69 70 private void createOptionPanel() { 71 optionPanel = new JPanel(); 72 BoxLayout boxLayout = new BoxLayout(optionPanel, BoxLayout.Y_AXIS); 73 optionPanel.setLayout(boxLayout); 74 75 JPanel sizeOptionPanel = new JPanel(); 76 JPanel lastModifiedTimeOptionPanel = new JPanel(); 77 JPanel md5OptionPanel = new JPanel(); 78 JPanel sha1OptionPanel = new JPanel(); 79 JPanel sha256OptionPanel = new JPanel(); 80 JPanel sha384OptionPanel = new JPanel(); 81 JPanel sha512OptionPanel = new JPanel(); 82 JPanel crc32OptionPanel = new JPanel(); 83 84 optionPanel.add(sizeOptionPanel); 85 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 86 optionPanel.add(lastModifiedTimeOptionPanel); 87 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 88 optionPanel.add(md5OptionPanel); 89 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 90 optionPanel.add(sha1OptionPanel); 91 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 92 optionPanel.add(sha256OptionPanel); 93 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 94 optionPanel.add(sha384OptionPanel); 95 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 96 optionPanel.add(sha512OptionPanel); 97 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 98 optionPanel.add(crc32OptionPanel); 99 100 sizeOptionPanel.setLayout(new BoxLayout(sizeOptionPanel, BoxLayout.X_AXIS)); 101 JLabel fileSizeLabel = new JLabel("Size:"); 102 sizeTextField = new JTextField(); 103 sizeOptionPanel.add(fileSizeLabel); 104 sizeOptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 105 sizeOptionPanel.add(sizeTextField); 106 107 lastModifiedTimeOptionPanel.setLayout(new BoxLayout(lastModifiedTimeOptionPanel, BoxLayout.X_AXIS)); 108 JLabel fileLastModifiedTimeLabel = new JLabel("Last Modified Time:"); 109 lastModifiedTimeTextField = new JTextField(); 110 lastModifiedTimeOptionPanel.add(fileLastModifiedTimeLabel); 111 lastModifiedTimeOptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 112 lastModifiedTimeOptionPanel.add(lastModifiedTimeTextField); 113 114 md5OptionPanel.setLayout(new BoxLayout(md5OptionPanel, BoxLayout.X_AXIS)); 115 md5CheckBox = new JCheckBox("MD5 checksum:"); 116 md5CheckBox.setSelected(true); 117 md5TextField = new JTextField(); 118 md5OptionPanel.add(md5CheckBox); 119 md5OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 120 md5OptionPanel.add(md5TextField); 121 122 sha1OptionPanel.setLayout(new BoxLayout(sha1OptionPanel, BoxLayout.X_AXIS)); 123 sha1CheckBox = new JCheckBox("SHA1 checksum:"); 124 sha1TextField = new JTextField(); 125 sha1OptionPanel.add(sha1CheckBox); 126 sha1OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 127 sha1OptionPanel.add(sha1TextField); 128 129 sha256OptionPanel.setLayout(new BoxLayout(sha256OptionPanel, BoxLayout.X_AXIS)); 130 sha256CheckBox = new JCheckBox("SHA256 checksum:"); 131 sha256TextField = new JTextField(); 132 sha256OptionPanel.add(sha256CheckBox); 133 sha256OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 134 sha256OptionPanel.add(sha256TextField); 135 136 sha384OptionPanel.setLayout(new BoxLayout(sha384OptionPanel, BoxLayout.X_AXIS)); 137 sha384CheckBox = new JCheckBox("SHA384 checksum:"); 138 sha384TextField = new JTextField(); 139 sha384OptionPanel.add(sha384CheckBox); 140 sha384OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 141 sha384OptionPanel.add(sha384TextField); 142 143 sha512OptionPanel.setLayout(new BoxLayout(sha512OptionPanel, BoxLayout.X_AXIS)); 144 sha512CheckBox = new JCheckBox("SHA512 checksum:"); 145 sha512TextField = new JTextField(); 146 sha512OptionPanel.add(sha512CheckBox); 147 sha512OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 148 sha512OptionPanel.add(sha512TextField); 149 150 crc32OptionPanel.setLayout(new BoxLayout(crc32OptionPanel, BoxLayout.X_AXIS)); 151 crc32CheckBox = new JCheckBox("CRC32 checksum:"); 152 crc32CheckBox.setSelected(true); 153 crc32TextField = new JTextField(); 154 crc32OptionPanel.add(crc32CheckBox); 155 crc32OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 156 crc32OptionPanel.add(crc32TextField); 157 } 158 159 private void createOperationPanel() { 160 operationPanel = new JPanel(); 161 162 JButton compareButton = new JButton("Compare"); 163 compareButton.setFocusPainted(false); 164 compareButton.addActionListener(arg0 -> { 165 File file = filePanel.getFile(); 166 if (file == null) { 167 logger.error("file is null"); 168 return; 169 } 170 calculate(file); 171 }); 172 173 progressBar = new JProgressBar(); 174 progressBar.setStringPainted(true); 175 progressBar.setMaximum(100); 176 177 178 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS)); 179 operationPanel.add(compareButton); 180 operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 181 operationPanel.add(progressBar); 182 183 } 184 185 private void calculate(File file) { 186 progressBar.setValue(0); 187 new Thread(()->{ 188 sizeTextField.setText(edu.jiangxin.apktoolbox.utils.FileUtils.sizeOfInHumanFormat(file)); 189 progressBar.setValue(progressBar.getValue() + 5); 190 }).start(); 191 192 new Thread(()->{ 193 try { 194 lastModifiedTimeTextField.setText(String.valueOf(FileUtils.lastModified(file))); 195 } catch (IOException e) { 196 lastModifiedTimeTextField.setText("Unknown"); 197 } 198 progressBar.setValue(progressBar.getValue() + 5); 199 }).start(); 200 201 new Thread(()->{ 202 if (md5CheckBox.isSelected()) { 203 md5TextField.setText(calculate(CalculateType.Md5, file)); 204 } else { 205 md5TextField.setText(""); 206 } 207 progressBar.setValue(progressBar.getValue() + 15); 208 }).start(); 209 210 new Thread(()->{ 211 if (sha1CheckBox.isSelected()) { 212 sha1TextField.setText(calculate(CalculateType.Sha1, file)); 213 } else { 214 sha1TextField.setText(""); 215 } 216 progressBar.setValue(progressBar.getValue() + 15); 217 }).start(); 218 219 new Thread(()->{ 220 if (sha256CheckBox.isSelected()) { 221 sha256TextField.setText(calculate(CalculateType.Sha256, file)); 222 } else { 223 sha256TextField.setText(""); 224 } 225 progressBar.setValue(progressBar.getValue() + 15); 226 }).start(); 227 228 new Thread(()->{ 229 if (sha384CheckBox.isSelected()) { 230 sha384TextField.setText(calculate(CalculateType.Sha384, file)); 231 } else { 232 sha384TextField.setText(""); 233 } 234 progressBar.setValue(progressBar.getValue() + 15); 235 }).start(); 236 237 new Thread(()->{ 238 if (sha512CheckBox.isSelected()) { 239 sha512TextField.setText(calculate(CalculateType.Sha512, file)); 240 } else { 241 sha512TextField.setText(""); 242 } 243 progressBar.setValue(progressBar.getValue() + 15); 244 }).start(); 245 246 new Thread(()->{ 247 if (crc32CheckBox.isSelected()) { 248 crc32TextField.setText(calculate(CalculateType.Crc32, file)); 249 } else { 250 crc32TextField.setText(""); 251 } 252 progressBar.setValue(progressBar.getValue() + 15); 253 }).start(); 254 } 255 256 private String calculate(final CalculateType selectedHash, final File file) { 257 String result = ""; 258 try (FileInputStream fis = new FileInputStream(file)) { 259 switch (selectedHash) { 260 case Md5: { 261 result = DigestUtils.md5Hex(fis); 262 break; 263 } 264 case Sha1: { 265 result = DigestUtils.sha1Hex(fis); 266 break; 267 } 268 case Sha256: { 269 result = DigestUtils.sha256Hex(fis); 270 break; 271 } 272 case Sha384: { 273 result = DigestUtils.sha384Hex(fis); 274 break; 275 } 276 case Sha512: { 277 result = DigestUtils.sha512Hex(fis); 278 break; 279 } 280 case Crc32: { 281 result = Long.toHexString(FileUtils.checksumCRC32(file)); 282 break; 283 } 284 default: { 285 result = "Not support"; 286 break; 287 } 288 } 289 } catch (FileNotFoundException e) { 290 logger.error("calculate, FileNotFoundException"); 291 } catch (IOException e) { 292 logger.error("calculate, IOException"); 293 } 294 return result; 295 } 296 } 297