Skip to content

Commit d7e405b

Browse files
Michalis-DragoutasMichalis-Dragoutas
authored andcommitted
basic chat
1 parent f8d3264 commit d7e405b

10 files changed

Lines changed: 223 additions & 9 deletions

File tree

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
<artifactId>javafx-fxml</artifactId>
4646
<version>${javafx.version}</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>io.github.cdimascio</groupId>
50+
<artifactId>dotenv-java</artifactId>
51+
<version>3.2.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>tools.jackson.core</groupId>
55+
<artifactId>jackson-databind</artifactId>
56+
<version>3.0.1</version>
57+
</dependency>
4858
</dependencies>
4959
<build>
5060
<plugins>
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.example;
22

3+
import javafx.event.ActionEvent;
34
import javafx.fxml.FXML;
45
import javafx.scene.control.Label;
6+
import javafx.scene.control.ListView;
7+
import javafx.scene.control.TextField;
58

69
/**
710
* Controller layer: mediates between the view (FXML) and the model.
@@ -10,13 +13,26 @@ public class HelloController {
1013

1114
private final HelloModel model = new HelloModel();
1215

16+
@FXML
17+
public ListView<NtfyMessageDto> messageView ;
18+
1319
@FXML
1420
private Label messageLabel;
1521

22+
@FXML
23+
private TextField messageField;
24+
1625
@FXML
1726
private void initialize() {
18-
if (messageLabel != null) {
19-
messageLabel.setText(model.getGreeting());
27+
messageLabel.setText(model.getGreeting());
28+
messageView.setItems(model.getMessages());
29+
}
30+
31+
public void sendMessage(ActionEvent actionEvent) {
32+
String text = messageField.getText().trim();
33+
if (!text.isEmpty()) {
34+
model.sendMessage(text);
35+
messageField.clear();
2036
}
2137
}
2238
}

src/main/java/com/example/HelloFX.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example;
22

3+
import io.github.cdimascio.dotenv.Dotenv;
34
import javafx.application.Application;
45
import javafx.fxml.FXMLLoader;
56
import javafx.scene.Parent;
@@ -10,6 +11,7 @@ public class HelloFX extends Application {
1011

1112
@Override
1213
public void start(Stage stage) throws Exception {
14+
1315
FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("hello-view.fxml"));
1416
Parent root = fxmlLoader.load();
1517
Scene scene = new Scene(root, 640, 480);

src/main/java/com/example/HelloModel.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package com.example;
22

3+
import io.github.cdimascio.dotenv.Dotenv;
4+
import javafx.application.Platform;
5+
import javafx.collections.FXCollections;
6+
import javafx.collections.ObservableList;
7+
import tools.jackson.databind.ObjectMapper;
8+
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.http.HttpClient;
12+
import java.net.http.HttpRequest;
13+
import java.net.http.HttpResponse;
14+
import java.util.Objects;
15+
316
/**
417
* Model layer: encapsulates application data and business logic.
518
*/
619
public class HelloModel {
20+
21+
private final String hostName;
22+
private final HttpClient http = HttpClient.newHttpClient();
23+
private final ObjectMapper mapper = new ObjectMapper();
24+
private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList();
25+
26+
private boolean senderMe=false;
27+
28+
public HelloModel() {
29+
Dotenv dotenv = Dotenv.load();
30+
hostName = Objects.requireNonNull(dotenv.get("HOST_NAME"));
31+
receiveMessage();
32+
}
33+
34+
public ObservableList<NtfyMessageDto> getMessages() {
35+
return messages;
36+
}
37+
738
/**
839
* Returns a greeting based on the current Java and JavaFX versions.
940
*/
@@ -12,4 +43,55 @@ public String getGreeting() {
1243
String javafxVersion = System.getProperty("javafx.version");
1344
return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".";
1445
}
46+
47+
public void sendMessage(String text) {
48+
49+
senderMe=true;
50+
51+
HttpRequest httpRequest= HttpRequest.newBuilder()
52+
.POST(HttpRequest.BodyPublishers.ofString("Hello world"))
53+
.uri(URI.create(hostName + "/mytopic"))
54+
.build();
55+
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.discarding())
56+
.exceptionally(ex -> {
57+
System.out.println("Error sending message: " + ex.getMessage());
58+
return null;
59+
});
60+
try {
61+
var response = http.send(httpRequest, HttpResponse.BodyHandlers.ofString());
62+
} catch (IOException e) {
63+
System.out.println("Error sending message");
64+
} catch (InterruptedException e) {
65+
System.out.println("Interrupted sending request");
66+
}
67+
68+
}
69+
70+
public void receiveMessage(){
71+
HttpRequest httpRequest = HttpRequest.newBuilder()
72+
.GET()
73+
.uri(URI.create(hostName + "/mytopic/json"))
74+
.build();
75+
76+
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines())
77+
.thenAccept(response -> response.body()
78+
.map(s -> {
79+
try {
80+
return mapper.readValue(s, NtfyMessageDto.class);
81+
} catch (Exception e) {
82+
System.err.println("Failed to parse message: " + e.getMessage());
83+
return null;
84+
}
85+
})
86+
.filter(Objects::nonNull)
87+
.filter(msg -> "message".equals(msg.event()))
88+
.forEach(msg->{
89+
if (senderMe) {
90+
senderMe=false;
91+
return;
92+
}
93+
Platform.runLater(() -> messages.add(msg));
94+
}));
95+
}
96+
1597
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example;
2+
3+
public class ManyParameters {
4+
5+
public ManyParameters(String computerName, int timeout, String method, int size, byte[] data) {
6+
7+
}
8+
9+
static void main () {
10+
11+
ManyParametersBuilder builder= new ManyParametersBuilder();
12+
builder
13+
.setComputerName("localhost")
14+
.setTimeout(10)
15+
.setSize(0)
16+
.createManyParameters();
17+
}
18+
19+
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example;
2+
3+
public class ManyParametersBuilder {
4+
private String computerName;
5+
private int timeout = 0;
6+
private String method;
7+
private int size = 0;
8+
private byte[] data = null;
9+
10+
public ManyParametersBuilder setComputerName(String computerName) {
11+
this.computerName = computerName;
12+
return this;
13+
}
14+
15+
public ManyParametersBuilder setTimeout(int timeout) {
16+
this.timeout = timeout;
17+
return this;
18+
}
19+
20+
public ManyParametersBuilder setMethod(String method) {
21+
this.method = method;
22+
return this;
23+
}
24+
25+
public ManyParametersBuilder setSize(int size) {
26+
this.size = size;
27+
return this;
28+
}
29+
30+
public ManyParametersBuilder setData(byte[] data) {
31+
this.data = data;
32+
return this;
33+
}
34+
35+
public ManyParameters createManyParameters() {
36+
return new ManyParameters(computerName, timeout, method, size, data);
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import java.text.SimpleDateFormat;
6+
import java.time.format.DateTimeFormatter;
7+
import java.util.Date;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
public record NtfyMessageDto(String id, long time, String event, String topic, String message){
11+
private static final DateTimeFormatter date =
12+
DateTimeFormatter.ofPattern("HH:mm");
13+
@Override
14+
public String toString(){
15+
String timeStr = new SimpleDateFormat("HH:mm").format(new Date(time * 1000));
16+
return timeStr+ " " + message ;
17+
}
18+
19+
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example;
2+
3+
public class Singelton {
4+
5+
private final static Singelton instance= new Singelton();
6+
7+
public static Singelton getInstance(){
8+
return instance;
9+
}
10+
11+
}

src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module hellofx {
22
requires javafx.controls;
33
requires javafx.fxml;
4+
requires io.github.cdimascio.dotenv.java;
5+
requires java.net.http;
6+
requires tools.jackson.databind;
47

58
opens com.example to javafx.fxml;
69
exports com.example;
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<?import javafx.scene.layout.StackPane?>
3-
<?import javafx.scene.control.Label?>
42

5-
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.HelloController">
6-
<children>
7-
<Label fx:id="messageLabel" text="Hello, JavaFX!" />
8-
</children>
9-
</StackPane>
3+
<?import javafx.geometry.*?>
4+
<?import javafx.scene.control.*?>
5+
<?import javafx.scene.layout.*?>
6+
7+
<VBox prefHeight="482.0" prefWidth="248.0" spacing="5.0" xmlns="http://javafx.com/javafx/17.0.12" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.HelloController">
8+
<Label fx:id="messageLabel" text="CHAT LAB3" />
9+
<ListView fx:id="messageView" prefHeight="373.0" prefWidth="248.0" VBox.vgrow="ALWAYS" />
10+
11+
<HBox spacing="10.0">
12+
<TextField fx:id="messageField" prefHeight="50.0" prefWidth="187.0" promptText="Type your message..." HBox.hgrow="ALWAYS">
13+
<HBox.margin>
14+
<Insets bottom="5.0" left="5.0" />
15+
</HBox.margin></TextField>
16+
<Button onAction="#sendMessage" prefHeight="34.0" prefWidth="51.0" text="Send">
17+
<HBox.margin>
18+
<Insets right="2.0" />
19+
</HBox.margin></Button>
20+
</HBox>
21+
</VBox>

0 commit comments

Comments
 (0)