package jp.ac.kyutech.mns.ist;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

public class ConfigWindow extends JFrame {

	private static final long serialVersionUID = -5175059774945480866L;

	final SimpleATN satn;
	JTabbedPane tabbedPane;

	public ConfigPanel_Draw draw;
	public ConfigPanel_Database database;
	public ConfigPanel_Seatmap seatmap;
	public ConfigPanel_PImageFinder pimgfinder;
	public ConfigPanel_Filter filter;
	public ConfigPanel_LBoxTomcat lbox;

	public ConfigWindow(final SimpleATN satn){
		super("Config Window");
		this.satn = satn;
		tabbedPane = new JTabbedPane();
		addTab(pimgfinder = new ConfigPanel_PImageFinder(satn, new File(System.getProperty("user.dir")), this));
		addTab(seatmap = new ConfigPanel_Seatmap(satn,this));
		addTab(database = new ConfigPanel_Database(satn,this));
		addTab(draw = new ConfigPanel_Draw(satn,this));
		addTab(filter = new ConfigPanel_Filter(satn,this));
		// addTab(lbox = new ConfigPanel_LBoxTomcat(satn,this));
		//		addTab(new ConfigPanel(this));
		getContentPane().add(tabbedPane);
		setSize(600,400);
		//		pack();
		tabbedPane.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				database.update();
				draw.update();
			}
		});

		try {
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}
		catch (final SecurityException e) {
			System.out.println("Ignoring security exception. Assuming Applet Context.");
		}
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowActivated(WindowEvent e) {}
			@Override
			public void windowClosed(WindowEvent e) {
				satn.menutoolbar.getAction("Config").setSelected(false);
			}
			@Override
			public void windowClosing(WindowEvent e) {}
			@Override
			public void windowDeactivated(WindowEvent e) {}
			@Override
			public void windowDeiconified(WindowEvent e) {}
			@Override
			public void windowIconified(WindowEvent e) {}
		});
	}

	public void addTab(ConfigPanel cp){
		tabbedPane.addTab(cp.tabTitle, cp);
		JLabel label = new JLabel(cp.tabTitle, cp.tabIcon, SwingConstants.CENTER);

		//MacosXなら、以下を実行しない
		// MacOSXで動作しているか?
		if (!SimpleATN.isMACOSX){
			label.setVerticalTextPosition(SwingConstants.BOTTOM);
			label.setHorizontalTextPosition(SwingConstants.CENTER);
		}
//		label.setVerticalAlignment(SwingConstants.CENTER);
//		label.setHorizontalAlignment(SwingConstants.CENTER);
		//	     Dimension dim = label.getPreferredSize();
		//	     label.setPreferredSize(new Dimension(60, dim.height));
		//	     label.setPreferredSize(new Dimension(100,200));
		tabbedPane.setTabComponentAt(tabbedPane.getTabCount()-1, label);

	}

	public static ConfigWindow configWin = null;
	public static void setVisible(SimpleATN satn2, boolean selected) {
		if (configWin == null) configWin = new ConfigWindow(satn2);
		configWin.setVisible(selected);
		if (selected) configWin.resetLocationNearSATN();
	}

	private void resetLocationNearSATN() {
		setLocation(satn.getX()+100,satn.getY()+100);
	}
}
