/**
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * Copyright © 2009 Mindoo GmbH
 *
 * @author Tammo Riedinger
 *
 */
package com.mindoo.ec09.framework.application;

import java.awt.Component;
import java.awt.Container;
import java.awt.Window;

import javax.swing.JPanel;

public abstract class AbstractApplication {
    private Component window;
    protected Container contentPane;

    public AbstractApplication() {
    	window=createWindow();

    	configureWindow(window);
    }

	/**
	 * Launches the application.
	 */
	public void launch() {
	    startup();
	    
	    show();
	}

	protected void installContentPane(Component window, Container contentPane) {
		this.contentPane=contentPane;
	}

	protected void createActions(Component window, Container contentPane) {
	}

	protected void installMenuBar(Component window, Container contentPane) {
	}

	protected void installToolBars(Component window, Container contentPane) {
	}

	public Component getWindow() {
		return window;
	}

	public Container getContentPane() {
		return contentPane;
	}

	protected Container createContentPane() {
		return new JPanel();
	}

	protected void configureWindow(Component window) {
	}

	protected void configureContentPane(Container contentPane) {
	}

	protected void startup() {
    	Container contentPane=createContentPane();
    	
    	configureContentPane(contentPane);
    	
    	installContentPane(window, contentPane);
    	
    	createActions(window, contentPane);
    	
    	installMenuBar(window, contentPane);
    	installToolBars(window, contentPane);
	}

	protected void shutdown() {
	}

	public void exit() {
		shutdown();
	}

	public void show() {
		show(getWindow());
	}

	public void hide() {
		hide(getWindow());
	}

	protected void show(Component window) {
		if (window instanceof Window)
			((Window)window).pack();
		window.setVisible(true);
	}

	protected void hide(Component window) {
		window.setVisible(false);
	}

	protected abstract Component createWindow();
}