summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/StandaloneApplet.java
blob: cf8a5d5ad563a2d3f40dd879fba3766409ba4c88 (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
114
115
116
117
118
119
120
121
package de.spline.kvm;

import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;

import de.spline.kvm.events.EventMulticaster;
import de.spline.kvm.events.LifetimeListener;
import de.spline.kvm.utils.ReflectionUtils;
import nn.pp.rc.RemoteConsoleApplet;
import nn.pp.rc.ServerConsolePanelBase;

/**
 * This is a class, to run an applet without an browser. It just overrides the
 * necessary methods to run the applet. The applet "window" displayed in tbe
 * browser is not drawn, but the interesting stuff (aka. the remote console)
 * is in a separate Frame.
 */
public class StandaloneApplet extends RemoteConsoleApplet
{
    protected String host;
    protected Map<String, String> parameters;

    protected LifetimeListener lifetimeListener = null;

    public StandaloneApplet(String host, Map<String, String> parameters)
    {
        this.host = host;
        this.parameters = parameters;
    }

    /**
     * Add a listener for the lifetime events of the applet.
     *
     * @param listener Listener instance to add
     */
    public void addLifetimeListener(LifetimeListener listener)
    {
        lifetimeListener = EventMulticaster.add(listener, lifetimeListener);
    }

    /**
     * Remove a listener for the lifetime events of the applet.
     *
     * @param listener Listener instance to remove
     */
    public void removeLifetimeListener(LifetimeListener listener)
    {
        lifetimeListener = EventMulticaster.remove(listener, lifetimeListener);
    }

    @Override
    public Image getImage(URL url)
    {
        return getToolkit().getImage(url);
    }

    @Override
    public URL getCodeBase()
    {
        URL url = null;

        try {
            url = new URL("https", host, 443, "");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return url;
    }

    @Override
    public URL getDocumentBase()
    {
        return getCodeBase();
    }

    @Override
    public Locale getLocale()
    {
        return Locale.getDefault();
    }

    /**
     * This are the parameters of the applet. The applet usually get the values
     * from html tags inside the applet tag. We have parsed this tags into a
     * map and simply return these values here.
     *
     * @param name Name of the parameter to get
     * @return Value of the parameter
     */
    @Override
    public String getParameter(String name)
    {
        return parameters.get(name.toUpperCase());
    }

    @Override
    public void init()
    {
        super.init();

        // The initial label is removed during init and this is causing
        // NullPointerExceptions when the popup should be displayed, so we
        // simply add the label again.
        ServerConsolePanelBase consolePanel = ReflectionUtils.getConsolePanel(this);
        PopupMenu menu = ReflectionUtils.getOptionMenu(consolePanel);
        menu.setLabel("Options");
    }

    @Override
    public void stop()
    {
        super.stop();
        destroy();

        lifetimeListener.appletStopped();
    }
}