KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > AppletLauncher


1 /* Thinlet GUI toolkit - www.thinlet.com
2  * Copyright (C) 2002-2003 Robert Bajzat (robert.bajzat@thinlet.com) */

3 package thinlet;
4
5 import java.applet.*;
6 import java.awt.*;
7
8 /**
9  * <code>AppletLauncher</code> is a double buffered applet
10  * to launch any <i>thinlet</i> component
11  */

12 public class AppletLauncher extends Applet implements Runnable JavaDoc {
13     
14     private transient Thinlet content;
15     private transient Image doublebuffer;
16     
17     /**
18      * Applet instance is created by the browser or applet viewer
19      */

20     public AppletLauncher() {
21         super(); // for javadoc
22
}
23     
24     /**
25      * Called by the browser to inform this applet that it has been loaded into
26      * the system, it displays the <i>Loading...</i> label and starts the loader thread
27      */

28     public void init() {
29         setBackground(Color.white); setForeground(Color.darkGray);
30         setLayout(new BorderLayout());
31         add(new Label("Loading...", Label.CENTER), BorderLayout.CENTER);
32         new Thread JavaDoc(this).start();
33     }
34     
35     /**
36      * Create a new <i>thinlet</i> instance of the class given as
37      * <code>class</code> applet parameter, and show it or the
38      * message of the thrown exception. First try a contructor with
39      * an applet parameter (thus you get this applet instance e.g. for
40      * the parameters of the applet HTML tag), then the empty constructor
41      */

42     public void run() {
43         try {
44             Class JavaDoc thinletclass = Class.forName(getParameter("class"));
45             try {
46                 content = (Thinlet) thinletclass.getConstructor(new Class JavaDoc[] {
47                     Applet.class }).newInstance(new Object JavaDoc[] { this });
48             } catch (NoSuchMethodException JavaDoc nsme) {
49                 content = (Thinlet) thinletclass.newInstance();
50             }
51             removeAll();
52             add(content, BorderLayout.CENTER);
53         } catch (Throwable JavaDoc exc) {
54             removeAll();
55             add(new Label(exc.getClass().getName() + " " +
56                 exc.getMessage(), Label.CENTER), BorderLayout.CENTER);
57         }
58         doLayout(); repaint();
59     }
60     
61     /**
62      * Clear the double buffer image, the overriden method lays out its
63      * components (centers the <i>thinlet</i> component)
64      */

65     public void doLayout() {
66         super.doLayout();
67         if (doublebuffer != null) {
68             doublebuffer.flush();
69             doublebuffer = null;
70         }
71     }
72     
73     /**
74      * Called by the browser to inform this applet that it should stop its execution,
75      * it clears the double buffer image
76      */

77     public void stop() {
78         if (doublebuffer != null) {
79             doublebuffer.flush();
80             doublebuffer = null;
81         }
82     }
83     
84     /**
85      * Call the paint method to redraw this component without painting a
86      * background rectangle
87      */

88     public void update(Graphics g) {
89         paint(g);
90     }
91
92     /**
93      * Create a double buffer if needed,
94      * the <i>thinlet</i> component paints the content
95      */

96     public void paint(Graphics g) {
97         if (doublebuffer == null) {
98             Dimension d = getSize();
99             doublebuffer = createImage(d.width, d.height);
100         }
101         Graphics dg = doublebuffer.getGraphics();
102         dg.setClip(g.getClipBounds());
103         super.paint(dg);
104         dg.dispose();
105         g.drawImage(doublebuffer, 0, 0, this);
106     }
107     
108     /**
109      * Called by the browser to inform this applet that it is being reclaimed,
110      * it calls the <i>thinlet</i> component's <code>destroy</code> method
111      * (its return value is irrelevant)
112      */

113     public void destroy() {
114         content.destroy();
115     }
116 }
Popular Tags