1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.ninjasoft.magiccodes.swingui;
18
19 import java.awt.*;
20 import java.awt.event.*;
21 import javax.swing.*;
22
23 import java.util.Arrays;
24 import org.ninjasoft.magiccodes.logic.*;
25 import org.ninjasoft.magiccodes.plugins.*;
26
27
28 public class MainFrame extends JFrame {
29 private boolean initialized = false;
30 private Actions actions = new Actions();
31 private JTextArea inputText = new JTextArea();
32 private JEditorPane outputText = new JEditorPane();
33 private JTextField keyText = new JTextField();
34 private JTextField pluginDescriptionText = new JTextField();
35 private DefaultComboBoxModel pluginComboModel = new DefaultComboBoxModel();
36 private JComboBox inputCombo = new JComboBox(new String[] {"input is binary", "input is hex", "input is ASCII"});
37 private JComboBox keyCombo = new JComboBox(new String[] {"key is binary", "key is hex", "key is ASCII"});
38 private JComboBox outputCombo = new JComboBox(new String[] {"output to binary", "output to hex", "output to ASCII", "output to printable ASCII"});
39 private JComboBox pluginCombo = new JComboBox(pluginComboModel);
40
41 public void initialize() {
42 initializeGui();
43 initializeEvents();
44 initializePlugins();
45
46 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
47 this.setTitle("Magic Codes");
48 this.setIconImage(new ImageIcon(MainFrame.class.getResource("/org/ninjasoft/magiccodes/icon64.gif")).getImage());
49 inputCombo.setSelectedIndex(2);
50 outputCombo.setSelectedIndex(3);
51 keyCombo.setSelectedIndex(2);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 private void initializePlugins() {
70 Class pluginInterface = Plugin.class;
71 PluginDiscoverer pd = new PluginDiscoverer();
72 String[] plugins = pd.findMatchingPlugins(pluginInterface);
73 Arrays.sort(plugins);
74 for (int i=0; i<plugins.length; i++) {
75 if (plugins[i].equals("org.ninjasoft.magiccodes.plugins.Plugin"))
76 continue;
77 Plugin item = null;
78 try {
79 item = (Plugin) Class.forName(plugins[i]).newInstance();
80 } catch (Exception e) {
81 System.out.println("Unable to load " + plugins[i] + ": " + e.toString());
82 }
83 if (item != null)
84 this.pluginComboModel.addElement(new PluginItem(item));
85 }
86 }
87
88 private void initializeGui() {
89 if (initialized)
90 return;
91 initialized = true;
92 this.setSize(500, 400);
93 Dimension windowSize = this.getSize();
94 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
95 this.setLocation(screenSize.width/2 - windowSize.width/2, screenSize.height/2 - windowSize.height/2);
96 Container content = this.getContentPane();
97 content.setLayout(new BorderLayout());
98 JPanel pane = new JPanel();
99 pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
100 content.add(pane, BorderLayout.CENTER);
101 pane.setLayout(new GridBagLayout());
102 GridBagConstraints c = new GridBagConstraints();
103
104 c.gridx = c.gridy = 0;
105 c.gridwidth = c.gridheight = 1;
106 c.weightx = c.weighty = 0;
107 c.anchor = GridBagConstraints.WEST;
108 c.fill = GridBagConstraints.NONE;
109 c.insets = new Insets(3, 3, 3, 3);
110 pane.add(new JLabel("Input:"), c);
111 c.gridx++;
112 c.fill = GridBagConstraints.HORIZONTAL;
113 pane.add(inputCombo, c);
114
115 c.gridx = 0;
116 c.gridy++;
117 c.gridwidth = 3;
118 c.weightx = 3;
119 c.weighty = 2;
120 c.fill = GridBagConstraints.BOTH;
121 pane.add(new JScrollPane(inputText), c);
122
123 c.gridx = 0;
124 c.gridy++;
125 c.gridwidth = 1;
126 c.weightx = c.weighty = 0;
127 c.fill = GridBagConstraints.NONE;
128 pane.add(new JLabel("Key:"), c);
129 c.gridx++;
130 c.fill = GridBagConstraints.HORIZONTAL;
131 pane.add(keyText, c);
132 c.gridx++;
133 c.fill = GridBagConstraints.HORIZONTAL;
134 pane.add(keyCombo, c);
135
136 c.gridx = 0;
137 c.gridy++;
138 c.gridwidth = 1;
139 c.weightx = c.weighty = 0;
140 c.fill = GridBagConstraints.NONE;
141 pane.add(new JLabel("Plugin:"), c);
142 c.gridx++;
143 c.fill = GridBagConstraints.HORIZONTAL;
144 pane.add(pluginCombo, c);
145
146 c.gridx = 0;
147 c.gridy++;
148 c.gridwidth = 3;
149 c.weightx = c.weighty = 0;
150 c.fill = GridBagConstraints.HORIZONTAL;
151 pluginDescriptionText.setBorder(BorderFactory.createEmptyBorder());
152 pluginDescriptionText.setBackground(pane.getBackground());
153 pluginDescriptionText.setOpaque(false);
154 pane.add(pluginDescriptionText, c);
155
156 c.gridx = 0;
157 c.gridy++;
158 c.gridwidth = 1;
159 c.weightx = c.weighty = 0;
160 c.fill = GridBagConstraints.NONE;
161 pane.add(new JLabel("Output:"), c);
162 c.gridx++;
163 c.fill = GridBagConstraints.HORIZONTAL;
164 pane.add(outputCombo, c);
165 c.gridx++;
166 c.fill = GridBagConstraints.NONE;
167 c.anchor = GridBagConstraints.EAST;
168 JButton button = new JButton("Calculate");
169 button.addActionListener(actions);
170 button.setActionCommand("go");
171 pane.add(button, c);
172
173 c.gridx = 0;
174 c.gridy++;
175 c.gridwidth = 3;
176 c.weightx = 3;
177 c.weighty = 2;
178 c.anchor = GridBagConstraints.WEST;
179 c.fill = GridBagConstraints.BOTH;
180 pane.add(new JScrollPane(outputText), c);
181
182 outputText.setEditable(false);
183 }
184
185 private void initializeEvents() {
186 pluginCombo.addItemListener(new ItemListener() {
187 public void itemStateChanged(ItemEvent e) {
188 updateKeyFieldState();
189 updatePluginDescription();
190 }
191 });
192 }
193
194 private void updateKeyFieldState() {
195 boolean enabled = ((PluginItem) this.pluginComboModel.getSelectedItem()).getPlugin().usesKey();
196 keyText.setEnabled(enabled);
197 keyCombo.setEnabled(enabled);
198 }
199
200 private void updatePluginDescription() {
201 String description = ((PluginItem) this.pluginComboModel.getSelectedItem()).getPlugin().getDescription();
202 pluginDescriptionText.setText(description);
203 }
204
205 private void doIt() {
206 Processor processor = new Processor();
207 Plugin plugin = ((PluginItem) this.pluginComboModel.getSelectedItem()).getPlugin();
208 if (plugin.isInformational())
209 this.outputCombo.setSelectedIndex(3);
210 processor.addPlugin(plugin);
211 processor.setInputType(this.inputCombo.getSelectedIndex() + 1);
212 processor.setOutputType(this.outputCombo.getSelectedIndex() + 1);
213 processor.setKeyType(this.keyCombo.getSelectedIndex() + 1);
214 try{
215 System.out.println("Calculating...");
216 String result = processor.doAction(inputText.getText(), keyText.getText());
217 outputText.setText("");
218 if ((this.outputCombo.getSelectedIndex() + 1) == 4)
219 outputText.setContentType("text/html");
220 else
221 outputText.setContentType("text/plain");
222 outputText.setText(result);
223 System.out.println("Done");
224 }catch(FormatException e) {
225 System.out.println("Error");
226 outputText.setText(">>> MagicCodes found an error with your input format:\n\n" + e.toString());
227 outputText.setContentType("text/plain");
228 }catch(Throwable t) {
229 t.printStackTrace();
230 outputText.setText(">>> Problem running plugin:\n\n" + t.toString());
231 outputText.setContentType("text/plain");
232 }
233 outputText.setCaretPosition(0);
234 }
235
236 public class Actions implements ActionListener {
237 public void actionPerformed(ActionEvent e) {
238 String command = e.getActionCommand();
239 command = command == null ? "" : command;
240 if (command.equals("go"))
241 doIt();
242 }
243 }
244
245 public void dispose() {
246 System.exit(0);
247 }
248
249 public void setVisible(boolean b) {
250 initialize();
251 super.setVisible(b);
252 }
253
254 public static void main(String[] args) {
255 new MainFrame().setVisible(true);
256 }
257 }