1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.ui;
20
21 import java.awt.BorderLayout;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.WindowAdapter;
24 import java.awt.event.WindowEvent;
25 import java.beans.XMLDecoder;
26 import java.beans.XMLEncoder;
27 import java.io.BufferedInputStream;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33
34 import javax.swing.AbstractAction;
35 import javax.swing.ButtonGroup;
36 import javax.swing.JButton;
37 import javax.swing.JComponent;
38 import javax.swing.JFileChooser;
39 import javax.swing.JFrame;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.JRadioButton;
44 import javax.swing.JScrollPane;
45 import javax.swing.JSplitPane;
46 import javax.swing.JTextArea;
47 import javax.swing.JToolBar;
48 import javax.swing.UIManager;
49 import javax.swing.UnsupportedLookAndFeelException;
50
51 import net.sf.dexterim.codec.HexPrinter;
52 import net.sf.dexterim.oscar.OscarByteBuffer;
53
54 /***
55 * @author christoph
56 */
57 public class HexUI extends JFrame {
58
59 private JTextArea inputArea;
60 private JTextArea outputArea;
61 private JRadioButton numberConvert;
62 private JRadioButton stringConvert;
63
64 private HexModel model;
65 private JFilterManager filterManager;
66
67 public HexUI() {
68 super("Hex String Converter");
69
70 this.model = createHexModel();
71
72 JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
73
74 this.inputArea = createTextArea();
75 pane.add(new JScrollPane(inputArea), JSplitPane.TOP);
76
77 this.outputArea = createTextArea();
78 pane.add(new JScrollPane(outputArea), JSplitPane.BOTTOM);
79
80 this.filterManager = new JFilterManager(JSplitPane.VERTICAL_SPLIT);
81 this.filterManager.setModel(model);
82
83 JSplitPane horizontalSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
84 horizontalSplit.setLeftComponent(pane);
85 horizontalSplit.setRightComponent(filterManager);
86
87 this.getContentPane().add(horizontalSplit, BorderLayout.CENTER);
88
89 getContentPane().add(createControls(), BorderLayout.NORTH);
90
91 this.setDefaultCloseOperation(HIDE_ON_CLOSE);
92
93 this.addWindowListener(new WindowAdapter() {
94
95 /***
96 * @see java.awt.event.WindowAdapter#windowClosed(java.awt.event.WindowEvent)
97 */
98 public void windowClosing(WindowEvent e) {
99 String userHome = System.getProperty("user.home");
100
101 File f = new File(userHome + File.separator + ".hexui");
102
103 try {
104 XMLEncoder enc = new XMLEncoder(new FileOutputStream(f));
105 enc.writeObject(model);
106 enc.close();
107 }
108 catch (FileNotFoundException e1) {
109 e1.printStackTrace();
110 }
111 finally {
112 System.exit(0);
113 }
114 }
115 });
116
117 this.setJMenuBar(createJMenuBar());
118 }
119
120 /***
121 * @return
122 */
123 private JTextArea createTextArea() {
124 return new JTextArea(6, 40);
125 }
126
127 /***
128 * @return
129 */
130 protected JComponent createControls() {
131 JToolBar controls = new JToolBar();
132
133 ButtonGroup group = new ButtonGroup();
134
135 stringConvert = new JRadioButton("String");
136 stringConvert.setSelected(true);
137 group.add(stringConvert);
138 controls.add(stringConvert);
139
140 numberConvert = new JRadioButton("Number");
141 group.add(numberConvert);
142 controls.add(numberConvert);
143
144 controls.add(new JButton(new ConvertAction()));
145 return controls;
146 }
147
148 /***
149 * @return
150 */
151 protected JMenuBar createJMenuBar() {
152 JMenuBar menuBar = new JMenuBar();
153
154 JMenu menu = new JMenu("File");
155 JMenuItem open = new JMenuItem(new OpenAction());
156
157 menu.add(open);
158 menuBar.add(menu);
159 return menuBar;
160 }
161
162 /***
163 * @return
164 */
165 protected HexModel createHexModel() {
166 return new BasicHexModel();
167 }
168
169 public HexModel getModel() {
170 return model;
171 }
172
173 /***
174 *
175 */
176 protected void convert() {
177 String text = inputArea.getText();
178
179 model.setText(text);
180 String filteredText = model.filterText();
181
182 String result;
183
184 if (stringConvert.isSelected()) {
185 result = HexPrinter.convertString(filteredText);
186 }
187 else {
188 result = HexPrinter.convertNumber(filteredText);
189 }
190
191 outputArea.setText(result);
192 }
193
194 public static void main(String[] args) {
195
196 try {
197 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
198 }
199 catch (ClassNotFoundException e) {
200 e.printStackTrace();
201 }
202 catch (InstantiationException e) {
203 e.printStackTrace();
204 }
205 catch (IllegalAccessException e) {
206 e.printStackTrace();
207 }
208 catch (UnsupportedLookAndFeelException e) {
209 e.printStackTrace();
210 }
211
212 HexUI ui = new HexUI();
213
214 String userHome = System.getProperty("user.home");
215
216 File f = new File(userHome + File.separator + ".hexui");
217
218 try {
219 XMLDecoder dec = new XMLDecoder(new FileInputStream(f));
220 ui.setModel((HexModel)dec.readObject());
221 dec.close();
222 }
223 catch (FileNotFoundException e1) {
224 e1.printStackTrace();
225 }
226
227 ui.pack();
228 ui.show();
229 }
230
231 /***
232 * @param model
233 */
234 private void setModel(HexModel model) {
235 this.model = model;
236
237 this.filterManager.setModel(model);
238 }
239
240 private class ConvertAction extends AbstractAction {
241
242 public ConvertAction() {
243 super("Convert");
244 }
245
246 public void actionPerformed(ActionEvent evt) {
247 convert();
248 }
249 }
250
251 private class OpenAction extends AbstractAction {
252
253 public OpenAction() {
254 super("Open");
255 }
256
257 public void actionPerformed(ActionEvent e) {
258 JFileChooser chooser = new JFileChooser();
259
260 if (chooser.showOpenDialog(HexUI.this) == JFileChooser.APPROVE_OPTION) {
261
262 BufferedInputStream inputStream;
263 try {
264 inputStream = new BufferedInputStream(new FileInputStream(chooser
265 .getSelectedFile()));
266
267 OscarByteBuffer byteBuffer = OscarByteBuffer.dynamic();
268
269 int in = -1;
270
271 while ((in = inputStream.read()) != -1) {
272 byteBuffer.write((byte) in);
273 }
274
275 HexUI.this.inputArea.setText(HexPrinter.format(byteBuffer.slice()));
276 }
277 catch (FileNotFoundException fex) {
278 fex.printStackTrace();
279 }
280 catch (IOException ioex) {
281 ioex.printStackTrace();
282 }
283 }
284 }
285
286 }
287 }