Source code: com/barteo/emulator/app/Swt.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.io.File;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25
26 import javax.microedition.midlet.MIDlet;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.KeyEvent;
30 import org.eclipse.swt.events.KeyListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Event;
35 import org.eclipse.swt.widgets.FileDialog;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Listener;
38 import org.eclipse.swt.widgets.Menu;
39 import org.eclipse.swt.widgets.MenuItem;
40 import org.eclipse.swt.widgets.Shell;
41
42 import com.barteo.emulator.DisplayComponent;
43 import com.barteo.emulator.EmulatorContext;
44 import com.barteo.emulator.MIDletBridge;
45 import com.barteo.emulator.app.ui.ResponseInterfaceListener;
46 import com.barteo.emulator.app.ui.StatusBarListener;
47 import com.barteo.emulator.app.ui.swt.SwtDeviceComponent;
48 import com.barteo.emulator.app.ui.swt.SwtDialog;
49 import com.barteo.emulator.app.ui.swt.SwtInputDialog;
50 import com.barteo.emulator.app.ui.swt.SwtMessageDialog;
51 import com.barteo.emulator.app.ui.swt.SwtSelectDeviceDialog;
52 import com.barteo.emulator.app.util.DeviceEntry;
53 import com.barteo.emulator.app.util.ProgressJarClassLoader;
54 import com.barteo.emulator.device.DeviceFactory;
55 import com.barteo.emulator.device.swt.SwtDevice;
56
57
58 public class Swt extends Common
59 {
60 public static Shell shell;
61
62 private static SwtDeviceComponent devicePanel;
63
64 Swt instance = null;
65
66 boolean initialized = false;
67
68 SwtSelectDeviceDialog selectDeviceDialog;
69 FileDialog fileDialog = null;
70 MenuItem menuOpenJADFile;
71 MenuItem menuOpenJADURL;
72 MenuItem menuSelectDevice;
73
74 DeviceEntry deviceEntry;
75
76 Label statusBar;
77
78 KeyListener keyListener = new KeyListener()
79 {
80 public void keyTyped(KeyEvent e)
81 {
82 }
83
84 public void keyPressed(KeyEvent e)
85 {
86 // devicePanel.keyPressed(e);
87 }
88
89 public void keyReleased(KeyEvent e)
90 {
91 // devicePanel.keyReleased(e);
92 }
93 };
94
95 Listener menuOpenJADFileListener = new Listener()
96 {
97 public void handleEvent(Event ev)
98 {
99 if (fileDialog == null) {
100 fileDialog = new FileDialog(shell, SWT.OPEN);
101 fileDialog.setText("Open JAD File...");
102 fileDialog.setFilterNames(new String[] {"JAD files"});
103 fileDialog.setFilterExtensions(new String[] {"*.jad"});
104 }
105
106 fileDialog.open();
107
108 if (fileDialog.getFileName().length() > 0) {
109 try {
110 openJadFile(new File(fileDialog.getFilterPath(), fileDialog.getFileName()).toURL());
111 } catch (MalformedURLException ex) {
112 System.err.println("Bad URL format " + fileDialog.getFileName());
113 }
114 }
115 }
116 };
117
118 Listener menuOpenJADURLListener = new Listener()
119 {
120 public void handleEvent(Event ev)
121 {
122 SwtInputDialog inputDialog = new SwtInputDialog(shell, "Open...", "Enter JAD URL:");
123 if (inputDialog.open() == SwtDialog.OK) {
124 try {
125 URL url = new URL(inputDialog.getValue());
126 openJadFile(url);
127 } catch (MalformedURLException ex) {
128 System.err.println("Bad URL format " + inputDialog.getValue());
129 }
130 }
131 }
132 };
133
134 Listener menuExitListener = new Listener()
135 {
136 public void handleEvent(Event e)
137 {
138 System.exit(0);
139 }
140 };
141
142
143 Listener menuSelectDeviceListener = new Listener()
144 {
145 public void handleEvent(Event e)
146 {
147 if (selectDeviceDialog.open() == SwtDialog.OK) {
148 if (selectDeviceDialog.getSelectedDeviceEntry().equals(getDevice())) {
149 return;
150 }
151 if (MIDletBridge.getCurrentMIDlet() != getLauncher()) {
152 if (!SwtMessageDialog.openQuestion(shell,
153 "Question?", "Changing device needs MIDlet to be restarted. All MIDlet data will be lost. Are you sure?")) {
154 return;
155 }
156 }
157 setDevice(selectDeviceDialog.getSelectedDeviceEntry());
158
159 if (MIDletBridge.getCurrentMIDlet() != getLauncher()) {
160 try {
161 MIDlet result = (MIDlet) MIDletBridge.getCurrentMIDlet().getClass().newInstance();
162 startMidlet(result);
163 } catch (Exception ex) {
164 System.err.println(ex);
165 }
166 } else {
167 startMidlet(getLauncher());
168 }
169 }
170 }
171 };
172
173 StatusBarListener statusBarListener = new StatusBarListener()
174 {
175 public void statusBarChanged(final String text)
176 {
177 shell.getDisplay().asyncExec(new Runnable()
178 {
179 public void run()
180 {
181 statusBar.setText(text);
182 }
183 });
184 }
185 };
186
187 ResponseInterfaceListener responseInterfaceListener = new ResponseInterfaceListener()
188 {
189 public void stateChanged(final boolean state)
190 {
191 shell.getDisplay().asyncExec(new Runnable()
192 {
193 public void run()
194 {
195 menuOpenJADFile.setEnabled(state);
196 menuOpenJADURL.setEnabled(state);
197 menuSelectDevice.setEnabled(state);
198 }
199 });
200 }
201 };
202
203 /* WindowAdapter windowListener = new WindowAdapter()
204 {
205 public void windowClosing(WindowEvent ev)
206 {
207 menuExitListener.actionPerformed(null);
208 }
209
210
211 public void windowIconified(WindowEvent ev)
212 {
213 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).pauseApp();
214 }
215
216 public void windowDeiconified(WindowEvent ev)
217 {
218 try {
219 MIDletBridge.getMIDletAccess(common.getLauncher().getCurrentMIDlet()).startApp();
220 } catch (MIDletStateChangeException ex) {
221 System.err.println(ex);
222 }
223 }
224 };*/
225
226
227 Swt(Shell shell)
228 {
229 super(new EmulatorContext()
230 {
231 ProgressJarClassLoader loader = new ProgressJarClassLoader();
232
233 public ClassLoader getClassLoader()
234 {
235 return loader;
236 }
237
238 public DisplayComponent getDisplayComponent()
239 {
240 return devicePanel.getDisplayComponent();
241 }
242 });
243
244 instance = this;
245
246 GridLayout layout = new GridLayout(1, false);
247 shell.setLayout(layout);
248 shell.setLayoutData(new GridData(GridData.FILL_BOTH));
249
250 Menu bar = new Menu(shell, SWT.BAR);
251 shell.setMenuBar(bar);
252
253 MenuItem menuFile = new MenuItem(bar, SWT.CASCADE);
254 menuFile.setText("File");
255
256 Menu fileSubmenu = new Menu(shell, SWT.DROP_DOWN);
257 menuFile.setMenu(fileSubmenu);
258
259 menuOpenJADFile = new MenuItem(fileSubmenu, SWT.PUSH);
260 menuOpenJADFile.setText("Open JAD File...");
261 menuOpenJADFile.addListener(SWT.Selection, menuOpenJADFileListener);
262
263 menuOpenJADURL = new MenuItem(fileSubmenu, 0);
264 menuOpenJADURL.setText("Open JAD URL...");
265 menuOpenJADURL.addListener(SWT.Selection, menuOpenJADURLListener);
266
267 new MenuItem(fileSubmenu, SWT.SEPARATOR);
268
269 MenuItem menuExit = new MenuItem(fileSubmenu, SWT.PUSH);
270 menuExit.setText("Exit");
271 menuExit.addListener(SWT.Selection, menuExitListener);
272
273 MenuItem menuOptions = new MenuItem(bar, SWT.CASCADE);
274 menuOptions.setText("Options");
275
276 Menu optionsSubmenu = new Menu(shell, SWT.DROP_DOWN);
277 menuOptions.setMenu(optionsSubmenu);
278
279 menuSelectDevice = new MenuItem(optionsSubmenu, SWT.PUSH);
280 menuSelectDevice.setText("Select device...");
281 menuSelectDevice.addListener(SWT.Selection, menuSelectDeviceListener);
282
283 shell.setText("MicroEmulator");
284 // addWindowListener(windowListener);
285
286 Config.loadConfig("config-swt.xml");
287 shell.addKeyListener(keyListener);
288
289 devicePanel = new SwtDeviceComponent(shell);
290 devicePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
291
292 statusBar = new Label(shell, SWT.HORIZONTAL);
293 statusBar.setText("Status");
294 statusBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
295
296 selectDeviceDialog = new SwtSelectDeviceDialog(shell);
297 setDevice(selectDeviceDialog.getSelectedDeviceEntry());
298
299 setStatusBarListener(statusBarListener);
300 setResponseInterfaceListener(responseInterfaceListener);
301
302 initialized = true;
303 }
304
305
306 public DeviceEntry getDevice()
307 {
308 return deviceEntry;
309 }
310
311
312 public void setDevice(DeviceEntry entry)
313 {
314 if (DeviceFactory.getDevice() != null) {
315 ((SwtDevice) DeviceFactory.getDevice()).dispose();
316 }
317
318 ProgressJarClassLoader loader = (ProgressJarClassLoader) emulatorContext.getClassLoader();
319 try {
320 Class deviceClass = null;
321 if (entry.getFileName() != null) {
322 loader.addRepository(
323 new File(Config.getConfigPath(), entry.getFileName()).toURL());
324 deviceClass = loader.findClass(entry.getClassName());
325 } else {
326 deviceClass = Class.forName(entry.getClassName());
327 }
328 SwtDevice device = (SwtDevice) deviceClass.newInstance();
329 this.deviceEntry = entry;
330 setDevice(device);
331 } catch (MalformedURLException ex) {
332 System.err.println(ex);
333 } catch (ClassNotFoundException ex) {
334 System.err.println(ex);
335 } catch (InstantiationException ex) {
336 System.err.println(ex);
337 } catch (IllegalAccessException ex) {
338 System.err.println(ex);
339 }
340 }
341
342
343 protected void setDevice(SwtDevice device)
344 {
345 super.setDevice(device);
346
347 device.init(emulatorContext);
348 shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
349 }
350
351
352 public static void main(String args[])
353 {
354 Display display = new Display();
355 shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN);
356
357 Swt app = new Swt(shell);
358 MIDlet m = null;
359
360 if (args.length > 0) {
361 for (int i = 0; i < args.length; i++) {
362 if (args[i].equals("--deviceClass")) {
363 i++;
364 try {
365 Class deviceClass = Class.forName(args[i]);
366 app.setDevice((SwtDevice) deviceClass.newInstance());
367 } catch (ClassNotFoundException ex) {
368 System.err.println(ex);
369 } catch (InstantiationException ex) {
370 System.err.println(ex);
371 } catch (IllegalAccessException ex) {
372 System.err.println(ex);
373 }
374
375 } else if (args[i].endsWith(".jad")) {
376 try {
377 File file = new File(args[i]);
378 URL url = file.exists() ? file.toURL() : new URL(args[i]);
379 app.openJadFile(url);
380 } catch(MalformedURLException exception) {
381 System.out.println("Cannot parse " + args[0] + " URL");
382 }
383 } else {
384 Class midletClass;
385 try {
386 midletClass = Class.forName(args[i]);
387 m = app.loadMidlet("MIDlet", midletClass);
388 } catch (ClassNotFoundException ex) {
389 System.out.println("Cannot find " + args[i] + " MIDlet class");
390 }
391 }
392 }
393 } else {
394 m = app.getLauncher();
395 }
396
397 if (app.initialized) {
398 if (m != null) {
399 app.startMidlet(m);
400 }
401
402 shell.pack ();
403 shell.open ();
404 while (!shell.isDisposed ()) {
405 if (!display.readAndDispatch ())
406 display.sleep ();
407 }
408 display.dispose ();
409 }
410
411 System.exit(0);
412 }
413
414 }