001: /* Thinlet GUI toolkit - www.thinlet.com
002: * Copyright (C) 2002-2003 Robert Bajzat (robert.bajzat@thinlet.com) */
003: package thinlet;
004:
005: import java.applet.*;
006: import java.awt.*;
007:
008: /**
009: * <code>AppletLauncher</code> is a double buffered applet
010: * to launch any <i>thinlet</i> component
011: */
012: public class AppletLauncher extends Applet implements Runnable {
013:
014: private transient Thinlet content;
015: private transient Image doublebuffer;
016:
017: /**
018: * Applet instance is created by the browser or applet viewer
019: */
020: public AppletLauncher() {
021: super (); // for javadoc
022: }
023:
024: /**
025: * Called by the browser to inform this applet that it has been loaded into
026: * the system, it displays the <i>Loading...</i> label and starts the loader thread
027: */
028: public void init() {
029: setBackground(Color.white);
030: setForeground(Color.darkGray);
031: setLayout(new BorderLayout());
032: add(new Label("Loading...", Label.CENTER), BorderLayout.CENTER);
033: new Thread(this ).start();
034: }
035:
036: /**
037: * Create a new <i>thinlet</i> instance of the class given as
038: * <code>class</code> applet parameter, and show it or the
039: * message of the thrown exception. First try a contructor with
040: * an applet parameter (thus you get this applet instance e.g. for
041: * the parameters of the applet HTML tag), then the empty constructor
042: */
043: public void run() {
044: try {
045: Class thinletclass = Class.forName(getParameter("class"));
046: try {
047: content = (Thinlet) thinletclass.getConstructor(
048: new Class[] { Applet.class }).newInstance(
049: new Object[] { this });
050: } catch (NoSuchMethodException nsme) {
051: content = (Thinlet) thinletclass.newInstance();
052: }
053: removeAll();
054: add(content, BorderLayout.CENTER);
055: } catch (Throwable exc) {
056: removeAll();
057: add(new Label(exc.getClass().getName() + " "
058: + exc.getMessage(), Label.CENTER),
059: BorderLayout.CENTER);
060: }
061: doLayout();
062: repaint();
063: }
064:
065: /**
066: * Clear the double buffer image, the overriden method lays out its
067: * components (centers the <i>thinlet</i> component)
068: */
069: public void doLayout() {
070: super .doLayout();
071: if (doublebuffer != null) {
072: doublebuffer.flush();
073: doublebuffer = null;
074: }
075: }
076:
077: /**
078: * Called by the browser to inform this applet that it should stop its execution,
079: * it clears the double buffer image
080: */
081: public void stop() {
082: if (doublebuffer != null) {
083: doublebuffer.flush();
084: doublebuffer = null;
085: }
086: }
087:
088: /**
089: * Call the paint method to redraw this component without painting a
090: * background rectangle
091: */
092: public void update(Graphics g) {
093: paint(g);
094: }
095:
096: /**
097: * Create a double buffer if needed,
098: * the <i>thinlet</i> component paints the content
099: */
100: public void paint(Graphics g) {
101: if (doublebuffer == null) {
102: Dimension d = getSize();
103: doublebuffer = createImage(d.width, d.height);
104: }
105: Graphics dg = doublebuffer.getGraphics();
106: dg.setClip(g.getClipBounds());
107: super .paint(dg);
108: dg.dispose();
109: g.drawImage(doublebuffer, 0, 0, this );
110: }
111:
112: /**
113: * Called by the browser to inform this applet that it is being reclaimed,
114: * it calls the <i>thinlet</i> component's <code>destroy</code> method
115: * (its return value is irrelevant)
116: */
117: public void destroy() {
118: content.destroy();
119: }
120: }
|