1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.util;
21
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27
28 import java.util.Locale;
29
30 import javax.swing.JButton;
31 import javax.swing.JCheckBox;
32 import javax.swing.JFrame;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTextArea;
37
38
39
40
41
42
43
44
45
46 public class StringUnicoder
47 {
48 private class StringUnicoderGUI extends JFrame implements ActionListener
49 {
50 private boolean convertAscii = true;
51 private JScrollPane scrollGarble = new JScrollPane();
52 private JScrollPane scrollUnicode = new JScrollPane();
53 private JTextArea textGarble = new JTextArea();
54 private JTextArea textUnicode = new JTextArea();
55 private JButton doItButton = new JButton("Transform!");
56 private JCheckBox refactorAsciiCheckBox = new JCheckBox("Convert ASCII",
57 convertAscii);
58 private JPanel commandPanel = new JPanel(new GridBagLayout());
59
60
61
62
63 public StringUnicoderGUI()
64 {
65 super();
66 initialize();
67 }
68
69 private void initialize()
70 {
71 getContentPane().setLayout(new GridBagLayout());
72 constructCommandPanel();
73
74 GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 1, 0,
75 GridBagConstraints.WEST, GridBagConstraints.NONE,
76 new Insets(4, 4, 4, 4), 0, 0);
77 getContentPane().add(new JLabel("Normal Text:"), c);
78
79 c.gridy = 1;
80 c.weighty = 1;
81 c.fill = GridBagConstraints.BOTH;
82 scrollGarble.setViewportView(textGarble);
83 getContentPane().add(scrollGarble, c);
84
85 c.gridy = 2;
86 c.weighty = 0;
87 getContentPane().add(commandPanel, c);
88
89 c.gridy = 3;
90 getContentPane().add(new JLabel("Unicode Escape Format:"), c);
91
92 c.gridy = 4;
93 c.weighty = 1;
94 textUnicode.setEditable(false);
95 scrollUnicode.setViewportView(textUnicode);
96 getContentPane().add(scrollUnicode, c);
97
98 setSize(600, 300);
99 setLocation(100, 100);
100 setTitle("StringUnicoder -- Cosylab");
101 }
102
103
104 private void constructCommandPanel()
105 {
106 GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 0, 0,
107 GridBagConstraints.CENTER, GridBagConstraints.NONE,
108 new Insets(4, 4, 4, 4), 0, 0);
109
110 doItButton.addActionListener(this);
111 commandPanel.add(refactorAsciiCheckBox, c);
112
113 c.gridy = 1;
114 refactorAsciiCheckBox.addActionListener(this);
115 commandPanel.add(doItButton, c);
116 }
117
118
119
120
121 public void actionPerformed(ActionEvent e)
122 {
123 if (e.getSource() == doItButton) {
124 textUnicode.setText(convertUnicodeString(textGarble.getText(),
125 convertAscii));
126 }
127
128 if (e.getSource() == refactorAsciiCheckBox) {
129 convertAscii = refactorAsciiCheckBox.isSelected();
130 }
131 }
132 }
133 ;
134
135
136
137
138
139
140 public static void main(String[] args)
141 {
142 StringUnicoder s = new StringUnicoder();
143 StringUnicoderGUI sugui = s.new StringUnicoderGUI();
144 sugui.show();
145 }
146
147
148
149
150
151
152
153
154
155 public static String convertUnicodeString(String str, boolean convertAscii)
156 {
157 String ostr = new String();
158
159 for (int i = 0; i < str.length(); i++) {
160 char ch = str.charAt(i);
161
162 if (!convertAscii && ((ch >= 0x0020) && (ch <= 0x007e))) {
163 ostr += ch;
164
165
166 } else {
167 ostr += "\\u";
168
169 String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);
170
171 if (hex.length() == 2) {
172 ostr += "00";
173 }
174
175 ostr += hex.toUpperCase(Locale.ENGLISH);
176 }
177 }
178
179 return ostr;
180 }
181 }
182
183