1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.jme3.app;
34
35import com.jme3.system.AppSettings;
36import com.jme3.system.JmeCanvasContext;
37import com.jme3.system.JmeSystem;
38import java.applet.Applet;
39import java.awt.Canvas;
40import java.awt.Graphics;
41import java.io.IOException;
42import java.io.InputStream;
43import java.net.MalformedURLException;
44import java.net.URL;
45import java.util.HashMap;
46import javax.swing.JOptionPane;
47import javax.swing.SwingUtilities;
48
49/**
50 * @author Kirill Vainer
51 */
52public class AppletHarness extends Applet {
53
54    public static final HashMap<Application, Applet> appToApplet
55                         = new HashMap<Application, Applet>();
56
57    private JmeCanvasContext context;
58    private Canvas canvas;
59    private Application app;
60
61    private String appClass;
62    private URL appCfg = null;
63    private URL assetCfg = null;
64
65    public static Applet getApplet(Application app){
66        return appToApplet.get(app);
67    }
68
69    private void createCanvas(){
70        AppSettings settings = new AppSettings(true);
71
72        // load app cfg
73        if (appCfg != null){
74            InputStream in = null;
75            try {
76                in = appCfg.openStream();
77                settings.load(in);
78                in.close();
79            } catch (IOException ex){
80                // Called before application has been created ....
81                // Display error message through AWT
82                JOptionPane.showMessageDialog(this, "An error has occured while "
83                                                  + "loading applet configuration"
84                                                  + ex.getMessage(),
85                                              "jME3 Applet",
86                                              JOptionPane.ERROR_MESSAGE);
87                ex.printStackTrace();
88            } finally {
89                if (in != null)
90                    try {
91                    in.close();
92                } catch (IOException ex) {
93                }
94            }
95        }
96
97        if (assetCfg != null){
98            settings.putString("AssetConfigURL", assetCfg.toString());
99        }
100
101        settings.setWidth(getWidth());
102        settings.setHeight(getHeight());
103
104        JmeSystem.setLowPermissions(true);
105
106        try{
107            Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
108            app = clazz.newInstance();
109        }catch (ClassNotFoundException ex){
110            ex.printStackTrace();
111        }catch (InstantiationException ex){
112            ex.printStackTrace();
113        }catch (IllegalAccessException ex){
114            ex.printStackTrace();
115        }
116
117        appToApplet.put(app, this);
118        app.setSettings(settings);
119        app.createCanvas();
120
121        context = (JmeCanvasContext) app.getContext();
122        canvas = context.getCanvas();
123        canvas.setSize(getWidth(), getHeight());
124
125        add(canvas);
126        app.startCanvas();
127    }
128
129    @Override
130    public final void update(Graphics g) {
131        canvas.setSize(getWidth(), getHeight());
132    }
133
134    @Override
135    public void init(){
136        appClass = getParameter("AppClass");
137        if (appClass == null)
138            throw new RuntimeException("The required parameter AppClass isn't specified!");
139
140        try {
141            appCfg = new URL(getParameter("AppSettingsURL"));
142        } catch (MalformedURLException ex) {
143            System.out.println(ex.getMessage());
144            appCfg = null;
145        }
146
147        try {
148            assetCfg = new URL(getParameter("AssetConfigURL"));
149        } catch (MalformedURLException ex){
150            System.out.println(ex.getMessage());
151            assetCfg = getClass().getResource("/com/jme3/asset/Desktop.cfg");
152        }
153
154        createCanvas();
155        System.out.println("applet:init");
156    }
157
158    @Override
159    public void start(){
160        context.setAutoFlushFrames(true);
161        System.out.println("applet:start");
162    }
163
164    @Override
165    public void stop(){
166        context.setAutoFlushFrames(false);
167        System.out.println("applet:stop");
168    }
169
170    @Override
171    public void destroy(){
172        System.out.println("applet:destroyStart");
173        SwingUtilities.invokeLater(new Runnable(){
174            public void run(){
175                removeAll();
176                System.out.println("applet:destroyRemoved");
177            }
178        });
179        app.stop(true);
180        System.out.println("applet:destroyDone");
181
182        appToApplet.remove(app);
183    }
184
185}
186