/**
 * 
 * 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.legacy;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;


public class LegacyApplication extends AbstractLegacyApplication {
	@Override
    protected void installMenuBar(Component window, Container contentPane) {
	    JMenuBar mb = createMenuBar(contentPane);
	
		if (window instanceof JFrame) {
			((JFrame)window).setJMenuBar(mb);
		}
		else
			throw new RuntimeException("Unknown type of application window");
	}
	
	@Override
	protected void installContentPane(Component window, Container contentPane) {
		super.installContentPane(window, contentPane);
		if (window instanceof JFrame) {
			JPanel panel=new JPanel(new BorderLayout()) {
				private static final long serialVersionUID = -177381675357084800L;

				@Override
				public Component add(Component c) {
					if (c instanceof JToolBar) {
						super.add(c,BorderLayout.NORTH);
						return c;
					}
					return super.add(c);
				}
			};
			
			panel.add(contentPane,BorderLayout.CENTER);
			
			((JFrame)window).setContentPane(panel);
		}
		else
			throw new RuntimeException("Unknown type of application window");
	}

    @Override
    protected void show(Component window) {
    	if (window instanceof JFrame)
    		((JFrame)window).setVisible(true);
    }
    
    @Override
    protected void hide(Component window) {
    	if (window instanceof JFrame)
    		((JFrame)window).setVisible(false);
    }
    
    @Override
    protected Component createWindow() {
		JFrame frame=new JFrame("Legacy application");
		frame.setSize(400,400);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent evt) {
				exit();
			}
		});
		
		return frame;
	}
    
    @Override
	public void exit() {
		super.exit();
		
		System.exit(-1);
	}
    
    public static void main(String[] args) {
    	createAndLaunch();
    }

    private static void createAndLaunch() {
		if (!SwingUtilities.isEventDispatchThread()) {
	        SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	            	createAndLaunch();
	            }
	        });
	        return;
		}
		
	    new LegacyApplication().launch();
    }
}