Source code: com/barteo/emulator/app/Awt.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001-2003 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.FileDialog;
24 import java.awt.Frame;
25 import java.awt.Image;
26 import java.awt.Label;
27 import java.awt.Menu;
28 import java.awt.MenuBar;
29 import java.awt.MenuItem;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import java.io.File;
35 import java.net.MalformedURLException;
36 import java.net.URL;
37
38 import javax.microedition.midlet.MIDlet;
39 import javax.microedition.midlet.MIDletStateChangeException;
40
41 import com.barteo.emulator.DisplayComponent;
42 import com.barteo.emulator.EmulatorContext;
43 import com.barteo.emulator.MIDletBridge;
44 import com.barteo.emulator.app.ui.ResponseInterfaceListener;
45 import com.barteo.emulator.app.ui.StatusBarListener;
46 import com.barteo.emulator.app.ui.awt.AwtDeviceComponent;
47 import com.barteo.emulator.app.ui.awt.AwtDialogWindow;
48 import com.barteo.emulator.app.ui.awt.AwtSelectDevicePanel;
49 import com.barteo.emulator.app.ui.awt.FileChooser;
50 import com.barteo.emulator.app.ui.awt.OptionPane;
51 import com.barteo.emulator.app.util.DeviceEntry;
52 import com.barteo.emulator.app.util.ExtensionFileFilter;
53 import com.barteo.emulator.app.util.ProgressJarClassLoader;
54 import com.barteo.emulator.device.DeviceFactory;
55 import com.barteo.emulator.device.applet.AppletDevice;
56
57
58 public class Awt extends Frame
59 {
60 Awt instance = null;
61
62 private Common common;
63
64 boolean initialized = false;
65
66 AwtSelectDevicePanel selectDevicePanel = null;
67 FileChooser fileChooser = null;
68 MenuItem menuOpenJADFile;
69 MenuItem menuOpenJADURL;
70 MenuItem menuSelectDevice;
71
72 AwtDeviceComponent devicePanel;
73 DeviceEntry deviceEntry;
74
75 Label statusBar = new Label("Status");
76
77 private EmulatorContext emulatorContext = new EmulatorContext()
78 {
79 ProgressJarClassLoader loader = new ProgressJarClassLoader();
80
81 public ClassLoader getClassLoader()
82 {
83 return loader;
84 }
85
86 public DisplayComponent getDisplayComponent()
87 {
88 return devicePanel.getDisplayComponent();
89 }
90 };
91
92 ActionListener menuOpenJADFileListener = new ActionListener()
93 {
94 public void actionPerformed(ActionEvent ev)
95 {
96 if (fileChooser == null) {
97 ExtensionFileFilter fileFilter = new ExtensionFileFilter("JAD files");
98 fileFilter.addExtension("jad");
99 fileChooser = new FileChooser(instance, "Open JAD File...", FileDialog.LOAD);
100 fileChooser.setFilenameFilter(fileFilter);
101 }
102
103 fileChooser.show();
104
105 if (fileChooser.getFile() != null) {
106 try {
107 common.openJadFile(fileChooser.getSelectedFile().toURL());
108 } catch (MalformedURLException ex) {
109 System.err.println("Bad URL format " + fileChooser.getSelectedFile().getName());
110 }
111 }
112 }
113 };
114
115 ActionListener menuOpenJADURLListener = new ActionListener()
116 {
117 public void actionPerformed(ActionEvent ev)
118 {
119 String entered = OptionPane.showInputDialog(instance, "Enter JAD URL:");
120 if (entered != null) {
121 try {
122 URL url = new URL(entered);
123 common.openJadFile(url);
124 } catch (MalformedURLException ex) {
125 System.err.println("Bad URL format " + entered);
126 }
127 }
128 }
129 };
130
131 ActionListener menuExitListener = new ActionListener()
132 {
133 public void actionPerformed(ActionEvent e)
134 {
135 System.exit(0);
136 }
137 };
138
139
140 ActionListener menuSelectDeviceListener = new ActionListener()
141 {
142 public void actionPerformed(ActionEvent e)
143 {
144 if (AwtDialogWindow.show("Select device...", selectDevicePanel)) {
145 if (selectDevicePanel.getSelectedDeviceEntry().equals(getDevice())) {
146 return;
147 }
148 if (MIDletBridge.getCurrentMIDlet() != common.getLauncher()) {
149 /* if (JOptionPane.showConfirmDialog(instance,
150 "Changing device needs MIDlet to be restarted. All MIDlet data will be lost. Are you sure?",
151 "Question?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) != 0) {
152 return;
153 }*/
154 }
155 setDevice(selectDevicePanel.getSelectedDeviceEntry());
156
157 if (MIDletBridge.getCurrentMIDlet() != common.getLauncher()) {
158 try {
159 MIDlet result = (MIDlet) MIDletBridge.getCurrentMIDlet().getClass().newInstance();
160 common.startMidlet(result);
161 } catch (Exception ex) {
162 System.err.println(ex);
163 }
164 } else {
165 common.startMidlet(common.getLauncher());
166 }
167 }
168 }
169 };
170
171 StatusBarListener statusBarListener = new StatusBarListener()
172 {
173 public void statusBarChanged(String text)
174 {
175 statusBar.setText(text);
176 }
177 };
178
179 ResponseInterfaceListener responseInterfaceListener = new ResponseInterfaceListener()
180 {
181 public void stateChanged(boolean state)
182 {
183 menuOpenJADFile.setEnabled(state);
184 menuOpenJADURL.setEnabled(state);
185 menuSelectDevice.setEnabled(state);
186 }
187 };
188
189 WindowAdapter windowListener = new WindowAdapter()
190 {
191 public void windowClosing(WindowEvent ev)
192 {
193 menuExitListener.actionPerformed(null);
194 }
195
196
197 public void windowIconified(WindowEvent ev)
198 {
199 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).pauseApp();
200 }
201
202 public void windowDeiconified(WindowEvent ev)
203 {
204 try {
205 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).startApp();
206 } catch (MIDletStateChangeException ex) {
207 System.err.println(ex);
208 }
209 }
210 };
211
212
213 Awt()
214 {
215 instance = this;
216
217 MenuBar menuBar = new MenuBar();
218
219 Menu menuFile = new Menu("File");
220
221 menuOpenJADFile = new MenuItem("Open JAD File...");
222 menuOpenJADFile.addActionListener(menuOpenJADFileListener);
223 menuFile.add(menuOpenJADFile);
224
225 menuOpenJADURL = new MenuItem("Open JAD URL...");
226 menuOpenJADURL.addActionListener(menuOpenJADURLListener);
227 menuFile.add(menuOpenJADURL);
228
229 menuFile.addSeparator();
230
231 MenuItem menuItem = new MenuItem("Exit");
232 menuItem.addActionListener(menuExitListener);
233 menuFile.add(menuItem);
234
235 Menu menuOptions = new Menu("Options");
236
237 menuSelectDevice = new MenuItem("Select device...");
238 menuSelectDevice.addActionListener(menuSelectDeviceListener);
239 menuOptions.add(menuSelectDevice);
240
241 menuBar.add(menuFile);
242 menuBar.add(menuOptions);
243 setMenuBar(menuBar);
244
245 setTitle("MicroEmulator");
246 addWindowListener(windowListener);
247
248
249 Config.loadConfig("config-awt.xml");
250
251 devicePanel = new AwtDeviceComponent();
252 selectDevicePanel = new AwtSelectDevicePanel();
253
254 common = new Common(emulatorContext);
255 common.setStatusBarListener(statusBarListener);
256 common.setResponseInterfaceListener(responseInterfaceListener);
257
258 setDevice(selectDevicePanel.getSelectedDeviceEntry());
259
260 add(devicePanel, "Center");
261 add(statusBar, "South");
262
263 initialized = true;
264 }
265
266
267 public DeviceEntry getDevice()
268 {
269 return deviceEntry;
270 }
271
272
273 public void setDevice(DeviceEntry entry)
274 {
275 if (DeviceFactory.getDevice() != null) {
276 ((AppletDevice) DeviceFactory.getDevice()).dispose();
277 }
278
279 ProgressJarClassLoader loader = (ProgressJarClassLoader) emulatorContext.getClassLoader();
280 try {
281 Class deviceClass = null;
282 if (entry.getFileName() != null) {
283 loader.addRepository(
284 new File(Config.getConfigPath(), entry.getFileName()).toURL());
285 deviceClass = loader.findClass(entry.getClassName());
286 } else {
287 deviceClass = Class.forName(entry.getClassName());
288 }
289 AppletDevice device = (AppletDevice) deviceClass.newInstance();
290 this.deviceEntry = entry;
291 setDevice(device);
292 } catch (MalformedURLException ex) {
293 System.err.println(ex);
294 } catch (ClassNotFoundException ex) {
295 System.err.println(ex);
296 } catch (InstantiationException ex) {
297 System.err.println(ex);
298 } catch (IllegalAccessException ex) {
299 System.err.println(ex);
300 }
301 }
302
303
304 protected void setDevice(AppletDevice device)
305 {
306 common.setDevice(device);
307
308 device.init(emulatorContext);
309 devicePanel.init();
310 Image tmpImg = device.getNormalImage();
311 Dimension size = new Dimension(tmpImg.getWidth(null), tmpImg.getHeight(null));
312 size.width += 10;
313 size.height += statusBar.getPreferredSize().height + 55;
314 setSize(size);
315 doLayout();
316 }
317
318
319 public static void main(String args[])
320 {
321 Awt app = new Awt();
322 MIDlet m = null;
323
324 if (args.length > 0) {
325 if (args[0].endsWith(".jad")) {
326 try {
327 File file = new File(args[0]);
328 URL url = file.exists() ? file.toURL() : new URL(args[0]);
329 app.common.openJadFile(url);
330 } catch(MalformedURLException exception) {
331 System.out.println("Cannot parse " + args[0] + " URL");
332 }
333 } else {
334 Class midletClass;
335 try {
336 midletClass = Class.forName(args[0]);
337 m = app.common.loadMidlet("MIDlet", midletClass);
338 } catch (ClassNotFoundException ex) {
339 System.out.println("Cannot find " + args[0] + " MIDlet class");
340 }
341 }
342 } else {
343 m = app.common.getLauncher();
344 }
345
346 if (app.initialized) {
347 if (m != null) {
348 app.common.startMidlet(m);
349 }
350 app.validate();
351 app.setVisible(true);
352 } else {
353 System.exit(0);
354 }
355 }
356
357 }