package ui;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import ui.GuiManager.TabActivator;
import ui.label.DynamicTabLabel;
import ui.label.TabLabel;
import ui.misc.MessageBox;
import ui.tabs.AbstractDynamicTab;
import ui.tabs.AbstractTab;
import ui.tabs.TabFlasher;
import main.settings.Settings;
public class MainFrame extends JFrame
{
private static final long serialVersionUID = -8808883923263763897L;
private CloseListener closeListener;
private ArrayList<AbstractTab> tabs;
private JTabbedPane tabPane;
public MainFrame()
{
setLayout(new BorderLayout());
setVisible(false);
setTitle(Settings.title);
setIconImage(Toolkit.getDefaultToolkit().createImage("gfx/icons/64x64.png"));
setSize(800, 600);
setLocationByPlatform(true);
// setExtendedState(Frame.MAXIMIZED_BOTH);
closeListener = new CloseListener();
if (Settings.application.ask_before_exit)
{
setAskBeforeExit();
}
else
{
unsetAskBeforeExit();
}
tabs = new ArrayList<AbstractTab>();
tabPane = new JTabbedPane();
tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
add(tabPane, BorderLayout.CENTER);
}
public void setAskBeforeExit()
{
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(closeListener);
}
public void unsetAskBeforeExit()
{
removeWindowListener(closeListener);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
private class CloseListener extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
askAndExit();
}
}
public void askAndExit()
{
if (askExitConfirm())
{
dispose();
}
}
private boolean askExitConfirm()
{
return MessageBox.yesNoConfirm("Do you want to exit Sillian?", "Confirm exit");
}
public void close()
{
if (Settings.application.ask_before_exit)
{
askAndExit();
}
else
{
dispose();
}
}
public void addTab(AbstractTab tab, String title)
{
tabs.add(tab);
tabPane.addTab(null, null, tab);
tabPane.setTabComponentAt(tabPane.indexOfComponent(tab), new TabLabel(title));
}
public int getNumberOfTabs()
{
return tabs.size();
}
public void initializeTabActivation(TabActivator activator)
{
tabPane.addChangeListener(activator);
tabPane.setSelectedIndex(activator.getLastActiveIndex());
}
public void addDynamicTab(AbstractDynamicTab tab, String title)
{
tabs.add(tab);
tabPane.addTab(null, null, tab);
tabPane.setTabComponentAt(tabPane.indexOfComponent(tab), new DynamicTabLabel(tab,
title));
}
public void removeDynamicTab(AbstractDynamicTab tab)
{
tabPane.remove(tab);
tabs.remove(tab);
}
public void flashTab(AbstractTab tab)
{
if (tabPane.getSelectedComponent() != tab)
{
new TabFlasher(tabPane, tab);
}
}
public void activateTab(AbstractTab tab)
{
tabPane.setSelectedComponent(tab);
}
public int updateActiveTab(int lastActiveTabIndex)
{
tabs.get(lastActiveTabIndex).onDeactivate();
int newIndex = tabPane.getSelectedIndex();
tabs.get(tabPane.getSelectedIndex()).onActivate();
return newIndex;
}
}