-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserView.java
More file actions
227 lines (193 loc) · 10.4 KB
/
UserView.java
File metadata and controls
227 lines (193 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package miniTwitter;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.util.*;
public class UserView extends javax.swing.JFrame {
private String userInput;
private final DefaultListModel<String> followingListModel;
private final DefaultListModel<String> newsFeedListModel;
User user = new User(userInput);
private final String username;
private User lastUpdatedUser;
/**
* Creates new form UserView
* @param user
*/
public UserView(User user) {
initComponents();
this.username = user.getUsername();
UserIdLabel.setText(username + " view");
creationTime.setText("time: " + timeFormat(user.getCreationTime()));
// following list
followingListModel = new DefaultListModel<>();
followingListModel.addElement("- Bob");
followingListModel.addElement("- Steve");
FollowingList.setModel(followingListModel);
// twitter news feed
newsFeedListModel = new DefaultListModel<>();
newsFeedListModel.addElement("- Steve: Bob is awesome");
newsFeedListModel.addElement("- Bob: I like to code");
NewsFeed.setModel(newsFeedListModel);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
FollowButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
FollowingList = new javax.swing.JList<>();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
TweetMessage = new javax.swing.JTextArea();
Post = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
NewsFeed = new javax.swing.JList<>();
UserIdLabel = new javax.swing.JLabel();
Username = new javax.swing.JTextField();
creationTime = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
FollowButton.setText("Follow");
FollowButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FollowButtonActionPerformed(evt);
}
});
FollowingList.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "- Bob", "- Steve" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(FollowingList);
jLabel2.setText("Current Following");
TweetMessage.setColumns(20);
TweetMessage.setRows(5);
TweetMessage.setText("Tweet Message");
jScrollPane2.setViewportView(TweetMessage);
Post.setText("Post Tweet");
Post.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PostActionPerformed(evt);
}
});
jLabel3.setText("News Feed");
NewsFeed.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "- Steve: Bob is awesome", "- Bob: I like to code" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jScrollPane3.setViewportView(NewsFeed);
UserIdLabel.setText("User Id");
Username.setText("Username");
creationTime.setText("Time");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Post, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
.addComponent(jScrollPane3)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(UserIdLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(creationTime, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(FollowButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(FollowButton, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(Username))
.addGroup(layout.createSequentialGroup()
.addComponent(UserIdLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(creationTime)
.addGap(0, 0, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(1, 1, 1)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Post, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2))
.addGap(8, 8, 8)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17))
);
pack();
}// </editor-fold>
private void FollowButtonActionPerformed(java.awt.event.ActionEvent evt) {
userInput = Username.getText();
System.out.println(userInput);
followingListModel.addElement("- " + userInput);
}
private void PostActionPerformed(java.awt.event.ActionEvent evt) {
user.setLastUpdateTime();
long time = user.getLastUpdateTime();
user.setLastUser(username);
System.out.println("last updater: " + user.getLastUser());
// print last time updated to console
System.out.println("Last time updated: " + getTimeString(time));
String tweet = TweetMessage.getText();
newsFeedListModel.addElement("- " + username + ": " + tweet + " (Posted on: " + getTimeString(time) + ")");
TweetMessage.setText("");
boolean isPos = tweet.contains("like") || tweet.contains("awesome") || tweet.contains("amazing");
if(isPos == true){
counters.addPositive();
}
counters.addTweet();
lastUpdatedUser = user;
}
private String timeFormat(long timestamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
Date date = new Date(timestamp);
return dateFormat.format(date);
}
private String getTimeString(long timestamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(timestamp);
return dateFormat.format(date);
}
public User getLastUpdatedUser() {
return lastUpdatedUser;
}
// Variables declaration - do not modify
private javax.swing.JButton FollowButton;
private javax.swing.JList<String> FollowingList;
private javax.swing.JList<String> NewsFeed;
private javax.swing.JButton Post;
private javax.swing.JTextArea TweetMessage;
private javax.swing.JLabel UserIdLabel;
private javax.swing.JTextField Username;
private javax.swing.JLabel creationTime;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
// End of variables declaration
}