View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * This class provides converter from any character to unicode escape code. The
41   * GUI for the encoder is added as inner class.
42   *
43   * @author <a href="mailto:gasper.pajor@cosylab.com">Gasper Pajor</a>
44   * @version $id$
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  		 * Creates a new StringUnicoderGUI object.
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 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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 	 * Run test applet.
137 	 *
138 	 * @param args command line parameters
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 	 * Converts input string into unicode escape code.
149 	 *
150 	 * @param str the string to be converted
151 	 * @param convertAscii wheather to also convert ascii charcters
152 	 *
153 	 * @return converted String
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 				//System.out.println("character "+ch+" was not transformed, it is ASCII char");
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 /* __oOo__ */