Browse Source

improve username change detection

Arnaud Vergnet 3 years ago
parent
commit
1fe1681d8b

+ 16
- 0
src/main/java/fr/insa/clavardator/ui/chat/ChatHeaderController.java View File

@@ -2,10 +2,12 @@ package fr.insa.clavardator.ui.chat;
2 2
 
3 3
 import fr.insa.clavardator.ui.users.UserActiveIndicatorController;
4 4
 import fr.insa.clavardator.users.PeerUser;
5
+import javafx.application.Platform;
5 6
 import javafx.fxml.FXML;
6 7
 import javafx.fxml.Initializable;
7 8
 import javafx.scene.control.Label;
8 9
 
10
+import java.beans.PropertyChangeEvent;
9 11
 import java.net.URL;
10 12
 import java.util.ArrayList;
11 13
 import java.util.ResourceBundle;
@@ -16,12 +18,26 @@ public class ChatHeaderController implements Initializable {
16 18
 	private Label remoteUsernameLabel;
17 19
 	@FXML
18 20
 	private UserActiveIndicatorController indicatorController;
21
+	private PeerUser user;
19 22
 
20 23
 	@Override
21 24
 	public void initialize(URL location, ResourceBundle resources) {
22 25
 	}
23 26
 
27
+	private void onUsernameChange(PropertyChangeEvent propertyChangeEvent) {
28
+		if (propertyChangeEvent.getPropertyName().equals("username")) {
29
+			final String newUsername = (String) propertyChangeEvent.getNewValue();
30
+			Platform.runLater(() -> remoteUsernameLabel.setText(newUsername));
31
+		}
32
+	}
33
+
24 34
 	public void setRemoteUser(PeerUser remoteUser) {
35
+		if (this.user != null) {
36
+			// remove old observer before setting new user
37
+			this.user.removeObserver(this::onUsernameChange);
38
+		}
39
+		this.user = remoteUser;
40
+		user.addObserver(this::onUsernameChange);
25 41
 		remoteUsernameLabel.setText(remoteUser.getUsername());
26 42
 		indicatorController.setUser(remoteUser);
27 43
 		indicatorController.setSize(10.0);

+ 2
- 1
src/main/java/fr/insa/clavardator/ui/users/UserActiveIndicatorController.java View File

@@ -2,6 +2,7 @@ package fr.insa.clavardator.ui.users;
2 2
 
3 3
 import fr.insa.clavardator.users.PeerUser;
4 4
 import fr.insa.clavardator.util.Log;
5
+import javafx.application.Platform;
5 6
 import javafx.fxml.FXML;
6 7
 import javafx.fxml.Initializable;
7 8
 import javafx.scene.shape.Circle;
@@ -34,7 +35,7 @@ public class UserActiveIndicatorController implements Initializable {
34 35
 		if (propertyChangeEvent.getPropertyName().equals("state")) {
35 36
 			final PeerUser.State newState = (PeerUser.State) propertyChangeEvent.getNewValue();
36 37
 			Log.v(this.getClass().getSimpleName(), "User state updated to " + newState.toString());
37
-			updateState(newState == PeerUser.State.CONNECTED);
38
+			Platform.runLater(() -> updateState(newState == PeerUser.State.CONNECTED));
38 39
 		}
39 40
 	}
40 41
 

Loading…
Cancel
Save