1 package edu.jiangxin.apktoolbox.convert.encoding;
2
3 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
4 import edu.jiangxin.apktoolbox.utils.Constants;
5
6 import javax.swing.*;
7 import javax.swing.border.TitledBorder;
8 import javax.swing.event.DocumentListener;
9 import java.awt.*;
10 import java.io.UnsupportedEncodingException;
11
12 public class AsciiGbViewerPanel extends EasyPanel {
13
14 private JTextField inputField;
15 private JTextArea hexArea;
16 private JTextArea decArea;
17
18 private static final String[] ASCII_NAMES = new String[128];
19
20 static {
21 for (int i = 0; i < 32; i++) {
22 ASCII_NAMES[i] = String.format("%-3s", getControlName(i));
23 }
24 ASCII_NAMES[32] = "SP";
25 for (int i = 33; i < 127; i++) {
26 ASCII_NAMES[i] = String.valueOf((char) i);
27 }
28 ASCII_NAMES[127] = "DEL";
29 }
30
31 public AsciiGbViewerPanel() {
32 super();
33 }
34
35 @Override
36 public void initUI() {
37 buildUI();
38 refreshGb();
39 }
40
41 private static String getControlName(int c) {
42 String[] n = {"NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL","BS","HT","LF","VT","FF","CR","SO","SI",
43 "DLE","DC1","DC2","DC3","DC4","NAK","SYN","ETB","CAN","EM","SUB","ESC","FS","GS","RS","US"};
44 return n[c];
45 }
46
47 private void buildUI() {
48 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
49
50 createInputPanel();
51 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
52
53 createHexPanel();
54 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
55
56 createDecPanel();
57 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
58
59 createAsciiTablePanel();
60 }
61
62 private void createInputPanel() {
63 JPanel inputPanel = new JPanel();
64 inputPanel.setLayout(new BorderLayout());
65 inputPanel.setBorder(BorderFactory.createTitledBorder("输入汉字"));
66
67 inputField = new JTextField("欢迎使用ApkToolBoxGUI", 20);
68 inputField.getDocument().addDocumentListener(new MyDocumentListener());
69
70 inputPanel.add(inputField);
71 add(inputPanel);
72 }
73
74 private void createHexPanel() {
75 JPanel hexPanel = new JPanel();
76 hexPanel.setLayout(new BorderLayout());
77 hexPanel.setBorder(BorderFactory.createTitledBorder("16进制表示(GB2312 编码)"));
78
79 hexArea = new JTextArea(6, 18);
80 hexArea.setEditable(false);
81 hexArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
82
83 hexPanel.add(new JScrollPane(hexArea));
84 add(hexPanel);
85 }
86
87 private void createDecPanel() {
88 JPanel decPanel = new JPanel();
89 decPanel.setLayout(new BorderLayout());
90 decPanel.setBorder(BorderFactory.createTitledBorder("10进制表示(GB2312 编码)"));
91
92 decArea = new JTextArea(6, 18);
93 decArea.setEditable(false);
94 decArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
95
96 decPanel.add(new JScrollPane(decArea));
97 add(decPanel);
98 }
99
100 private void createAsciiTablePanel() {
101 JPanel asciiTablePanel = new JPanel(new GridLayout(16, 8, 2, 2));
102 asciiTablePanel.setBorder(new TitledBorder("标准 7-bit ASCII 表(128 字符),鼠标悬停可查看十/十六进制值"));
103 asciiTablePanel.setPreferredSize(new Dimension(500, 400));
104
105 for (int i = 0; i < 128; i++) {
106 JLabel lab = new JLabel(ASCII_NAMES[i], SwingConstants.CENTER);
107 lab.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
108 lab.setToolTipText("Dec=" + i + " Hex=0x" + Integer.toHexString(i).toUpperCase());
109 asciiTablePanel.add(lab);
110 }
111
112 add(asciiTablePanel);
113 }
114
115 class MyDocumentListener implements DocumentListener {
116 public void insertUpdate(javax.swing.event.DocumentEvent e) { refreshGb(); }
117 public void removeUpdate(javax.swing.event.DocumentEvent e) { refreshGb(); }
118 public void changedUpdate(javax.swing.event.DocumentEvent e) { refreshGb(); }
119 }
120
121 private void refreshGb() {
122 String txt = inputField.getText();
123 StringBuilder hex = new StringBuilder();
124 StringBuilder dec = new StringBuilder();
125 try {
126 byte[] gb = txt.getBytes("GB2312");
127 for (byte value : gb) {
128 int b = value & 0xff;
129 hex.append(String.format("%02X ", b));
130 dec.append(b).append(' ');
131 }
132 } catch (UnsupportedEncodingException ex) {
133 hex.append("GB2312 不支持");
134 dec.append("GB2312 不支持");
135 }
136 hexArea.setText(hex.toString());
137 decArea.setText(dec.toString());
138 }
139 }