Newer
Older
POS_for_GUI / src / frames / MainFrame.java
k-fujii on 13 Apr 2022 2 KB [update]refactoring
package frames;

import java.awt.BorderLayout;

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

import panels.MainPanel;
import panels.ShowHistoryPanel;
import resources.CustomersModel;

//-------------------------------------------------------------------------
// 
public class MainFrame extends JFrame implements IMainFrame {
	
	// model
	private CustomersModel model;

	// cache
	private MainPanel mainPanel;
	private ShowHistoryPanel historyPanel;

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// constructor
	// -------------------------------------------------------------------------
	//
	public MainFrame() {
		super("POS");

		this.setTitle("POS");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// initialize model
		model = new CustomersModel();
		
		// initialize Panels
		mainPanel = new MainPanel(this, model);
		historyPanel = new ShowHistoryPanel(this, model);

		// set visible
		historyPanel.setVisible(false);
		this.add(historyPanel, BorderLayout.CENTER);

		mainPanel.setVisible(true);
		this.add(mainPanel, BorderLayout.CENTER);
		
		// set current panel
		curPanel = mainPanel;
		
		// adjust window size
		this.pack();
		this.setVisible(true);
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// public
	// -------------------------------------------------------------------------
	//
	public void reloadPanel(String destinationPanelName) {
		// main
		if (destinationPanelName.equals(State.MAIN)) {
			this.remove(this.mainPanel);
			MainPanel mainPanel = new MainPanel(this, model);
			this.add(mainPanel);
		}
		// history
		else if (destinationPanelName.equals(State.DETAIL)) {
			this.remove(this.historyPanel);
			ShowHistoryPanel historyPanel = new ShowHistoryPanel(this, model);
			this.add(historyPanel);
		}
	}

	// -------------------------------------------------------------------------
	// 
	public void showMainPanel(JPanel nowPanel) {
		getContentPane().removeAll();
		
		mainPanel.updateTableByTransition();
		
		add(mainPanel);
		validate();
		repaint();
	}

	// -------------------------------------------------------------------------
	//
	public void showHistoryPanel(JPanel nowPanel, int index) {
		getContentPane().removeAll();
		
		historyPanel.setVisible(true);
		historyPanel.updateListModelByIndex(index);		
		
		add(historyPanel);
		validate();
		repaint();
	}
}