-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserUI.java
More file actions
136 lines (115 loc) · 4.53 KB
/
Copy pathUserUI.java
File metadata and controls
136 lines (115 loc) · 4.53 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
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.util.Observable;
import java.util.Observer;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.stream.Collectors;
public class UserUI extends Stage implements Observer {
private final TextArea txtUID = new TextArea("User ID");
private final Button btnFollowUser = new Button("Follow User");
private final TextArea txtTweet = new TextArea("Tweet Message");
private final Button btnPostTweet = new Button("Post Tweet");
final ObservableList<String> following = FXCollections.observableArrayList();
final ObservableList<String> newFeed = FXCollections.observableArrayList();
private final User mainUser;
final Alert alert = new Alert(Alert.AlertType.ERROR);
public UserUI(User mainUser) {
super();
this.mainUser = mainUser;
this.mainUser.addObserver(this);
init();
updateFollowingListView();
updateNewFeed();
}
private void init() {
setTitle("User View: " + mainUser.getUid());
GridPane mainPane = new GridPane();
Scene scene = new Scene(mainPane, 400, 500);
txtUID.setMaxSize(250, 250);
mainPane.add(txtUID, 0, 0);
txtUID.setOnMouseClicked(this::textareaOnClick);
btnFollowUser.setMaxSize(250, 250);
mainPane.add(btnFollowUser, 1, 0);
btnFollowUser.setOnAction(actionEvent -> followUser());
ListView<String> lsvFollowing = new ListView<>(following);
mainPane.add(lsvFollowing, 0, 1, 2, 1);
txtTweet.setMaxSize(250, 250);
mainPane.add(txtTweet, 0, 2);
txtTweet.setOnMouseClicked(this::textareaOnClick);
btnPostTweet.setMaxSize(250, 250);
mainPane.add(btnPostTweet, 1, 2);
btnPostTweet.setOnAction(actionEvent -> tweet());
ListView<String> lsvNewFeed = new ListView<>(newFeed);
mainPane.add(lsvNewFeed, 0, 3, 2, 1);
mainPane.setHgap(20);
mainPane.setVgap(20);
mainPane.setPadding(new Insets(10, 10, 10, 10));
setScene(scene);
}
private void tweet() {
mainUser.tweet(txtTweet.getText());
txtTweet.setText("Tweet Message");
}
private void followUser() {
User userToFollow = RootGroup.getInstance().findUser(txtUID.getText().trim());
if (userToFollow == null) {
showAlert("User has ID " + txtUID.getText() + " is not exist", "ERROR 1");
return;
}
if (mainUser.equals(userToFollow)) {
showAlert("Cannot follow yourself", "ERROR 2");
return;
}
if (!mainUser.follow(userToFollow))
showAlert("You has already follow this user", "ERROR 3");
txtUID.setText("User ID");
userToFollow.addObserver(this);
}
private void showAlert(String content, String title) {
alert.setContentText(content);
alert.setTitle(title);
alert.show();
}
private void textareaOnClick(MouseEvent mouseEvent) {
TextArea source = (TextArea) mouseEvent.getSource();
source.selectAll();
}
@Override
public void update(Observable o, Object arg) {
updateFollowingListView();
updateNewFeed();
}
private void updateFollowingListView() {
this.following.clear();
for (User u : mainUser.getFollowings()) {
this.following.add(u.getUid());
}
}
private void updateNewFeed() {
this.newFeed.clear();
Queue<Tweet> tweets = new PriorityQueue<>();
tweets.addAll(mainUser.getTweets()
.stream()
.map(t -> new Tweet(t.getDate(), mainUser.getUid() + ": " + t.getContent()))
.collect(Collectors.toList()));
for (User u : mainUser.getFollowings()) {
tweets.addAll(u.getTweets()
.stream()
.map(t -> new Tweet(t.getDate(), u.getUid() + ": " + t.getContent()))
.collect(Collectors.toList()));
}
while (!tweets.isEmpty()) {
newFeed.add(tweets.poll().getContent());
}
}
}