Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/barteo/emulator/app/Common.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.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.Enumeration;
28  
29  import javax.microedition.midlet.MIDlet;
30  import javax.microedition.midlet.MIDletStateChangeException;
31  
32  import com.barteo.emulator.EmulatorContext;
33  import com.barteo.emulator.MIDletBridge;
34  import com.barteo.emulator.MIDletEntry;
35  import com.barteo.emulator.MicroEmulator;
36  //import com.barteo.emulator.app.capture.Capturer;
37  import com.barteo.emulator.app.launcher.Launcher;
38  import com.barteo.emulator.app.ui.ResponseInterfaceListener;
39  import com.barteo.emulator.app.ui.StatusBarListener;
40  import com.barteo.emulator.app.util.ProgressEvent;
41  import com.barteo.emulator.app.util.ProgressJarClassLoader;
42  import com.barteo.emulator.app.util.ProgressListener;
43  import com.barteo.emulator.device.Device;
44  import com.barteo.emulator.device.DeviceFactory;
45  import com.barteo.emulator.util.JadMidletEntry;
46  import com.barteo.emulator.util.JadProperties;
47  
48  
49  public class Common implements MicroEmulator 
50  {
51    private static Launcher launcher;
52    
53      protected EmulatorContext emulatorContext;
54    
55      protected String captureFile = null;
56  //    private Capturer capturer = null;
57      
58    protected JadProperties jad = new JadProperties();
59    protected JadProperties manifest = new JadProperties();
60    
61    private StatusBarListener statusBarListener = null; 
62    private ResponseInterfaceListener responseInterfaceListener = null; 
63    
64    private ProgressListener progressListener = new ProgressListener()
65    {
66      int percent = -1;
67      
68      public void stateChanged(ProgressEvent event)
69      {
70        int newpercent = (int) ((float) event.getCurrent() / (float) event.getMax() * 100);
71        
72        if (newpercent != percent) {
73          setStatusBar("Loading... (" + newpercent +" %)");
74          percent = newpercent;
75        }
76      }    
77    };
78  
79    
80    public Common(EmulatorContext context)
81    {
82      emulatorContext = context;
83  
84      launcher = new Launcher();
85      launcher.setCurrentMIDlet(launcher);     
86  
87      MIDletBridge.setMicroEmulator(this);
88    }
89    
90    
91    public String getAppProperty(String key)
92    {
93      String result = jad.getProperty(key);
94      
95      if (result == null) {
96        result = manifest.getProperty(key);
97      }
98      
99      return result; 
100   }
101 
102   
103   public void notifyDestroyed()
104   {
105     startMidlet(launcher);
106   }
107   
108 
109   public void notifySoftkeyLabelsChanged() 
110   {
111   }
112   
113   
114   public Launcher getLauncher()
115   {
116     return launcher;
117   }
118   
119   
120   public MIDlet loadMidlet(String name, Class midletClass)
121   {
122     MIDlet result;
123     
124     try {
125       result = (MIDlet) midletClass.newInstance();
126       launcher.addMIDletEntry(new MIDletEntry(name, result));
127       launcher.setCurrentMIDlet(result);
128     } catch (Exception ex) {
129       System.out.println("Cannot initialize " + midletClass + " MIDlet class");
130       System.out.println(ex);
131       ex.printStackTrace();
132       return null;
133     }  
134     
135     return result;
136   }
137 
138 
139   public void openJadFile(URL url)
140   {
141     try {
142       setStatusBar("Loading...");
143       jad.clear();
144       jad.load(url.openStream());
145       loadFromJad(url);
146     } catch (FileNotFoundException ex) {
147       System.err.println("Cannot found " + url.getPath());
148     } catch (NullPointerException ex) {
149       ex.printStackTrace();
150       System.err.println("Cannot open jad " + url.getPath());
151     } catch (IllegalArgumentException ex) {
152       ex.printStackTrace();
153       System.err.println("Cannot open jad " + url.getPath());
154     } catch (IOException ex) {
155       ex.printStackTrace();
156       System.err.println("Cannot open jad " + url.getPath());
157     }
158   }
159 
160 
161   public void startMidlet(MIDlet m)
162   {
163     try {
164       MIDletBridge.getMIDletAccess(m).startApp();
165     } catch (MIDletStateChangeException ex) {
166       System.err.println(ex);
167     }
168   }
169   
170   
171   public void setStatusBarListener(StatusBarListener listener)
172   {
173     statusBarListener = listener;
174   }
175   
176 
177   public void setResponseInterfaceListener(ResponseInterfaceListener listener)
178   {
179     responseInterfaceListener = listener;
180   }
181   
182   
183   protected void close()
184   {
185     if (captureFile != null) {
186 //      if (capturer != null) {
187 //        capturer.stopCapture(emulatorContext.getDisplayComponent());
188 //      }
189     }    
190   }  
191   
192 
193   protected void loadFromJad(URL jadUrl)
194   {
195     final ProgressJarClassLoader loader = (ProgressJarClassLoader) emulatorContext.getClassLoader();
196     
197     setResponseInterface(false);
198     URL url = null;
199     try {
200       if (jadUrl.getProtocol().equals("file")) {
201         String tmp = jadUrl.getFile();
202         File f = new File(tmp.substring(0, tmp.lastIndexOf('/')), jad.getJarURL());
203         url = f.toURL();
204       } else {
205         url = new URL(jad.getJarURL());
206       }
207     } catch (MalformedURLException ex) {
208       System.err.println(ex);
209       setResponseInterface(true);
210     }
211     
212     loader.addRepository(url);
213     launcher.removeMIDletEntries();
214     
215 
216     manifest.clear();
217     try {
218       manifest.load(loader.getResourceAsStream("/META-INF/MANIFEST.MF"));
219     } catch (IOException ex) {
220       ex.printStackTrace();
221     }          
222 
223     Thread task = new Thread() 
224     {
225       
226       public void run()
227       {
228         loader.setProgressListener(progressListener);
229         try {
230           for (Enumeration e = jad.getMidletEntries().elements(); e.hasMoreElements(); ) {
231             JadMidletEntry jadEntry = (JadMidletEntry) e.nextElement();
232             Class midletClass = loader.loadClass(jadEntry.getClassName());
233             loadMidlet(jadEntry.getName(), midletClass);
234           }
235           notifyDestroyed();
236         } catch (ClassNotFoundException ex) {
237           System.err.println(ex);
238         }        
239         loader.setProgressListener(null);
240         setStatusBar("");
241         setResponseInterface(true);
242       }
243       
244     };
245     
246     task.start();
247   }
248   
249   
250   protected void setDevice(Device device)
251   {
252     if (captureFile != null) {
253 //      if (capturer != null) {
254 //        capturer.stopCapture(emulatorContext.getDisplayComponent());
255 //      }
256     }
257 
258     DeviceFactory.setDevice(device);
259     
260     if (captureFile != null) {
261 //      if (capturer == null) {
262 //        capturer = new Capturer();
263 //      }
264 //      capturer.startCapture(emulatorContext.getDisplayComponent(), captureFile);
265     }
266   }
267 
268 
269   private void setStatusBar(String text)
270   {
271     if (statusBarListener != null) {
272       statusBarListener.statusBarChanged(text);
273     }
274   }
275   
276   
277   private void setResponseInterface(boolean state)
278   {
279     if (responseInterfaceListener != null) {
280       responseInterfaceListener.stateChanged(state);
281     }
282   }
283     
284 }