Listing 3: An application that uses the SwitchBoard

package com.crosoft.iom;

import com.crosoft.iom.SwitchBoard;
import com.crosoft.iom.SwitchBoardAdapter;

class PersonnelList
{
    PersonnelList()
    {
        // add the SwitchBoard subscriptions for saveYourself 
        // and appNotBusy
        SwitchBoard.subscribeTo("saveYourself", 
            new SwitchBoardAdapter() {
                    public void 
                    deliver(String subscription){ store(); }
                }
        );
    }

    // PersonnelList code would go here ...

    /**
     * store() is called through the "saveYourself" message 
     * as notified via the SwitchBoard
     */
    public void store()
    {
        // use the SwitchBoard to update the application state
        SwitchBoard.notify("busy", "PersonnelList - saving...");
        SwitchBoard.notify("setStatus", "saved.");
        SwitchBoard.notify("notBusy");
    }
}


class ApplicationShell
{
    private int _busyness = 0;

    ApplicationShell()
    {
        // add the SwitchBoard subscriptions for busy, notBusy, 
        // and setStatus
        SwitchBoard.subscribeTo("busy", 
            new SwitchBoardAdapter() {
                    public void 
                    deliver(String subscription){ busy(); } 
                }
        );
        SwitchBoard.subscribeTo("busy", 
            new SwitchBoardAdapter() {
                    public void 
                    deliver(String subscription, String msg)
                    { busy(msg); } 
                }
        );
        SwitchBoard.subscribeTo("notBusy", 
            new SwitchBoardAdapter() {
                    public void 
                    deliver(String subscription){ notBusy(); } 
                }
        );
        SwitchBoard.subscribeTo("setStatus", 
            new SwitchBoardAdapter() {
                    public void 
                    deliver(String subscription, String msg)  
                    { setStatus(msg); } 
                }
        );
    }

    // -- busyness management center ---------------------------
    ////
    /**
     * busy() can be called from any object by way of 
     * SwitchBoard.notify "busy".
     */
    public void busy()
    {
        // tell each subscribing object to switch its cursor to 
        // the busy one
        if (_busyness == 0)
            SwitchBoard.notify("setBusyCursor");

        // increment the reference count so notBusy() will know 
        // when to stop
        _busyness++;
    }

    /**
     * busy(String) calls setStatus() and busy() to effectively 
     * busy the application and tell the user why
     */
    public void busy(String msg)
    {
        setStatus(msg);
        busy();
    }

    /**
     * notBusy() undoes a call to busy()
     */
    public void notBusy()
    {
        if (_busyness > 0)
        {
            if (--_busyness == 0)
            {
                // restore the default status
                setStatus("Ready.");

                // notify all subscribing objects that the app 
                // is no longer busy
                SwitchBoard.notify("restoreDefaultCursor");
            }
        }
    }

    /**
     * setStatus() for this example just prints the status message 
     * and the busyness.
     */
    public void setStatus(String msg)
    {
        for (int i = 0; i < _busyness; i++)
            System.err.print(" ");
        System.err.println(msg);
    }

    // -- example methods --------------------------------------
    ////
    public void refresh()
    {
        SwitchBoard.notify("busy", "Refreshing...");

        // refresh the application ...

        SwitchBoard.notify("notBusy");
    }

    public void save()
    {
        // switch the application into a busy state
        SwitchBoard.notify("busy", 
            "Saving the application state...");

        // code to save the application state ...

        // notify all the persistent objects that it's now time 
        // to save
        SwitchBoard.notify("saveYourself");

        refresh();

        // return to a normal state
        SwitchBoard.notify("notBusy");
    }


    // -- main -------------------------------------------------
    ////
    public static void main(String args[])
    {
        // instantiate an ApplicationShell
        ApplicationShell app = new ApplicationShell();

        // just for kicks, declare a personnel list 
        // outside of the application
        PersonnelList pl = new PersonnelList();

        // save the application state
        app.save();

        // we're done
        System.exit(0);
    }
}

//End of File