/*
 * java-gnome, a UI library for writing GTK and GNOME programs from Java!
 * 
 * Copyright © 2010 Operational Dynamics Consulting, Pty Ltd
 *
 * The code in this file, and the program it is a part of, is made available
 * to you by its authors as open source software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License version
 * 2 ("GPL") as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GPL for more details.
 *
 * You should have received a copy of the GPL along with this program. If not,
 * see http://www.gnu.org/licenses/. The authors of this program may be
 * contacted through http://java-gnome.sourceforge.net/.
 */
import org.gnome.gdk.Event;
import org.gnome.gtk.Bin;
import org.gnome.gtk.Entry;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

public class Boom3 extends Window
{
    private final Window window;

    private Boom3() {
        super();
        final Label label;

        window = this;
        window.setTitle("boom");
        window.setDefaultSize(150, 100);

        label = new Label("Starting");
        window.add(label);

        window.connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        window.showAll();
    }

    public static void main(String[] args) {
        final Boom3 boom;
        final Thread draw;

        Gtk.init(args);
        boom = new Boom3();

        draw = new DrawingThread3(boom);
        draw.setDaemon(true);
        draw.start();

        Gtk.main();
    }
}

class DrawingThread3 extends Thread
{
    private Bin parent;

    DrawingThread3(Bin parent) {
        this.parent = parent;
    }

    public void run() {
        Widget child;
        Entry entry;
        org.gnome.gdk.Window underlying;
        int i;

        i = 0;

        try {
            while (true) {
                Thread.sleep(1000);
                i++;

                child = parent.getChild();
                parent.remove(child);

                entry = new Entry();
                entry.setText("Iteration " + i);

                parent.add(entry);
                parent.showAll();

                underlying = entry.getWindow();
                underlying = null;

                System.out.println(i);
                System.out.flush();

                System.gc();
                Thread.yield();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Gtk.mainQuit();
        }
    }
}
