/**
 * An example of event bubbling
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class BubblingDriver
{
    private static Paragraph[]            paragraphs;
    private static Section                section;
    private static Word[]                 words;


    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
	BubblingEventQueue             eventq;
	BubblingEvent                  event;

	words = new Word[8];
	words[0] = new Word("This");
	words[1] = new Word("is");
	words[2] = new Word("paragraph");
	words[3] = new Word("one");
	words[4] = new Word("This");
	words[5] = new Word("is");
	words[6] = new Word("paragraph");
	words[7] = new Word("two");

	paragraphs = new Paragraph[2];
	paragraphs[0] = new Paragraph();
	for (int i=0; i<4; i++) words[i].setParent(paragraphs[0]);

	paragraphs[1] = new Paragraph();
	for (int i=4; i<8; i++) words[i].setParent(paragraphs[1]);

	section = new Section();
	paragraphs[0].setParent(section);
	paragraphs[1].setParent(section);

	eventq = BubblingEventQueue.getEventQueue();
	eventq.start();

        System.out.println("Initial Document");
        print();

        // Change the face for the Section (can't bubble)
	event = new BubblingEvent(section, "FaceChange", "Futura");
	eventq.postEvent(event);
	try { Thread.sleep(1000); } catch (InterruptedException ie) {}
        System.out.println("\n\nAfter Section face change");
        print();
        
        // Change the style for a Word (shouldn't bubble)
	event = new BubblingEvent(words[4], "StyleChange", "Italic");
	eventq.postEvent(event);
	try { Thread.sleep(1000); } catch (InterruptedException ie) {}
        System.out.println("\n\nAfter Word style change");
	print();

        // Change the size for a Paragraph (shouldn't bubble)
	event = new BubblingEvent(paragraphs[0], "SizeChange", "12");
	eventq.postEvent(event);
	try { Thread.sleep(1000); } catch (InterruptedException ie) {}
        System.out.println("\n\nAfter Paragraph size change");
	print();

        // Change the face for a Word (should bubble to Section
	event = new BubblingEvent(words[0], "FaceChange", "Garamond");
	eventq.postEvent(event);
	try { Thread.sleep(1000); } catch (InterruptedException ie) {}
        System.out.println("\n\nAfter Word face change");
	print();
    }


    /**
     * Prnt the current state
     */
    private static void print()
    {
	section.print(System.out);
        paragraphs[0].print(System.out);
        for (int i=0; i<4; i++) words[i].print(System.out);           

        paragraphs[1].print(System.out);
        for (int i=4; i<8; i++) words[i].print(System.out);           
    }
    
}
