package net.sourceforge.pain.tools.guitool;
import net.sourceforge.pain.tools.guitool.menu.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class ApplicationFrame extends JFrame {
    private static final int PREFERRED_WIDTH = 720;
    private static final int PREFERRED_HEIGHT = 600;
	private JPanel appPanel = null;
	private JWindow splashScreen = null;
	private JLabel splashLabel = null;
    public MainMenu mainMenu = null;
	public ToolBar toolbar = null;
	public StatusBar statusBar = null;
    public WorkspacePanel workspacePanel = null;
	public ApplicationFrame(GraphicsConfiguration grc) {
		super(grc);
    }
	void go(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createSplashScreen();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				splashScreen.show();
			}
		});
		initialize();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				showGuiTool();
				hideSplash();
			}
		});
	}
	private void initialize() {
		appPanel = new JPanel();
		appPanel.setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
		appPanel.setLayout(new BorderLayout());
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		appPanel.add(top, BorderLayout.NORTH);
		mainMenu = new MainMenu();
		top.add(mainMenu.menuBar ,BorderLayout.NORTH );
		JPanel centerPanel = new JPanel(new BorderLayout());
	    appPanel.add(centerPanel, BorderLayout.CENTER);
		statusBar = new StatusBar();
		appPanel.add(statusBar, BorderLayout.SOUTH);
		toolbar = new ToolBar();
		centerPanel.add(toolbar.getJToolBar(), BorderLayout.NORTH);
		workspacePanel = new WorkspacePanel();
        centerPanel.add(workspacePanel, BorderLayout.CENTER);
		getContentPane().add(appPanel, BorderLayout.CENTER);
	}
	public void createSplashScreen() {
		splashLabel = new JLabel(GuiTool.loadImage("Splash.jpg", "desc"));
		splashScreen = new JWindow(this);
		splashScreen.getContentPane().add(splashLabel);
		splashScreen.pack();
		Rectangle screenRect = getGraphicsConfiguration().getBounds();
		splashScreen.setLocation(
		        screenRect.x + screenRect.width / 2 - splashScreen.getSize().width / 2,
		        screenRect.y + screenRect.height / 2 - splashScreen.getSize().height / 2);
	}
	public void hideSplash() {
		splashScreen.setVisible(false);
		splashScreen = null;
		splashLabel = null;
	}
	public void showGuiTool() {
		// put management in a frame and show it
		setTitle("PAiN Management Tool");
		pack();
		Rectangle screenRect = getGraphicsConfiguration().getBounds();
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
		        getGraphicsConfiguration());
		// Make sure we don't place the demo off the screen.
		int centerWidth = screenRect.width < getSize().width ?
		        screenRect.x :
		        screenRect.x + screenRect.width / 2 - getSize().width / 2;
		int centerHeight = screenRect.height < getSize().height ?
		        screenRect.y :
		        screenRect.y + screenRect.height / 2 - getSize().height / 2;
		centerHeight = centerHeight < screenInsets.top ?
		        screenInsets.top : centerHeight;
		setLocation(centerWidth, centerHeight);
//        workspacePanel.vSplitPane.setDividerLocation(5/6D);
        workspacePanel.hSplitPane.setDividerLocation(workspacePanel.hSplitPane.getResizeWeight());
        workspacePanel.vSplitPane.setDividerLocation(workspacePanel.vSplitPane.getResizeWeight());
		show();
	}
	public WorkspacePanel getWorkspacePanel(){
		return workspacePanel;
	}
	public StatusBar getStatusBar() {
		return statusBar;
	}
	//-------------INNERS ----------------//
	class TabListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			SingleSelectionModel model = (SingleSelectionModel) e.getSource();
			boolean logSelected = model.getSelectedIndex() == 1;
			if (logSelected) {
				statusBar.setContextMessage("log window");
			} else {
				statusBar.setContextMessage("main window");
			}
		}
	}
}