/*
 * Copyright © 2000-2005 Canoo Engineering AG, Switzerland.
 */
package eventextension;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IEventListener;
import com.ulcjava.base.application.event.WindowEvent;
import com.ulcjava.base.client.UIFrame;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.server.IDispatcher;

public class WindowClosedEventSnippet1 extends AbstractApplication {
	public void start() {
		ULCBoxPane box = new ULCBoxPane(true);
		ULCButton but = new ULCButton("Dispose");
		
		box.add(but);
		
		final ULCMyFrame frame = new ULCMyFrame("WindowClosedEventSnippet1");
		
		but.addActionListener(new IActionListener() {
			public void actionPerformed(ActionEvent event) {
				frame.dispose();
			}
			
		});
		frame.add(box);
		frame.addWindowClosedListener(new IWindowClosedListener() {
			public void windowClosed(WindowClosedEvent event) {
				System.out.println("My Window closed");
				 //ApplicationContext.terminate();
			}
		});
		frame.setDefaultCloseOperation(ULCFrame.DISPOSE_ON_CLOSE);
		frame.setSize(300, 300);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		DevelopmentRunner
				.setApplicationClass(WindowClosedEventSnippet1.class);
		DevelopmentRunner.run();
	}

	// Server side	
	public static class ULCMyFrame extends ULCFrame {

		public ULCMyFrame(String string) {
			super(string);
			// workaround for the bug to enforce setting of DISPOSE on client
			setDefaultCloseOperation(ULCFrame.HIDE_ON_CLOSE);
		}

		public void dispose() {
			super.dispose();
			dispatchEvent("windowClosed", "windowClosed", new WindowClosedEvent(this));
		}

		protected IDispatcher createDispatcher() {
			return new ULCMyFrameDispatcher();
		}

		protected String typeString() {
			return UIMyFrame.class.getName();
		}
		
		public void addWindowClosedListener(IWindowClosedListener listener) {
			addListener("windowClosed", listener);
		}

		public void removeWindowClosedListener(IWindowClosedListener listener) {
			removeListener("windowClosed", listener);
		}

		protected WindowClosedEvent createWindowClosedEvent() {
			return new WindowClosedEvent(this);
		}
		
		protected Class getWindowClosedListenerClass() {
	        return IWindowClosedListener.class;
		}

		protected void processWindowClosedEvent(String eventCategory, String listenerMethodName, WindowClosedEvent event) {
			dispatchEvent(eventCategory, listenerMethodName, event);
		}
		
		public class ULCMyFrameDispatcher extends ULCFrame.ULCFrameDispatcher {
		    public final WindowClosedEvent createWindowClosedEvent() {
	            return ULCMyFrame.this.createWindowClosedEvent();
	        }
		    
		    public final Class getWindowClosedListenerClass() {
	            return ULCMyFrame.this.getWindowClosedListenerClass();
	        }
		    
		    public final void processWindowClosedEvent(String eventCategory, String listenerMethodName,	WindowClosedEvent event) {
		    		ULCMyFrame.this.processWindowClosedEvent(eventCategory,	listenerMethodName, event);
		    }
		    		
		}
	}

	public static class WindowClosedEvent extends WindowEvent {
		public WindowClosedEvent(Object source) {
			super(source);
		}
	}
		
	public static interface IWindowClosedListener extends IEventListener {
		public void windowClosed(WindowClosedEvent event);
	}
	
	// Client Side
	public static class UIMyFrame extends UIFrame {

		private boolean disposeWindowCalled = false;
		
		public void disposeWindow() {
			super.disposeWindow();
			disposeWindowCalled = true;
		}
		
		public void windowClosed(java.awt.event.WindowEvent e) {
			super.windowClosed(e);
			if (!disposeWindowCalled) {
				fireEventULC("windowClosed", "windowClosed", EMPTY_ARGUMENTS);
			} else {
				disposeWindowCalled = false;
			}
		}
	}
}
