Mercurial Hosting > junotu
changeset 5:81608928a9db
Separated window functionality off from the Main class into a Window class
In preparation for multiple windows support.
author | Fox |
---|---|
date | Tue, 05 Apr 2022 00:45:42 +0200 |
parents | 1b754922d6af |
children | fbe307da953a |
files | src/junotu/Main.java src/junotu/TabSimpleSearch.java src/junotu/Window.java |
diffstat | 3 files changed, 104 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/Main.java Tue Apr 05 00:15:07 2022 +0200 +++ b/src/junotu/Main.java Tue Apr 05 00:45:42 2022 +0200 @@ -1,87 +1,38 @@ package junotu; -import java.awt.Dimension; -import java.awt.Font; - -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.Window; +import junotu.Window.Tab; public class Main { - public enum Tab { - SEARCH, - EDIT, - }; - public static final String PROGRAM_NAME = "Junotu"; + public static final int MAX_WINDOWS = 8; - private static final String[] TAB_NAMES = { - "Search", - "Edit", - }; - - private static JFrame window; - - private static JPanel tabs; - private static CardLayout tabsLayout; - private static JPanel search; - private static JPanel edit; - - private static int activeTab = 0; + public static Window[] windows = new Window[MAX_WINDOWS]; public static void main(String[] args) { - - window = new JFrame(); - panelsCreate(); - tabSwitch( Tab.SEARCH.ordinal() ); - //tabSwitch( Tab.EDIT.ordinal() ); - - window.setSize(384, 384); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - window.setVisible(true); + addWindow( Tab.SEARCH ); } - public static void tabSwitch( int tab ) - { - tabsLayout.show(tabs, TAB_NAMES[tab]); - activeTab = tab; - window.setTitle(preferredTitle()); - } - - private static void panelsCreate() + public static void addWindow( Tab tab ) { - - tabsLayout = new CardLayout(); - tabs = new JPanel( tabsLayout ); - - search = new TabSimpleSearch(); - edit = new TabEdit(); - - window.add(tabs); - tabs.add(search); - tabs.add(edit); - tabsLayout.addLayoutComponent( search, TAB_NAMES[Tab.SEARCH.ordinal()] ); - tabsLayout.addLayoutComponent( edit, TAB_NAMES[Tab.EDIT.ordinal()] ); - + for( int i = 0; i < windows.length; i++ ) { + if( windows[i] == null ) { + windows[i] = new Window( tab ); + return; + } + } + return; } - private static String preferredTitle() - { - return PROGRAM_NAME+" - "+TAB_NAMES[activeTab]; + public static void activeWindowTabSwitch( Tab tab ) { + for( int i = 0; i < windows.length; i++ ) { + if( windows[i] != null && windows[i].isActive() ) { + windows[i].tabSwitch( tab ); + return; + } + } + return; } - }
--- a/src/junotu/TabSimpleSearch.java Tue Apr 05 00:15:07 2022 +0200 +++ b/src/junotu/TabSimpleSearch.java Tue Apr 05 00:45:42 2022 +0200 @@ -15,6 +15,7 @@ import javax.swing.JScrollPane; import junotu.Main; +import junotu.Window.Tab; import junotu.CardWidget; public class TabSimpleSearch extends JPanel { @@ -61,7 +62,7 @@ private void buttonClickedCreate() { - Main.tabSwitch( Main.Tab.EDIT.ordinal() ); + Main.activeWindowTabSwitch( Tab.EDIT ); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/junotu/Window.java Tue Apr 05 00:45:42 2022 +0200 @@ -0,0 +1,81 @@ +package junotu; + +import java.awt.Dimension; +import java.awt.Font; + +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; + +public class Window extends JFrame { + + public enum Tab { + SEARCH, + EDIT, + }; + + private static final String[] TAB_NAMES = { + "Search", + "Edit", + }; + + private JPanel tabs; + private CardLayout tabsLayout; + private JPanel search; + private JPanel edit; + + private Tab activeTab; + + public Window( Tab tab ) + { + + panelsCreate(); + tabSwitch( tab ); + + this.setSize(384, 384); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + } + + public void tabSwitch( Tab tab ) + { + tabsLayout.show(tabs, TAB_NAMES[tab.ordinal()]); + activeTab = tab; + this.setTitle(preferredTitle()); + } + + private void panelsCreate() + { + + tabsLayout = new CardLayout(); + tabs = new JPanel( tabsLayout ); + + search = new TabSimpleSearch(); + edit = new TabEdit(); + + this.add(tabs); + tabs.add(search); + tabs.add(edit); + tabsLayout.addLayoutComponent( search, TAB_NAMES[Tab.SEARCH.ordinal()] ); + tabsLayout.addLayoutComponent( edit, TAB_NAMES[Tab.EDIT.ordinal()] ); + + } + + private String preferredTitle() + { + return Main.PROGRAM_NAME+" - "+TAB_NAMES[activeTab.ordinal()]; + } + +}