Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainFrame {
- //egentligen private static void
- public static void createAndShowGUI() {
- final Session session = new Session();
- JFrame frame = new JFrame("Dimensions helper");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- final JPanel contentPane = new JPanel();
- contentPane.setLayout(new CardLayout(5, 5));
- /* Skapar instancer av samtliga windows-klasser och lägger till dem
- * i contentpanelen. Contentpanelen styrs av en CardLayout, vilken
- * fungerar på så vis att endast en panel visas åt gången. När man
- * klickar på knapparna next respektive previous anropas CardLayouts
- * funktioner cardlayout.next() resp cardlayout.previous() som ändrar
- * vad som ska visas på skärmen.
- */
- /*
- * Placeholder
- */
- String un = "u008228";
- String proj = "Mars";
- String prod = "Mars";
- Win1 win1 = new Win1(un, proj, prod);
- contentPane.add(win1);
- Win2 win2 = new Win2();
- contentPane.add(win2);
- JPanel buttonPanel = new JPanel();
- final JButton previousButton = new JButton("< PREVIOUS");
- previousButton.setEnabled(false);
- final JButton nextButton = new JButton("NEXT >");
- final JButton cancelButton = new JButton("CANCEL");
- buttonPanel.add(cancelButton);
- buttonPanel.add(previousButton);
- buttonPanel.add(nextButton);
- /* Skapar eventlistener och eventhandler för previous-knappen. Som default
- * är knappen disablad för att förhindra att användaren ska kunna gå till
- * sista panelen via första. Vid varje anrop kollar den mot Interface
- * Verifiable:s getIdentifier()-funktion för att se om användaren är på login-
- * rutan. Om så disablas knappen återigen. getIdentifier() är överlagrad för
- * samtliga window-klasser och returnerar en unik string för varje klass.
- */
- // previousButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent ae) {
- // CardLayout cardLayout = (CardLayout) contentPane.getLayout();
- // cardLayout.previous(contentPane);
- // Component[] contents = contentPane.getComponents();
- // nextButton.setText("NEXT");
- // for(Component component : contents) {
- // if(component instanceof Interface1 && component.isVisible()) {
- // Verifiable window = (Verifiable)component;
- // if(window.getIdentifier().equals("FIRST")) {
- // previousButton.setEnabled(false);
- // }
- // break;
- // }
- // }
- // }
- // });
- /* Skapar eventlistener och eventhandler för next-knappen. Varje gång den
- * anropas kollar den om användaren har fyllt i rätt information. Detta görs
- * via ett interface Verifiable som innehåller isDataValid()-funktionen. Denna
- * är överlagrad för samtliga window-klasser och returnerar true om
- * samtliga åtgärder i klassen har gått igenom.
- */
- nextButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
- Component[] contents = contentPane.getComponents();
- for(Component component : contents) {
- if(component.isVisible() && component instanceof Interface1) {
- Interface1 window = (Interface1) component;
- String username = window.getUsername();
- String password = window.getPassword();
- boolean login = session.isLoginValid(username, password);
- if(login) {
- nextButton.setCursor(Cursor.getDefaultCursor());
- CardLayout cardLayout = (CardLayout) contentPane.getLayout();
- cardLayout.next(contentPane);
- previousButton.setEnabled(true);
- } else {
- JOptionPane.showMessageDialog(component, "Wrong username and/or password",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- break;
- } else if(component.isVisible() && component instanceof Interface2) {
- Interface2 window = (Interface2) component;
- String items = window.getItemsname();
- session.test(items);
- }
- nextButton.setCursor(Cursor.getDefaultCursor());
- //break;
- }
- }
- });
- // nextButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent ae) {
- // //TODO: Anropa sessions states
- //
- // }
- // });
- cancelButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- session.terminateConnection();
- System.exit(0);
- }
- });
- frame.add(contentPane);
- frame.add(buttonPanel, BorderLayout.PAGE_END);
- frame.setSize(400, 400);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment