import org.gnome.gdk.Event;
import org.gnome.glib.ApplicationFlags;
import org.gnome.gtk.Application;
import org.gnome.gtk.Button;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

public class DeadlockExample {
	private static class Worker extends Thread {
		private Label l;

		private Worker(Label l) {
			this.l = l;
		}

		@Override
		public void run() {
			while (true) {
				l.setLabel(String.valueOf(System.currentTimeMillis()));

				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		final Application a;
		final Window w;
		final VBox x;
		final Label l;
		final Button b;
		final int s;

		Gtk.init(args);

		a = new Application("org.gnome.DeadlockTest", ApplicationFlags.NONE);

		w = new Window();
		a.addWindow(w);

		x = new VBox(false, 3);

		l = new Label("Go ahead:\nMake my day");
		x.add(l);

		b = new Button("Press me!");
		x.add(b);

		b.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				System.out.println("I was clicked: " + b.getLabel());
			}
		});

		w.add(x);
		w.setTitle("Hello World");

		w.connect(new Window.DeleteEvent() {
			public boolean onDeleteEvent(Widget source, Event event) {
				a.removeWindow(w);
				return false;
			}
		});

		a.connect(new Application.Activate() {
			public void onActivate(Application source) {
				System.out.println("Activated");
				w.showAll();
			}
		});

		new Worker(l).start();

		s = a.run(args);

		System.out.println("Status = " + s);
		System.exit(s);
	}
}
