1
+ package appscreen ;
2
+
1
3
import javax .swing .*;
2
4
import java .awt .*;
3
5
import java .awt .event .ActionEvent ;
8
10
9
11
public class AppScreen implements AppScreenPresenter , AppScreenController , ChatName , Refresh {
10
12
11
- final JFrame jFrame ;
12
- final String currentUserName ;
13
+ private final JFrame jFrame ;
14
+ private JScrollPane jScrollPane ;
15
+ private final String currentUsername ;
13
16
private ArrayList <Chat > chats ;
14
17
15
18
@@ -18,12 +21,12 @@ public class AppScreen implements AppScreenPresenter, AppScreenController, ChatN
18
21
@param chats This is a list of chats given by the user (the list will always come as sorted with the
19
22
most recent chats at the end of the list)
20
23
*/
21
- public AppScreen (String currentUserName , ArrayList <Chat > chats ) {
22
- this .currentUserName = currentUserName ;
24
+ public AppScreen (String currentUsername , ArrayList <Chat > chats ) {
25
+ this .currentUsername = currentUsername ;
23
26
this .chats = chats ;
24
- this . jFrame = new JFrame ();
25
- this . jFrame .setSize (300 , 500 );
26
- this . jFrame .setDefaultCloseOperation (WindowConstants .EXIT_ON_CLOSE );
27
+ jFrame = new JFrame ();
28
+ jFrame .setSize (300 , 500 );
29
+ jFrame .setDefaultCloseOperation (WindowConstants .EXIT_ON_CLOSE );
27
30
28
31
29
32
// top panel containing the buttons for creating a new chat
@@ -35,30 +38,26 @@ public AppScreen(String currentUserName, ArrayList<Chat> chats) {
35
38
addPrivateChat .setPreferredSize (new Dimension (40 , 30 ));
36
39
addGroupChat .setPreferredSize (new Dimension (40 , 30 ));
37
40
38
- // TODO: implement the action listeners for +PrivateChat and +GroupChat
41
+ // TODO: implement the action listeners for the buttons +PrivateChat and +GroupChat
39
42
40
43
topPanel .add (addPrivateChat );
41
44
topPanel .add (addGroupChat );
42
- this . jFrame .add (topPanel , BorderLayout .NORTH );
45
+ jFrame .add (topPanel , BorderLayout .NORTH );
43
46
44
47
this .chats = chats ;
45
48
openScreen ();
46
49
47
50
}
48
51
49
52
/**
50
- * Update the order of the chats
51
- * @param chat The chat that has an update
53
+ * Attempts to open the screen to display to the user
52
54
*/
53
-
54
- public void updateChatOrder (Chat chat ){
55
-
56
- if (this .chats .contains (chat )) {
57
- this .chats .remove (chat );
58
- this .chats .add (chat );
59
- }
60
- else {
61
- this .chats .add (chat );
55
+ @ Override
56
+ public void openScreen () {
57
+ try {
58
+ displayAppScreen ();
59
+ } catch (Exception e ) {
60
+ throw new RuntimeException ("Unable to to open screen" );
62
61
}
63
62
64
63
}
@@ -102,12 +101,11 @@ public void actionPerformed(ActionEvent e) {
102
101
jPanel .setMaximumSize (new Dimension (100 , 500 ));
103
102
jPanel .setBorder (BorderFactory .createTitledBorder ("My Chats" ));
104
103
105
- //jFrame.getContentPane().add(jPanel);
106
104
107
105
// making the chat list scrollable
108
106
scrollableChats (jPanel );
109
107
110
- this . jFrame .setVisible (true );
108
+ jFrame .setVisible (true );
111
109
112
110
}
113
111
@@ -117,9 +115,9 @@ public void actionPerformed(ActionEvent e) {
117
115
*/
118
116
private void scrollableChats (JPanel jPanel ) {
119
117
JScrollPane scrollFrame = new JScrollPane (jPanel );
120
- // scrollFrame.setAutoscroll(true) ;
118
+ jScrollPane = scrollFrame ;
121
119
scrollFrame .setPreferredSize (new Dimension ( 200 ,500 ));
122
- this . jFrame .add (scrollFrame );
120
+ jFrame .add (scrollFrame );
123
121
}
124
122
125
123
@@ -134,36 +132,51 @@ public String getChatName(Chat chat) {
134
132
}
135
133
136
134
/**
137
- * Return true if the given chat as an update to its conversation history
138
- * @param chat The given chat
139
- * @return true/false
135
+ * Update the order of the chats
136
+ * @param chat The chat that has an update
140
137
*/
141
- @ Override
142
- public boolean hasUpdate (Chat chat ) {
143
- return this .chats .get (this .chats .size () - 1 ) != chat ;
138
+ public void updateChatOrder (Chat chat ){
139
+
140
+ if (this .chats .contains (chat )) {
141
+ this .chats .remove (chat );
142
+ this .chats .add (chat );
143
+ }
144
+ else {
145
+ this .chats .add (chat );
146
+ }
147
+
144
148
}
145
149
150
+
146
151
/**
147
152
* Update the screen if the given chat has been updated
148
- * @param chat The given chat
153
+ * @param chatID The ID of the given chat
149
154
*/
150
155
@ Override
151
- public void updateScreen (Chat chat ) {
152
- if (hasUpdate (chat )){
153
- updateChatOrder (chat );
156
+ public void updateScreen (String chatID ) {
157
+ if (hasUpdate (chatID )){
158
+
159
+ updateChatOrder (getChat (chatID ));
160
+ jFrame .remove (this .jScrollPane );
154
161
155
162
// refresh the screen
156
163
displayAppScreen ();
157
164
}
158
165
}
159
166
167
+ /**
168
+ * Return true if the given chat as an update to its conversation history
169
+ * @param chatID The ID of the given chat
170
+ * @return true/false
171
+ */
160
172
@ Override
161
- public void openScreen () {
162
- displayAppScreen ();
173
+ public boolean hasUpdate (String chatID ) {
174
+ Chat chat = getChat (chatID );
175
+ return this .chats .get (this .chats .size () - 1 ) != chat ;
163
176
}
164
177
165
178
@ Override
166
- public Chat getUpdatedChat (String chatID ) {
179
+ public Chat getChat (String chatID ) {
167
180
for (Chat chat : this .chats ){
168
181
if (chat .getChatID ().equals (chatID )){
169
182
return chat ;
@@ -172,4 +185,11 @@ public Chat getUpdatedChat(String chatID) {
172
185
throw new RuntimeException ("Current user is not part of this chat" );
173
186
}
174
187
188
+ /**
189
+ * Get the username of the current user
190
+ * @return currentUserName
191
+ */
192
+ public String getCurrentUsername () {
193
+ return currentUsername ;
194
+ }
175
195
}
0 commit comments