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