view src/junotu/Window.java @ 122:ad6ad9f695bb default tip

TabEdit: Started on card options pop-up menu
author Fox
date Sat, 25 Nov 2023 16:40:05 +0100
parents 6a78c671a7cf
children
line wrap: on
line source

package junotu;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;

import javax.swing.KeyStroke;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import junotu.TabEdit;
import junotu.TabSimpleSearch;
import junotu.TabColumnBoard;
import junotu.TabCalendarBoard;
import junotu.TabOptions;

public class Window extends JFrame implements ActionListener {
	
	public static interface TabInterface {
		void onSwitchedTo();
	}
	
	public enum Tab {
		SEARCH,
		EDIT,
		COLUMN_BOARD,
		CALENDAR_BOARD,
		OPTIONS,
	};
	
	private static final String[] TAB_NAMES = {
		"Search",
		"Edit",
		"Column Board",
		"Calendar board",
		"Options",
	};
	
	public final String KEY_ACTION_NEW_WINDOW = "new_window";
	
	public TabSimpleSearch  tabSearch;
	public TabEdit          tabEdit;
	public TabColumnBoard   tabColumnBoard;
	public TabCalendarBoard tabCalendarBoard;
	public TabOptions       tabOptions;
	
	private JPanel tabs;
	private CardLayout tabsLayout;
	
	private Tab activeTab;
	
	public Window( Tab tab )
	{
		
		panelsCreate();
		tabSwitch( tab );
		
		this.setSize(512, 384);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		if( tab == Tab.OPTIONS ) {
			/* TODO: Make the window a popup or similar. ('Transient' on X).
			         Practically, I want it to be a floating window
			         in my window manager. */
		}
		
		this.setVisible(true);
		
	}
	
	@Override
	protected void processWindowEvent( WindowEvent e )
	{
		switch( e.getID() ) {
			case WindowEvent.WINDOW_CLOSING: {
				Main.windowClose(this);
			}
		}
	}
	
	@Override
	public void actionPerformed( ActionEvent e )
	{
		if( e.getActionCommand() == KEY_ACTION_NEW_WINDOW ) {
			Main.windowAdd( Tab.SEARCH );
		}
	}
	
	public void tabSwitch( Tab tab )
	{
		if( tab == activeTab ) {
			return;
		}
		tabsLayout.show(tabs, TAB_NAMES[tab.ordinal()]);
		Object tabObject = tabs.getComponent(tab.ordinal());
		if( tabObject instanceof TabInterface ) {
			TabInterface tabInterface = (TabInterface)tabObject;
			tabInterface.onSwitchedTo();
		}
		activeTab = tab;
		this.setTitle(preferredTitle(TAB_NAMES[activeTab.ordinal()]));
	}
	
	public Tab tabCurrent()
	{
		return activeTab;
	}
	
	public String preferredTitle( String tabStatus )
	{
		return Main.PROGRAM_NAME+" - "+tabStatus;
	}
	
	private void panelsCreate()
	{
		
		tabsLayout = new CardLayout();
		tabs = new JPanel( tabsLayout );
		
		tabSearch = new TabSimpleSearch();
		tabEdit = new TabEdit();
		tabColumnBoard = new TabColumnBoard();
		tabCalendarBoard = new TabCalendarBoard();
		tabOptions = new TabOptions();
		
		this.add(tabs);
		tabs.add(tabSearch);
		tabs.add(tabEdit);
		tabs.add(tabColumnBoard);
		tabs.add(tabCalendarBoard);
		tabs.add(tabOptions);
		tabsLayout.addLayoutComponent( tabSearch,        TAB_NAMES[Tab.SEARCH.ordinal()] );
		tabsLayout.addLayoutComponent( tabEdit,          TAB_NAMES[Tab.EDIT.ordinal()] );
		tabsLayout.addLayoutComponent( tabColumnBoard,   TAB_NAMES[Tab.COLUMN_BOARD.ordinal()] );
		tabsLayout.addLayoutComponent( tabCalendarBoard, TAB_NAMES[Tab.CALENDAR_BOARD.ordinal()] );
		tabsLayout.addLayoutComponent( tabOptions,       TAB_NAMES[Tab.OPTIONS.ordinal()] );
		
		tabs.registerKeyboardAction(
			this,
			KEY_ACTION_NEW_WINDOW,
			KeyStroke.getKeyStroke( KeyEvent.VK_F2, 0 ),
			JPanel.WHEN_IN_FOCUSED_WINDOW
		);
		
	}
	
}