summaryrefslogtreecommitdiffstats
path: root/khtml/java/org/kde/kjas/server/KJASAppletPanel.java
blob: d7acbdaf9c4e8c8aecb585e0f0173b197622e7f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.kde.kjas.server;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.net.URL;

/**
 * @author till
 *
 * A panel which embeds the applet and shows some
 * information during class loading.
 */
public class KJASAppletPanel extends javax.swing.JPanel implements StatusListener {
    private final static int LOADING = 1;
    private final static int RUNNING = 2;
    private final static int FAILED = 3;

    private Image load_img = null;
    private Image fail_img = null;
    private int status = LOADING;
	private Font font;
	private String msg = "Loading Applet...";
	
	/**
	 * Constructor for KJASAppletPanel.
	 */
	public KJASAppletPanel() {
		super(new BorderLayout());
		font = new Font("SansSerif", Font.PLAIN, 10);
		URL url =
			getClass().getClassLoader().getResource("images/animbean.gif");
		load_img = getToolkit().createImage(url);
		//setBackground(Color.white);
	}

	void setApplet(Applet applet) {
		add("Center", applet);
		validate();
	}

    public void showStatus(String msg) {
        this.msg = msg;
        if (status != RUNNING)
            repaint();
    }

	public void paint(Graphics g) {
		super.paint(g);
        if (status == RUNNING)
            return;
        Image img = (status == LOADING ? load_img : fail_img);
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        if (img != null) {
            //synchronized (img) {
            int w = img.getWidth(this);
            int h = img.getHeight(this);
            int imgx = x - w / 2;
            int imgy = y - h / 2;
            //g.setClip(imgx, imgy, w, h);
            g.drawImage(img, imgx, imgy, this);
            y += img.getHeight(this) / 2;
            //}
        }
        if (msg != null) {
            //synchronized(msg) {
            g.setFont(font);
            FontMetrics m = g.getFontMetrics();
            int h = m.getHeight();
            int w = m.stringWidth(msg);
            int msgx = x - w / 2;
            int msgy = y + h;
            //g.setClip(0, y, getWidth(), h);
            g.drawString(msg, msgx, msgy);
            //}
        }
	}
	void showFailed() {
		URL url =
			getClass().getClassLoader().getResource("images/brokenbean.gif");
		fail_img = getToolkit().createImage(url);
        status = FAILED;
		msg = "Applet Failed.";
		repaint();
	}

	void showFailed(String message) {
		showFailed();
		showStatus(message);
	}

	public void stopAnimation() {
        status = RUNNING;
	}

    public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
    {
        if (img != null && img == load_img && status != LOADING) {
            img.flush();
            load_img = null;
            Main.debug("flushing image");
            return false;
        }
        return super.imageUpdate(img, flags, x, y, w, h);
    }
}