/*
 * [MultiInstance.java]
 *
 * Summary: Demonstrate AppletContext.getApplets bug which fails for more than 11 instances.
 *
 * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2001-03-08 initial
 */
package com.mindprod.example;

import javax.swing.JApplet;
import javax.swing.JButton;
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import static java.lang.System.*;
// with 12+ instances FireFox, Opera and IE fail. Applets cannot see the other Applets.
// Chrome fails with 13+ instances.
// With <param  name="separate_jvm" value="false"> Chrome, ironically fails on 12+ instances.
// simple jar build: jar -cvfm multi.jar manifest.mf \com\mindprod\example\MultiInstance*.class
// manifest.mf looks like this:
//        Manifest-Version: 1.0
//        Application-Name: MultiInstance
//        Permissions: sandbox
//        Created-By: 1.8.0 (Oracle Corporation)
//        Main-Class: com.mindprod.example.MultiInstance
// run with multi.html
//that looks like this:
//        <!DOCTYPE HTML>
//        <html lang="en-CA">
//        <head>
//        <meta charset="utf-8">
//        </head>
//        <body>
//        <p>Click each applet to see which others it can see, listed on the console.</p>
//
//        <object  type="application/x-java-applet" data="multi.jar" width="50" height="20">
//        <param name="code" value="com.mindprod.example.MultiInstance">
//        <param name="archive" value="multi.jar">
//        <param name="instance" value="1">     <!-- unique instance identifier -->
//        Applet failed to run.
//        </object>
//
//        <object  type="application/x-java-applet" data="multi.jar" width="50" height="20">
//        <param name="code" value="com.mindprod.example.MultiInstance">
//        <param name="archive" value="multi.jar">
//        <param name="instance" value="2">
//        Applet failed to run.
//        </object>
//        ...      11 instances will work, 12 will fail.
//        </body>
//        </html>

/**
 * Demonstrate AppletContext.getApplets bug which fails for more than 11 instances.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2014-04-12 initial version
 * @since 2014-04-12
 */
public final class MultiInstance extends JApplet
    {
    /**
     * which instance on the page are we, unique string
     */
    private String instance = "unknown";

    /**
     * constructor
     */
    public MultiInstance()
        {
        }

    /**
     * which instance on the page are
     *
     * @return unique instance string
     */
    String getInstance()
        {
        return instance;
        }

    /**
     * dump out who can see what
     */
    public void dump()
        {
        // dump out what getApplets provides
        final AppletContext ac = this.getAppletContext();
        // all Applets on page, possibly including us, including non-MultiInstance
        final Enumeration<Applet> otherApplets = ac.getApplets();
        if ( otherApplets == null || !otherApplets.hasMoreElements() )
            {
            out.println( "instance " + instance + " unable to see any other applets" );
            }
        else
            {
            while ( otherApplets.hasMoreElements() )
                {
                Applet other = otherApplets.nextElement();
                if ( other instanceof MultiInstance )
                    {
                    out.println( instance + " can see: instance " + ( ( MultiInstance ) other ).getInstance() );
                    }
                else
                    {
                    out.println( instance + " can see: " + other.getAppletInfo() );
                    }
                }
            }
        }

    public void init()
        {
        instance = getParameter( "instance" );
        JButton t = new JButton( instance );
        t.setBackground( Color.orange );
        this.add( t );
        t.addActionListener( new ActionListener()
            {
            /**
             * Invoked when user clicks button
             */
            public void actionPerformed( ActionEvent e )
                {
                dump();
                }
            } );
        this.validate();
        this.setVisible( true );
        } // end init
    // has no main method.
    }