|
| 1 | +package app_screen; |
| 2 | + |
| 3 | + |
| 4 | +import app_screen_use_case.AppScreenController; |
| 5 | +import app_screen_use_case.AppScreenPresenter; |
| 6 | +import app_screen_use_case.ChatName; |
| 7 | +import app_screen_use_case.Refresh; |
| 8 | +import entities.Chat; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.util.ArrayList; |
| 13 | + |
| 14 | +public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh { |
| 15 | + |
| 16 | + private final JFrame jFrame; |
| 17 | + private JScrollPane jScrollPane; |
| 18 | + private final String currentUsername; |
| 19 | + private ArrayList<Chat> chats; |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + Create an AppScreen object |
| 24 | + @param chats This is a list of chats given by the user (the list will always come as sorted with the |
| 25 | + most recent chats at the end of the list) |
| 26 | + */ |
| 27 | + public AppScreen(String currentUsername, ArrayList<Chat> chats) { |
| 28 | + this.currentUsername = currentUsername; |
| 29 | + this.chats = chats; |
| 30 | + jFrame = new JFrame(); |
| 31 | + jFrame.setSize(300, 500); |
| 32 | + jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 33 | + |
| 34 | + |
| 35 | + // top panel containing the buttons for creating a new chat |
| 36 | + JPanel topPanel = new JPanel(); |
| 37 | + topPanel.setLayout(new GridLayout(1,2)); |
| 38 | + |
| 39 | + JButton addPrivateChat = new JButton("+ Private Chat"); |
| 40 | + JButton addGroupChat = new JButton("+ Group Chat"); |
| 41 | + |
| 42 | + addPrivateChat.setPreferredSize(new Dimension(40, 30)); |
| 43 | + addGroupChat.setPreferredSize(new Dimension(40, 30)); |
| 44 | + |
| 45 | + // adding the action listeners for the +private-chat and +group-chat buttons |
| 46 | + addPrivateChat.addActionListener(e -> { |
| 47 | + ChatView newChat = new ChatView(currentUsername, true); |
| 48 | + newChat.chatDisplay(); |
| 49 | + |
| 50 | + }); |
| 51 | + addGroupChat.addActionListener(e -> { |
| 52 | + ChatView newChat = new ChatView(currentUsername, true); |
| 53 | + newChat.chatDisplay(); |
| 54 | + }); |
| 55 | + |
| 56 | + |
| 57 | + topPanel.add(addPrivateChat); |
| 58 | + topPanel.add(addGroupChat); |
| 59 | + jFrame.add(topPanel, BorderLayout.NORTH); |
| 60 | + |
| 61 | + this.chats = chats; |
| 62 | + openScreen(); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Attempts to open the screen to display to the user |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public void openScreen() { |
| 71 | + try{ |
| 72 | + displayAppScreen(); |
| 73 | + } catch (Exception e) { |
| 74 | + throw new RuntimeException("Unable to to open screen"); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + Display a screen containing an ordered list of chats to the user based on latest conversation times |
| 81 | + */ |
| 82 | + public void displayAppScreen(){ |
| 83 | + |
| 84 | + JPanel jPanel = new JPanel(); |
| 85 | + |
| 86 | + // getting the names of each chat to display and creating buttons for each chat |
| 87 | + for (int i = this.chats.size() - 1; i > -1; i--) { |
| 88 | + |
| 89 | + String chatName = getChatName(this.chats.get(i)); |
| 90 | + JButton b = new JButton(chatName); |
| 91 | + b.setPreferredSize(new Dimension(280, 50)); |
| 92 | + JLabel jLabel = new JLabel("time"); |
| 93 | + jLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); |
| 94 | + jLabel.setFont(new Font(null, Font.BOLD, 11)); |
| 95 | + b.add(jLabel); |
| 96 | + |
| 97 | + // defines the action of opening a chat when a chat is clicked on |
| 98 | + b.addActionListener(e -> { |
| 99 | + |
| 100 | + ChatView newChat = new ChatView(currentUsername, false); |
| 101 | + newChat.chatDisplay(); |
| 102 | + }); |
| 103 | + jPanel.add(b); |
| 104 | + } |
| 105 | + |
| 106 | + jPanel.setAlignmentY(Component.CENTER_ALIGNMENT); |
| 107 | + |
| 108 | + // set height of panel to appropriate size based on the number of chats |
| 109 | + jPanel.setPreferredSize(new Dimension(100, 60 * this.chats.size())); |
| 110 | + jPanel.setBorder(BorderFactory.createTitledBorder("My Chats")); |
| 111 | + |
| 112 | + |
| 113 | + // making the chat list scrollable |
| 114 | + scrollableChats(jPanel); |
| 115 | + |
| 116 | + jFrame.setVisible(true); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + Make the chat list scrollable |
| 122 | + @param jPanel The panel containing the chats |
| 123 | + */ |
| 124 | + private void scrollableChats(JPanel jPanel) { |
| 125 | + JScrollPane scrollFrame = new JScrollPane(jPanel); |
| 126 | + jScrollPane = scrollFrame; |
| 127 | + scrollFrame.setPreferredSize(new Dimension( 200,500)); |
| 128 | + scrollFrame. getVerticalScrollBar().setUnitIncrement(5); |
| 129 | + jFrame.getContentPane().add(scrollFrame); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the name of the chat |
| 135 | + * @param chat The chat in context |
| 136 | + * @return name |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public String getChatName(Chat chat) { |
| 140 | + return chat.getName(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Update the order of the chats |
| 145 | + * @param chat The chat that has an update |
| 146 | + */ |
| 147 | + public void updateChatOrder(Chat chat){ |
| 148 | + |
| 149 | + if (this.chats.contains(chat)) { |
| 150 | + this.chats.remove(chat); |
| 151 | + this.chats.add(chat); |
| 152 | + } |
| 153 | + else { |
| 154 | + this.chats.add(chat); |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Add a new chat to the screen, if the chat already exists (i.e. there exists a chat with the |
| 161 | + * same ID, do nothing) |
| 162 | + * @param chat The new chat to be added |
| 163 | + */ |
| 164 | + public void addNewChat(Chat chat){ |
| 165 | + |
| 166 | + if (!(this.chats.contains(chat))){ |
| 167 | + updateChatOrder(chat); |
| 168 | + jFrame.remove(this.jScrollPane); |
| 169 | + |
| 170 | + // refresh the screen |
| 171 | + displayAppScreen(); |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Update the order of chats that appear on screen if there was a change to conversation history |
| 178 | + * @param chatID The ID of the given chat |
| 179 | + */ |
| 180 | + @Override |
| 181 | + public void updateScreen(String chatID) { |
| 182 | + if (hasUpdate(chatID)){ |
| 183 | + |
| 184 | + updateChatOrder(getChat(chatID)); |
| 185 | + jFrame.remove(this.jScrollPane); |
| 186 | + |
| 187 | + // refresh the screen |
| 188 | + displayAppScreen(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Return true if the given chat as an update to its conversation history |
| 194 | + * @param chatID The ID of the given chat |
| 195 | + * @return true/false |
| 196 | + */ |
| 197 | + @Override |
| 198 | + public boolean hasUpdate(String chatID) { |
| 199 | + Chat chat = getChat(chatID); |
| 200 | + return this.chats.get(this.chats.size() - 1) != chat; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Get the chat object given its chat ID |
| 205 | + * @param chatID The ID of the chat |
| 206 | + * @return The chat with the given ID |
| 207 | + */ |
| 208 | + @Override |
| 209 | + public Chat getChat(String chatID) { |
| 210 | + for (Chat chat: this.chats){ |
| 211 | + if (chat.getChatID().equals(chatID)){ |
| 212 | + return chat; |
| 213 | + } |
| 214 | + } |
| 215 | + throw new RuntimeException("Current user is not part of this chat"); |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments