1 package edu.jiangxin.apktoolbox.convert.zh2unicode; 2 3 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel; 4 import edu.jiangxin.apktoolbox.utils.Constants; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.io.UnsupportedEncodingException; 9 10 public class Zh2UnicodeConvertPanel extends EasyPanel { 11 private JPanel zhPanel; 12 13 private JTextArea zhTextArea; 14 15 private JPanel unicodePanel; 16 17 private JTextArea unicodeTextArea; 18 19 public Zh2UnicodeConvertPanel() throws HeadlessException { 20 super(); 21 } 22 23 @Override 24 public void initUI() { 25 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS); 26 setLayout(boxLayout); 27 28 createTextPanel(); 29 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 30 createOperationPanel(); 31 } 32 33 private void createTextPanel() { 34 JPanel textPanel = new JPanel(); 35 textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS)); 36 add(textPanel); 37 38 createZhPanel(); 39 textPanel.add(zhPanel); 40 41 textPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 42 43 createUnicodePanel(); 44 textPanel.add(unicodePanel); 45 } 46 47 private void createZhPanel() { 48 zhPanel = new JPanel(); 49 zhPanel.setLayout(new BorderLayout()); 50 zhPanel.setBorder(BorderFactory.createTitledBorder("中文")); 51 52 zhTextArea = new JTextArea(); 53 zhTextArea.setToolTipText("输入中文"); 54 zhTextArea.setEditable(true); 55 56 JScrollPane zhScrollPane = new JScrollPane(zhTextArea); 57 zhScrollPane.setPreferredSize(new Dimension(200, 500)); 58 59 zhPanel.add(zhScrollPane); 60 } 61 62 private void createUnicodePanel() { 63 unicodePanel = new JPanel(); 64 unicodePanel.setLayout(new BorderLayout()); 65 unicodePanel.setBorder(BorderFactory.createTitledBorder("Unicode")); 66 67 unicodeTextArea = new JTextArea(); 68 unicodeTextArea.setToolTipText("Enter Unicode Character"); 69 unicodeTextArea.setEditable(true); 70 71 JScrollPane unicodeScrollPane = new JScrollPane(unicodeTextArea); 72 unicodeScrollPane.setPreferredSize(new Dimension(200, 500)); 73 74 unicodePanel.add(unicodeScrollPane); 75 } 76 77 private void createOperationPanel() { 78 JPanel operationPanel = new JPanel(); 79 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS)); 80 add(operationPanel); 81 82 JButton zh2UnicodeConvertBtn = new JButton("中文->Unicode"); 83 zh2UnicodeConvertBtn.addActionListener(e -> { 84 String value = zhTextArea.getText(); 85 unicodeTextArea.setText(string2Unicode(value)); 86 }); 87 88 JButton unicode2ZhConvertBtn = new JButton("Unicode->中文"); 89 unicode2ZhConvertBtn.addActionListener(e -> { 90 String value = unicodeTextArea.getText(); 91 zhTextArea.setText(unicode2String(value)); 92 }); 93 94 operationPanel.add(zh2UnicodeConvertBtn); 95 operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 96 operationPanel.add(unicode2ZhConvertBtn); 97 } 98 99 private String string2Unicode(String str) { 100 StringBuilder sb = new StringBuilder(); 101 byte[] bytes; 102 try { 103 bytes = str.getBytes("unicode"); 104 } catch (UnsupportedEncodingException e) { 105 logger.error("string2Unicode: UnsupportedEncodingException"); 106 return null; 107 } 108 for (int i = 2; i < bytes.length - 1; i += 2) { 109 sb.append("\\u"); 110 String firstByte = Integer.toHexString(bytes[i] & 0xff); 111 sb.append("0".repeat(2 - firstByte.length())); 112 sb.append(firstByte); 113 String secondByte = Integer.toHexString(bytes[i + 1] & 0xff); 114 sb.append("0".repeat(2 - secondByte.length())); 115 sb.append(secondByte); 116 } 117 return sb.toString().toLowerCase(); 118 } 119 120 private String unicode2String(String unicodeStr) { 121 StringBuilder sb = new StringBuilder(); 122 String[] characterArray = unicodeStr.toLowerCase().split("\\\\u"); 123 for (String s : characterArray) { 124 if (s.equals("")) { 125 continue; 126 } 127 char character = (char) Integer.parseInt(s.trim(), 16); 128 sb.append(character); 129 } 130 return sb.toString(); 131 } 132 }