Source code: com/barteo/emulator/app/Main.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.emulator.app;
21
22 import java.awt.Dimension;
23 import java.awt.Image;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.KeyEvent;
27 import java.awt.event.KeyListener;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30 import java.io.File;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33
34 import javax.microedition.midlet.MIDlet;
35 import javax.microedition.midlet.MIDletStateChangeException;
36 import javax.swing.JFileChooser;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JMenu;
40 import javax.swing.JMenuBar;
41 import javax.swing.JMenuItem;
42 import javax.swing.JOptionPane;
43 import javax.swing.LookAndFeel;
44 import javax.swing.UIManager;
45
46 import com.barteo.emulator.DisplayComponent;
47 import com.barteo.emulator.EmulatorContext;
48 import com.barteo.emulator.MIDletBridge;
49 import com.barteo.emulator.app.ui.ResponseInterfaceListener;
50 import com.barteo.emulator.app.ui.StatusBarListener;
51 import com.barteo.emulator.app.ui.swing.ExtensionFileFilter;
52 import com.barteo.emulator.app.ui.swing.SwingDeviceComponent;
53 import com.barteo.emulator.app.ui.swing.SwingDialogWindow;
54 import com.barteo.emulator.app.ui.swing.SwingSelectDevicePanel;
55 import com.barteo.emulator.app.util.DeviceEntry;
56 import com.barteo.emulator.app.util.ProgressJarClassLoader;
57 import com.barteo.emulator.device.DeviceFactory;
58 import com.barteo.emulator.device.j2se.J2SEDevice;
59
60
61 public class Main extends JFrame
62 {
63 Main instance = null;
64
65 private Common common;
66
67 boolean initialized = false;
68
69 SwingSelectDevicePanel selectDevicePanel = null;
70 JFileChooser fileChooser = null;
71 JMenuItem menuOpenJADFile;
72 JMenuItem menuOpenJADURL;
73 JMenuItem menuSelectDevice;
74
75 SwingDeviceComponent devicePanel;
76 DeviceEntry deviceEntry;
77
78 JLabel statusBar = new JLabel("Status");
79
80 private EmulatorContext emulatorContext = new EmulatorContext()
81 {
82 ProgressJarClassLoader loader = new ProgressJarClassLoader();
83
84 public ClassLoader getClassLoader()
85 {
86 return loader;
87 }
88
89 public DisplayComponent getDisplayComponent()
90 {
91 return devicePanel.getDisplayComponent();
92 }
93 };
94
95 KeyListener keyListener = new KeyListener()
96 {
97 public void keyTyped(KeyEvent e)
98 {
99 }
100
101 public void keyPressed(KeyEvent e)
102 {
103 devicePanel.keyPressed(e);
104 }
105
106 public void keyReleased(KeyEvent e)
107 {
108 devicePanel.keyReleased(e);
109 }
110 };
111
112 ActionListener menuOpenJADFileListener = new ActionListener()
113 {
114 public void actionPerformed(ActionEvent ev)
115 {
116 if (fileChooser == null) {
117 ExtensionFileFilter fileFilter = new ExtensionFileFilter("JAD files");
118 fileFilter.addExtension("jad");
119 fileChooser = new JFileChooser();
120 fileChooser.setFileFilter(fileFilter);
121 fileChooser.setDialogTitle("Open JAD File...");
122 }
123
124 int returnVal = fileChooser.showOpenDialog(instance);
125 if (returnVal == JFileChooser.APPROVE_OPTION) {
126 try {
127 common.openJadFile(fileChooser.getSelectedFile().toURL());
128 } catch (MalformedURLException ex) {
129 System.err.println("Bad URL format " + fileChooser.getSelectedFile().getName());
130 }
131 }
132 }
133 };
134
135 ActionListener menuOpenJADURLListener = new ActionListener()
136 {
137 public void actionPerformed(ActionEvent ev)
138 {
139 String entered = JOptionPane.showInputDialog(instance, "Enter JAD URL:");
140 if (entered != null) {
141 try {
142 URL url = new URL(entered);
143 common.openJadFile(url);
144 } catch (MalformedURLException ex) {
145 System.err.println("Bad URL format " + entered);
146 }
147 }
148 }
149 };
150
151 ActionListener menuExitListener = new ActionListener()
152 {
153 public void actionPerformed(ActionEvent e)
154 {
155 System.exit(0);
156 }
157 };
158
159
160 ActionListener menuSelectDeviceListener = new ActionListener()
161 {
162 public void actionPerformed(ActionEvent e)
163 {
164 if (SwingDialogWindow.show(instance, "Select device...", selectDevicePanel)) {
165 if (selectDevicePanel.getSelectedDeviceEntry().equals(getDevice())) {
166 return;
167 }
168 if (MIDletBridge.getCurrentMIDlet() != common.getLauncher()) {
169 if (JOptionPane.showConfirmDialog(instance,
170 "Changing device needs MIDlet to be restarted. All MIDlet data will be lost. Are you sure?",
171 "Question?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) != 0) {
172 return;
173 }
174 }
175 setDevice(selectDevicePanel.getSelectedDeviceEntry());
176
177 if (MIDletBridge.getCurrentMIDlet() != common.getLauncher()) {
178 try {
179 MIDlet result = (MIDlet) MIDletBridge.getCurrentMIDlet().getClass().newInstance();
180 common.startMidlet(result);
181 } catch (Exception ex) {
182 System.err.println(ex);
183 }
184 } else {
185 common.startMidlet(common.getLauncher());
186 }
187 }
188 }
189 };
190
191 StatusBarListener statusBarListener = new StatusBarListener()
192 {
193 public void statusBarChanged(String text)
194 {
195 statusBar.setText(text);
196 }
197 };
198
199 ResponseInterfaceListener responseInterfaceListener = new ResponseInterfaceListener()
200 {
201 public void stateChanged(boolean state)
202 {
203 menuOpenJADFile.setEnabled(state);
204 menuOpenJADURL.setEnabled(state);
205 menuSelectDevice.setEnabled(state);
206 }
207 };
208
209 WindowAdapter windowListener = new WindowAdapter()
210 {
211 public void windowClosing(WindowEvent ev)
212 {
213 menuExitListener.actionPerformed(null);
214 }
215
216
217 public void windowIconified(WindowEvent ev)
218 {
219 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).pauseApp();
220 }
221
222 public void windowDeiconified(WindowEvent ev)
223 {
224 try {
225 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).startApp();
226 } catch (MIDletStateChangeException ex) {
227 System.err.println(ex);
228 }
229 }
230 };
231
232
233 Main()
234 {
235 instance = this;
236
237 JMenuBar menuBar = new JMenuBar();
238
239 JMenu menuFile = new JMenu("File");
240
241 menuOpenJADFile = new JMenuItem("Open JAD File...");
242 menuOpenJADFile.addActionListener(menuOpenJADFileListener);
243 menuFile.add(menuOpenJADFile);
244
245 menuOpenJADURL = new JMenuItem("Open JAD URL...");
246 menuOpenJADURL.addActionListener(menuOpenJADURLListener);
247 menuFile.add(menuOpenJADURL);
248
249 menuFile.addSeparator();
250
251 JMenuItem menuItem = new JMenuItem("Exit");
252 menuItem.addActionListener(menuExitListener);
253 menuFile.add(menuItem);
254
255 JMenu menuOptions = new JMenu("Options");
256
257 menuSelectDevice = new JMenuItem("Select device...");
258 menuSelectDevice.addActionListener(menuSelectDeviceListener);
259 menuOptions.add(menuSelectDevice);
260
261 menuBar.add(menuFile);
262 menuBar.add(menuOptions);
263 setJMenuBar(menuBar);
264
265 setTitle("MicroEmulator");
266 addWindowListener(windowListener);
267
268 Config.loadConfig("config.xml");
269 addKeyListener(keyListener);
270
271 devicePanel = new SwingDeviceComponent();
272 selectDevicePanel = new SwingSelectDevicePanel();
273
274 common = new Common(emulatorContext);
275 common.setStatusBarListener(statusBarListener);
276 common.setResponseInterfaceListener(responseInterfaceListener);
277
278 setDevice(selectDevicePanel.getSelectedDeviceEntry());
279
280 getContentPane().add(devicePanel, "Center");
281 getContentPane().add(statusBar, "South");
282
283 initialized = true;
284 }
285
286
287 public DeviceEntry getDevice()
288 {
289 return deviceEntry;
290 }
291
292
293 public void setDevice(DeviceEntry entry)
294 {
295 if (DeviceFactory.getDevice() != null) {
296 ((J2SEDevice) DeviceFactory.getDevice()).dispose();
297 }
298
299 ProgressJarClassLoader loader = (ProgressJarClassLoader) emulatorContext.getClassLoader();
300 try {
301 Class deviceClass = null;
302 if (entry.getFileName() != null) {
303 loader.addRepository(
304 new File(Config.getConfigPath(), entry.getFileName()).toURL());
305 deviceClass = loader.findClass(entry.getClassName());
306 } else {
307 deviceClass = Class.forName(entry.getClassName());
308 }
309 J2SEDevice device = (J2SEDevice) deviceClass.newInstance();
310 this.deviceEntry = entry;
311 setDevice(device);
312 } catch (MalformedURLException ex) {
313 System.err.println(ex);
314 } catch (ClassNotFoundException ex) {
315 System.err.println(ex);
316 } catch (InstantiationException ex) {
317 System.err.println(ex);
318 } catch (IllegalAccessException ex) {
319 System.err.println(ex);
320 }
321 }
322
323
324 protected void setDevice(J2SEDevice device)
325 {
326 common.setDevice(device);
327
328 device.init(emulatorContext);
329 devicePanel.init();
330 Image tmpImg = device.getNormalImage();
331 Dimension size = new Dimension(tmpImg.getWidth(null), tmpImg.getHeight(null));
332 size.width += 10;
333 size.height += statusBar.getPreferredSize().height + 55;
334 setSize(size);
335 doLayout();
336 }
337
338
339 public static void main(String args[])
340 {
341 Class uiClass = null;
342 int uiFontSize = 11;
343 try {
344 uiClass = Class.forName(UIManager.getSystemLookAndFeelClassName ());
345 } catch (ClassNotFoundException e) {}
346
347 if (uiClass != null) {
348 try {
349 LookAndFeel customUI = (javax.swing.LookAndFeel)uiClass.newInstance();
350 UIManager.setLookAndFeel(customUI);
351 } catch (Exception e) {
352 System.out.println("ERR_UIError");
353 }
354 } else{
355 try {
356 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
357 } catch (Exception ex) {
358 System.out.println("Failed loading Metal look and feel");
359 System.out.println(ex);
360 uiFontSize=11;
361 }
362 }
363
364 if(uiFontSize>0) {
365 java.awt.Font dialogPlain = new java.awt.Font("Dialog", java.awt.Font.PLAIN, uiFontSize);
366 java.awt.Font serifPlain = new java.awt.Font("Serif", java.awt.Font.PLAIN, uiFontSize);
367 java.awt.Font sansSerifPlain = new java.awt.Font("SansSerif", java.awt.Font.PLAIN, uiFontSize);
368 java.awt.Font monospacedPlain = new java.awt.Font("Monospaced", java.awt.Font.PLAIN, uiFontSize);
369 UIManager.getDefaults().put ("Button.font", dialogPlain);
370 UIManager.getDefaults().put ("ToggleButton.font", dialogPlain);
371 UIManager.getDefaults().put ("RadioButton.font", dialogPlain);
372 UIManager.getDefaults().put ("CheckBox.font", dialogPlain);
373 UIManager.getDefaults().put ("ColorChooser.font", dialogPlain);
374 UIManager.getDefaults().put ("ComboBox.font", dialogPlain);
375 UIManager.getDefaults().put ("Label.font", dialogPlain);
376 UIManager.getDefaults().put ("List.font", dialogPlain);
377 UIManager.getDefaults().put ("MenuBar.font", dialogPlain);
378 UIManager.getDefaults().put ("MenuItem.font", dialogPlain);
379 UIManager.getDefaults().put ("RadioButtonMenuItem.font", dialogPlain);
380 UIManager.getDefaults().put ("CheckBoxMenuItem.font", dialogPlain);
381 UIManager.getDefaults().put ("Menu.font", dialogPlain);
382 UIManager.getDefaults().put ("PopupMenu.font", dialogPlain);
383 UIManager.getDefaults().put ("OptionPane.font", dialogPlain);
384 UIManager.getDefaults().put ("Panel.font", dialogPlain);
385 UIManager.getDefaults().put ("ProgressBar.font", dialogPlain);
386 UIManager.getDefaults().put ("ScrollPane.font", dialogPlain);
387 UIManager.getDefaults().put ("Viewport.font", dialogPlain);
388 UIManager.getDefaults().put ("TabbedPane.font", dialogPlain);
389 UIManager.getDefaults().put ("Table.font", dialogPlain);
390 UIManager.getDefaults().put ("TableHeader.font", dialogPlain);
391 UIManager.getDefaults().put ("TextField.font", sansSerifPlain);
392 UIManager.getDefaults().put ("PasswordField.font", monospacedPlain);
393 UIManager.getDefaults().put ("TextArea.font", monospacedPlain);
394 UIManager.getDefaults().put ("TextPane.font", serifPlain);
395 UIManager.getDefaults().put ("EditorPane.font", serifPlain);
396 UIManager.getDefaults().put ("TitledBorder.font", dialogPlain);
397 UIManager.getDefaults().put ("ToolBar.font", dialogPlain);
398 UIManager.getDefaults().put ("ToolTip.font", sansSerifPlain);
399 UIManager.getDefaults().put ("Tree.font", dialogPlain);
400 }
401
402 Main app = new Main();
403 MIDlet m = null;
404
405 if (args.length > 0) {
406 if (args[0].endsWith(".jad")) {
407 try {
408 File file = new File(args[0]);
409 URL url = file.exists() ? file.toURL() : new URL(args[0]);
410 app.common.openJadFile(url);
411 } catch(MalformedURLException exception) {
412 System.out.println("Cannot parse " + args[0] + " URL");
413 }
414 } else {
415 Class midletClass;
416 try {
417 midletClass = Class.forName(args[0]);
418 m = app.common.loadMidlet("MIDlet", midletClass);
419 } catch (ClassNotFoundException ex) {
420 System.out.println("Cannot find " + args[0] + " MIDlet class");
421 }
422 }
423 } else {
424 m = app.common.getLauncher();
425 }
426
427 if (app.initialized) {
428 if (m != null) {
429 app.common.startMidlet(m);
430 }
431 app.validate();
432 app.setVisible(true);
433 } else {
434 System.exit(0);
435 }
436 }
437
438 }