/* * [Broadcaster.java] * * Summary: Broadcasts changed country to other applets on the page. Runs in a separate thread. * * Copyright: (c) 2001-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: * 3.5 2008-09-19 allow commas in parameter amount values. */ package com.mindprod.currcon; import java.applet.Applet; import java.applet.AppletContext; import java.util.Enumeration; import static java.lang.System.*; /** * Broadcasts changed country to other applets on the page. Runs in a separate thread. * * @author Roedy Green, Canadian Mind Products * @version 3.5 2008-09-19 allow commas in parameter amount values. * @since 2001 */ class Broadcaster implements Runnable { /** * instance of CurrCon Applet that first noticed the change. */ private final CurrCon self; /** * new currency index we broadcast */ private final int currentCurrIndex; /** * constructor * * @param currentCurrIndex New currency index, 0..N, e.g. dollars, pounds.. * @param self Applet instance doing the broadcasting. */ Broadcaster( CurrCon self, final int currentCurrIndex ) { this.self = self; this.currentCurrIndex = currentCurrIndex; } /** * broadcast to all CurrCon Applets on this page, including ourself. * Runs on a background thread to notify all other Applets. */ public void run() { // We have to get everyone to notice currentCurrIndex has changed, and // recalculate their amounts. final AppletContext ac = self.getAppletContext(); if ( ac != null ) { // all Applets on page, possibly including us, including non-CurrCon final Enumeration otherApplets = ac.getApplets(); if ( otherApplets == null || !otherApplets.hasMoreElements() ) { if ( CurrCon.DEBUGGING ) { out.println( self.getInstance() + " sees no other Applets" ); } } else { while ( otherApplets.hasMoreElements() ) { // Broadcast to all other Applets. List may or may not include ourself. // Exclude ourself just in case final Object otherApplet = otherApplets.nextElement(); if ( otherApplet instanceof CurrCon && otherApplet != self ) { if ( CurrCon.DEBUGGING ) { out.println( self.getInstance() + " notifying " + ( ( CurrCon ) otherApplet ).getInstance() ); } // other instances will use our thread to update the screen ( ( CurrCon ) otherApplet ).currencyChangeListener( currentCurrIndex ); } else { if ( CurrCon.DEBUGGING ) { out.println( self.getInstance() + " seeing an applet that does not need notifying" ); } } } // end while } } } // end run }