import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.Box.Filler; import java.net.URL; /* XInterface.java =============== This defines the top level GUI components and is based around 'modes'. 'Modes' operate using bitwise operators relating to the various screens the user may view (defined in Modes interface). @author Jack Wootton @version Last modified 11/10/05 */ class XInterface extends JFrame implements Modes, Settings { /* Vars. */ private int modes = 0; // Which panels are viewable and which are not. private int oldPanel = 0; // The most recent panel to be displayed, excluding footer. /* The panels that make up our interface and all extend JPanel. New types for each panel were created as it creates more maintainable / cleaner code. Each Panel also handles its own events, this keeps eventHandlers nice and quick. */ private Splash splash; private Login login; private Register register; private CustomerList customerList; private AccountList accountList; private CustomerAccount customerAccount; private Footer footer; /* We need to do this for the one nested class we have (WindowCatcher). */ private JFrame self = this; /* Main panel doesn't have it's own type, since it only provides padding and has no GUI components, so we keep it in this class. */ private JPanel main; /* Create a global reference to the Frames content pane. */ private Container contentPane; public XInterface() { super(WINDOW_TITLE); contentPane = getContentPane(); } public void init() { /* Load and read employee and customer databases. New thread? */ EmployeeManager.open(); CustomerManager.open(); splash = new Splash(this); login = new Login(this); register = new Register(this); customerList = new CustomerList(this); accountList = new AccountList(this); customerAccount = new CustomerAccount(this); footer = new Footer(this); buildGUI(); } public void buildGUI() { int x, y; Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); addWindowListener( new WindowCatcher() ); setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); /* Ensure we have system window decorations. */ try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); } catch (Exception e) { } /* Initialize and build the main panel that houses the others. */ createMain(); /* Show splash screen by default (and always show the footer). */ modes |= SPLASH; modes |= REGISTER; /* Main panel must always been shown. */ contentPane.add(main); /* Make sure correct panels are attached to the JFrame, based on 'modes'. */ updateFrame(); /* Use pack instead of explicitly setting Frame dimensions, because layout managers handle platform differences better. */ pack(); /* Calculate coordinates to center window on screeen. We need to cast from type double to int when using Dimension methods. */ x = half( (int) dim.getWidth() ) - half( getWidth() ); y = half( (int) dim.getHeight() ) - half( getHeight() ); /* Location and visibility should be set last. */ setLocation( x, y ); setVisible( true ); } private void updateFrame() { /* Turn off last viewed panel. */ if (oldPanel > 0) modes &= ~oldPanel; /* Remove old panel from main panel. */ removePanel(oldPanel); /* Work out which panel the user should see next. */ if ((modes & SPLASH) == SPLASH) { main.add(splash, BorderLayout.CENTER); splash.setVisible(true); oldPanel = SPLASH; } else if ((modes & LOGIN) == LOGIN) { main.add(login, BorderLayout.CENTER); login.setVisible(true); oldPanel = LOGIN; } else if ((modes & REGISTER) == REGISTER) { main.add(register, BorderLayout.CENTER); register.setVisible(true); oldPanel = REGISTER; } else if ((modes & CUSTOMER_LIST) == CUSTOMER_LIST) { customerList.populateGui(); main.add(customerList, BorderLayout.CENTER); customerList.setVisible(true); oldPanel = CUSTOMER_LIST; } else if ((modes & ACCOUNT_LIST) == ACCOUNT_LIST) { accountList.populateGui(); main.add(accountList, BorderLayout.CENTER); accountList.setVisible(true); oldPanel = ACCOUNT_LIST; } else if ((modes & CUSTOMER_ACCOUNT) == CUSTOMER_ACCOUNT) { customerAccount.populateGui(); main.add(customerAccount, BorderLayout.CENTER); customerAccount.setVisible(true); oldPanel = CUSTOMER_ACCOUNT; } /* Footer cannot be part of the 'else if' since it can be viewed at the same time as the others. */ if ((modes & FOOTER) == FOOTER) { main.add(footer, BorderLayout.PAGE_END); footer.setVisible(true); } } private void removePanel(int panelBit) { switch (panelBit) { case SPLASH: splash.setVisible(false); break; case LOGIN: login.setVisible(false); break; case REGISTER: register.setVisible(false); break; case CUSTOMER_LIST: customerList.setVisible(false); break; case ACCOUNT_LIST: accountList.setVisible(false); case CUSTOMER_ACCOUNT: customerAccount.setVisible(false); break; case FOOTER: footer.setVisible(false); break; } } private void createMain() { main = new JPanel( new BorderLayout() ); /* Create the padding around the inner Panels. */ main.add(new Box.Filler(OUTER_PADDING, OUTER_PADDING, OUTER_PADDING), BorderLayout.EAST); main.add(new Box.Filler(OUTER_PADDING, OUTER_PADDING, OUTER_PADDING), BorderLayout.WEST); main.add(new Box.Filler(OUTER_PADDING, OUTER_PADDING, OUTER_PADDING), BorderLayout.PAGE_START); main.add(new Box.Filler(OUTER_PADDING, OUTER_PADDING, OUTER_PADDING), BorderLayout.PAGE_END); } /* show() is used by other classes to 'switch on' panels from listener events. */ public void show(int panel) { modes |= panel; updateFrame(); } public void setLoggedIn(boolean b) { footer.setLoggedIn(b); } private int half(int x) { return x / TWO; } class WindowCatcher extends WindowAdapter { private XInterface front; public void windowClosing( WindowEvent evt ) { int result = JOptionPane.showConfirmDialog(self, "Do you want to save your changes?", "Bank Application", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE ); switch (result) { case JOptionPane.YES_OPTION : EmployeeManager.save(); closeWindow(evt); break; case JOptionPane.NO_OPTION : closeWindow(evt); break; } } private void closeWindow(WindowEvent evt) { evt.getWindow().dispose(); System.exit(0); } }; };