|
| 1 | +package MainPage; |
| 2 | + |
| 3 | +import create_flashcardset_use_case.*; |
| 4 | +import dataAccess.*; |
| 5 | +import entities.FlashcardSetFactory; |
| 6 | +import loginAndSignupUseCase.UserLoginResponseModel; |
| 7 | +import loginAndSignupUseCase.loginAndSignupUseCaseScreens.WelcomeScreen; |
| 8 | +import search_use_case.*; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class HomePage extends JFrame { |
| 17 | + |
| 18 | + UserLoginResponseModel user; |
| 19 | + DBGateway gateway; |
| 20 | + |
| 21 | + public HomePage(UserLoginResponseModel user) throws IOException { |
| 22 | + super(user.getSignedInUsername() + "'s home page"); |
| 23 | + |
| 24 | + this.user = user; |
| 25 | + // home page layout |
| 26 | + getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); |
| 27 | + |
| 28 | + // initialize DBGateway |
| 29 | + IFlashcardSetDataAccess flashcardSetDataAccess = new FlashcardSetDataAccess(DBGateway.getFlashcardSetPath()); |
| 30 | + IFlashcardDataAccess flashcardDataAccess = new FlashcardDataAccess(DBGateway.getFlashcardPath()); |
| 31 | + IUserDataAccess userDataAccess = new CommonUserDataAccess(DBGateway.getUserPath()); |
| 32 | + DBGateway gateway = new DBGateway(flashcardDataAccess, |
| 33 | + flashcardSetDataAccess, userDataAccess); |
| 34 | + |
| 35 | + // top bar |
| 36 | + JPanel topBar = new JPanel(); |
| 37 | + topBar.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); |
| 38 | + |
| 39 | + JButton searchButton = new JButton("Search"); |
| 40 | + JButton addFlashcardSetButton = new JButton("Add Flashcard Set"); |
| 41 | + JButton logOff = new JButton("Log Off"); |
| 42 | + |
| 43 | + searchButton.addActionListener(e -> { |
| 44 | + SearchOutputBoundary presenter = new SearchPresenter(); |
| 45 | + SearchInteractor interactor = new SearchInteractor(presenter, gateway); |
| 46 | + SearchController controller = new SearchController(interactor); |
| 47 | + new SearchScreen(controller, gateway, user); |
| 48 | + }); |
| 49 | + |
| 50 | + addFlashcardSetButton.addActionListener(e -> { |
| 51 | + FlashcardSetOutputBoundary presenter = new FlashcardSetPresenter(); |
| 52 | + FlashcardSetFactory setFactory = new FlashcardSetFactory(); |
| 53 | + FlashcardSetInteractor interactor = new FlashcardSetInteractor(gateway, presenter, |
| 54 | + setFactory); |
| 55 | + FlashcardSetController controller = new FlashcardSetController(interactor); |
| 56 | + new CreationScreen(controller, user); |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | + logOff.addActionListener(e -> { |
| 61 | + this.setVisible(false); |
| 62 | + this.dispose(); |
| 63 | + try { |
| 64 | + new WelcomeScreen(); |
| 65 | + } catch (IOException ex) { |
| 66 | + throw new RuntimeException(ex); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + topBar.add(searchButton); |
| 71 | + topBar.add(addFlashcardSetButton); |
| 72 | + topBar.add(logOff); |
| 73 | + topBar.setSize(1000,20); |
| 74 | + this.add(topBar); |
| 75 | + |
| 76 | + Map<Integer, String[]> idsToFlashcardSetData = user.getFlashcardSets(); |
| 77 | + |
| 78 | + int numSets = idsToFlashcardSetData.size(); |
| 79 | + System.out.println(numSets); |
| 80 | + if (numSets==0){ |
| 81 | + JLabel label = new JLabel("You have no Flashcard Sets!"); |
| 82 | + label.setHorizontalAlignment(SwingConstants.CENTER); |
| 83 | + label.setVerticalAlignment(SwingConstants.TOP); |
| 84 | + JPanel labelPanel = new JPanel(); |
| 85 | + labelPanel.add(label); |
| 86 | + this.add(labelPanel); |
| 87 | + } |
| 88 | + else { |
| 89 | + this.add(new ListOfFlashcardSetsDataPanel(idsToFlashcardSetData, gateway, user)); |
| 90 | + } |
| 91 | + this.setSize(1000, 800); |
| 92 | + this.setVisible(true); |
| 93 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private void refresh() { |
| 98 | + try { |
| 99 | +// UserLoginOutputBoundary presenter = new UserLoginPresenter(); |
| 100 | +// UserLoginInputBoundary interactor = new UserLoginInteractor( |
| 101 | +// gateway, presenter); |
| 102 | +// UserLoginController userLoginController = new UserLoginController(interactor); |
| 103 | +// setVisible(false); |
| 104 | +// dispose(); |
| 105 | +// new LoginScreen(userLoginController).setVisible(true); |
| 106 | +// |
| 107 | +// UserLoginResponseModel user = userLoginController.create(user.getSignedInUsername(), |
| 108 | +// user.getPassword()); |
| 109 | +// this.dispose(); |
| 110 | + new HomePage(user); |
| 111 | + } catch (Exception e) { |
| 112 | + JOptionPane.showMessageDialog(this, e.getMessage()); |
| 113 | + } |
| 114 | + } |
| 115 | + public static void main(String[] args) throws IOException { |
| 116 | + Map<Integer, String[]> map = new HashMap<>(); |
| 117 | + for (int i = 1; i < 4; i++) { |
| 118 | + map.put(i, new String[]{"test set " + i, "test description " + i}); |
| 119 | + } |
| 120 | + UserLoginResponseModel user = new UserLoginResponseModel("Lucas", false, map); |
| 121 | + new HomePage(user); |
| 122 | + } |
| 123 | +} |
0 commit comments