1 //Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2 //This file is part of MagicCodes.
3 //
4 //MagicCodes is free software; you can redistribute it and/or modify
5 //it under the terms of the GNU General Public License as published by
6 //the Free Software Foundation; either version 2 of the License, or
7 //(at your option) any later version.
8 //
9 //MagicCodes is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 //GNU General Public License for more details.
13 //
14 //You should have received a copy of the GNU General Public License
15 //along with Foobar; if not, write to the Free Software
16 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 package org.ninjasoft.magiccodes.swingui;
18
19 import java.awt.*;
20 import java.awt.event.*;
21 import javax.swing.*;
22 //import javax.swing.event.*;
23 import java.util.Arrays;
24 import org.ninjasoft.magiccodes.logic.*;
25 import org.ninjasoft.magiccodes.plugins.*;
26 //import com.apple.eawt.*;
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 //initializeApple();
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 public void initializeApple() {
56 System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Magic Codes");
57 System.setProperty("com.apple.laf.useScreenMenuBar", "true");
58 Application application = Application.getApplication();
59 application.addApplicationListener(new ApplicationAdapter() {
60 public void handleQuit(ApplicationEvent event) {
61 dispose();
62 }
63 });
64 application.setEnabledAboutMenu(false);
65 application.setEnabledPreferencesMenu(false);
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 // ROW 2 //////////////////////////////////////
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 // ROW 2 //////////////////////////////////////
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 // ROW 3 //////////////////////////////////////
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 // ROW 4 //////////////////////////////////////
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 // ROW 5 //////////////////////////////////////
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 // ROW 6 //////////////////////////////////////
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 // ROW 7 //////////////////////////////////////
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 }
229 outputText.setCaretPosition(0);
230 }
231
232 public class Actions implements ActionListener {
233 public void actionPerformed(ActionEvent e) {
234 String command = e.getActionCommand();
235 command = command == null ? "" : command;
236 if (command.equals("go"))
237 doIt();
238 }
239 }
240
241 public void dispose() {
242 System.exit(0);
243 }
244
245 public void setVisible(boolean b) {
246 initialize();
247 super.setVisible(b);
248 }
249
250 public static void main(String[] args) {
251 new MainFrame().setVisible(true);
252 }
253 }
This page was automatically generated by Maven