OSDN Git Service

Change .gitignore.
[mikumikustudio/MikuMikuStudio.git] / engine / src / desktop / com / jme3 / app / AppletHarness.java
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
33 package com.jme3.app;
34
35 import com.jme3.system.AppSettings;
36 import com.jme3.system.JmeCanvasContext;
37 import com.jme3.system.JmeSystem;
38 import java.applet.Applet;
39 import java.awt.Canvas;
40 import java.awt.Graphics;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.util.HashMap;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
48 import javax.swing.JOptionPane;
49 import javax.swing.SwingUtilities;
50
51 /**
52  * @author Kirill Vainer
53  */
54 public class AppletHarness extends Applet {
55
56     public static final HashMap<Application, Applet> appToApplet
57                          = new HashMap<Application, Applet>();
58
59     private JmeCanvasContext context;
60     private Canvas canvas;
61     private Application app;
62
63     private String appClass;
64     private URL appCfg = null;
65     private URL assetCfg = null;
66
67     public static Applet getApplet(Application app){
68         return appToApplet.get(app);
69     }
70
71     private void createCanvas(){
72         AppSettings settings = new AppSettings(true);
73
74         // load app cfg
75         if (appCfg != null){
76             InputStream in = null;
77             try {
78                 in = appCfg.openStream();
79                 settings.load(in);
80                 in.close();
81             } catch (IOException ex){
82                 // Called before application has been created ....
83                 // Display error message through AWT
84                 JOptionPane.showMessageDialog(this, "An error has occured while "
85                                                   + "loading applet configuration"
86                                                   + ex.getMessage(),
87                                               "jME3 Applet",
88                                               JOptionPane.ERROR_MESSAGE);
89                 ex.printStackTrace();
90             } finally {
91                 if (in != null)
92                     try {
93                     in.close();
94                 } catch (IOException ex) {
95                 }
96             }
97         }
98
99         if (assetCfg != null){
100             settings.putString("AssetConfigURL", assetCfg.toString());
101         }
102
103         settings.setWidth(getWidth());
104         settings.setHeight(getHeight());
105
106         JmeSystem.setLowPermissions(true);
107
108         try{
109             Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
110             app = clazz.newInstance();
111         }catch (ClassNotFoundException ex){
112             ex.printStackTrace();
113         }catch (InstantiationException ex){
114             ex.printStackTrace();
115         }catch (IllegalAccessException ex){
116             ex.printStackTrace();
117         }
118
119         appToApplet.put(app, this);
120         app.setSettings(settings);
121         app.createCanvas();
122
123         context = (JmeCanvasContext) app.getContext();
124         canvas = context.getCanvas();
125         canvas.setSize(getWidth(), getHeight());
126
127         add(canvas);
128         app.startCanvas();
129     }
130
131     @Override
132     public final void update(Graphics g) {
133         canvas.setSize(getWidth(), getHeight());
134     }
135
136     @Override
137     public void init(){
138         appClass = getParameter("AppClass");
139         if (appClass == null)
140             throw new RuntimeException("The required parameter AppClass isn't specified!");
141
142         try {
143             appCfg = new URL(getParameter("AppSettingsURL"));
144         } catch (MalformedURLException ex) {
145             System.out.println(ex.getMessage());
146             appCfg = null;
147         }
148
149         try {
150             assetCfg = new URL(getParameter("AssetConfigURL"));
151         } catch (MalformedURLException ex){
152             System.out.println(ex.getMessage());
153             assetCfg = getClass().getResource("/com/jme3/asset/Desktop.cfg");
154         }
155
156         createCanvas();
157         System.out.println("applet:init");
158     }
159
160     @Override
161     public void start(){
162         context.setAutoFlushFrames(true);
163         System.out.println("applet:start");
164     }
165
166     @Override
167     public void stop(){
168         context.setAutoFlushFrames(false);
169         System.out.println("applet:stop");
170     }
171
172     @Override
173     public void destroy(){
174         System.out.println("applet:destroyStart");
175         SwingUtilities.invokeLater(new Runnable(){
176             public void run(){
177                 removeAll();
178                 System.out.println("applet:destroyRemoved");
179             }
180         });
181         app.stop(true);
182         System.out.println("applet:destroyDone");
183
184         appToApplet.remove(app);
185     }
186
187 }