Initial commit
This commit is contained in:
commit
e4c00f10e8
37 changed files with 2512 additions and 0 deletions
27
client/README.md
Normal file
27
client/README.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
Assuming that you have [Maven](https://maven.apache.org/install.html) installed, you can run the project out-of-the-box from your terminal via
|
||||
|
||||
mvn -pl client -am javafx:run
|
||||
|
||||
from your project root (not from within the `client` folder!).
|
||||
|
||||
Starting the client within your IDE (Eclipse/IntelliJ) requires setting up OpenJFX.
|
||||
|
||||
First download (and unzip!) an [OpenJFX SDK](https://openjfx.io).
|
||||
Make sure that the download *matches your Java JDK version*.
|
||||
|
||||
Then create a *run configuration* for the `Main` class and add the following *VM* commands (which, in IntelliJ, are hidden by default):
|
||||
|
||||
--module-path="/path/to/javafx-sdk/lib"
|
||||
--add-modules=javafx.controls,javafx.fxml,javafx.web
|
||||
|
||||
Adjust the module path to *your* local download location and make sure you adapt the path
|
||||
to the `lib`(!) directory (not just the directory that you unzipped)...
|
||||
|
||||
*Tip:* Windows paths are different, they uses `\` as path separator and starts with a drive letter like `C:`.
|
||||
|
||||
*Tip:* Make sure not to forget the `/lib` at the end of the path.
|
||||
|
||||
*Tip:* Double-check that the path is correct. If you receive abstract errors, like `Module javafx.web not found`
|
||||
or a segmentation fault, you are likely not pointing to the right folder. Try opening the folder and check that
|
||||
it contains several .jar files, such as `javafx.controls.jar`.
|
||||
130
client/pom.xml
Normal file
130
client/pom.xml
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>csep</groupId>
|
||||
<artifactId>client</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<version.junit>5.10.1</version.junit>
|
||||
<version.mockito>5.8.0</version.mockito>
|
||||
<version.jersey>3.1.9</version.jersey>
|
||||
<version.jfx>25.0.1</version.jfx>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>csep</groupId>
|
||||
<artifactId>commons</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.core</groupId>
|
||||
<artifactId>jersey-client</artifactId>
|
||||
<version>${version.jersey}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.inject</groupId>
|
||||
<artifactId>jersey-hk2</artifactId>
|
||||
<version>${version.jersey}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-json-jackson</artifactId>
|
||||
<version>${version.jersey}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.activation</groupId>
|
||||
<artifactId>jakarta.activation-api</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>7.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JavaFX -->
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-fxml</artifactId>
|
||||
<version>${version.jfx}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-controls</artifactId>
|
||||
<version>${version.jfx}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-web</artifactId>
|
||||
<version>${version.jfx}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${version.junit}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${version.junit}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${version.mockito}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<configuration>
|
||||
<release>23</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-maven-plugin</artifactId>
|
||||
<version>0.0.8</version>
|
||||
<configuration>
|
||||
<mainClass>client.Main</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<configLocation>../checkstyle.xml</configLocation>
|
||||
<sourceDirectories>${project.basedir}</sourceDirectories>
|
||||
<includes>src/**/*.java,**/*.xml,**/*.yml</includes>
|
||||
<excludes>**/target/**</excludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>validate</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
57
client/src/main/java/client/Main.java
Normal file
57
client/src/main/java/client/Main.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client;
|
||||
|
||||
import static com.google.inject.Guice.createInjector;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import client.scenes.AddQuoteCtrl;
|
||||
import client.scenes.MainCtrl;
|
||||
import client.scenes.QuoteOverviewCtrl;
|
||||
import client.utils.ServerUtils;
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class Main extends Application {
|
||||
|
||||
private static final Injector INJECTOR = createInjector(new MyModule());
|
||||
private static final MyFXML FXML = new MyFXML(INJECTOR);
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException, IOException {
|
||||
launch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
|
||||
var serverUtils = INJECTOR.getInstance(ServerUtils.class);
|
||||
if (!serverUtils.isServerAvailable()) {
|
||||
var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down.";
|
||||
System.err.println(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml");
|
||||
var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml");
|
||||
|
||||
var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
|
||||
mainCtrl.initialize(primaryStage, overview, add);
|
||||
}
|
||||
}
|
||||
74
client/src/main/java/client/MyFXML.java
Normal file
74
client/src/main/java/client/MyFXML.java
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.util.Builder;
|
||||
import javafx.util.BuilderFactory;
|
||||
import javafx.util.Callback;
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class MyFXML {
|
||||
|
||||
private Injector injector;
|
||||
|
||||
public MyFXML(Injector injector) {
|
||||
this.injector = injector;
|
||||
}
|
||||
|
||||
public <T> Pair<T, Parent> load(Class<T> c, String... parts) {
|
||||
try {
|
||||
var loader = new FXMLLoader(getLocation(parts), null, null, new MyFactory(), StandardCharsets.UTF_8);
|
||||
Parent parent = loader.load();
|
||||
T ctrl = loader.getController();
|
||||
return new Pair<>(ctrl, parent);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private URL getLocation(String... parts) {
|
||||
var path = Path.of("", parts).toString();
|
||||
return MyFXML.class.getClassLoader().getResource(path);
|
||||
}
|
||||
|
||||
private class MyFactory implements BuilderFactory, Callback<Class<?>, Object> {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Builder<?> getBuilder(Class<?> type) {
|
||||
return new Builder() {
|
||||
@Override
|
||||
public Object build() {
|
||||
return injector.getInstance(type);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call(Class<?> type) {
|
||||
return injector.getInstance(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
client/src/main/java/client/MyModule.java
Normal file
34
client/src/main/java/client/MyModule.java
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Scopes;
|
||||
|
||||
import client.scenes.AddQuoteCtrl;
|
||||
import client.scenes.MainCtrl;
|
||||
import client.scenes.QuoteOverviewCtrl;
|
||||
|
||||
public class MyModule implements Module {
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
|
||||
binder.bind(AddQuoteCtrl.class).in(Scopes.SINGLETON);
|
||||
binder.bind(QuoteOverviewCtrl.class).in(Scopes.SINGLETON);
|
||||
}
|
||||
}
|
||||
96
client/src/main/java/client/scenes/AddQuoteCtrl.java
Normal file
96
client/src/main/java/client/scenes/AddQuoteCtrl.java
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client.scenes;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import client.utils.ServerUtils;
|
||||
import commons.Person;
|
||||
import commons.Quote;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.stage.Modality;
|
||||
|
||||
public class AddQuoteCtrl {
|
||||
|
||||
private final ServerUtils server;
|
||||
private final MainCtrl mainCtrl;
|
||||
|
||||
@FXML
|
||||
private TextField firstName;
|
||||
|
||||
@FXML
|
||||
private TextField lastName;
|
||||
|
||||
@FXML
|
||||
private TextField quote;
|
||||
|
||||
@Inject
|
||||
public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) {
|
||||
this.mainCtrl = mainCtrl;
|
||||
this.server = server;
|
||||
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
|
||||
public void ok() {
|
||||
try {
|
||||
server.addQuote(getQuote());
|
||||
} catch (WebApplicationException e) {
|
||||
|
||||
var alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.initModality(Modality.APPLICATION_MODAL);
|
||||
alert.setContentText(e.getMessage());
|
||||
alert.showAndWait();
|
||||
return;
|
||||
}
|
||||
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
|
||||
private Quote getQuote() {
|
||||
var p = new Person(firstName.getText(), lastName.getText());
|
||||
var q = quote.getText();
|
||||
return new Quote(p, q);
|
||||
}
|
||||
|
||||
private void clearFields() {
|
||||
firstName.clear();
|
||||
lastName.clear();
|
||||
quote.clear();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
case ENTER:
|
||||
ok();
|
||||
break;
|
||||
case ESCAPE:
|
||||
cancel();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
57
client/src/main/java/client/scenes/MainCtrl.java
Normal file
57
client/src/main/java/client/scenes/MainCtrl.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client.scenes;
|
||||
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class MainCtrl {
|
||||
|
||||
private Stage primaryStage;
|
||||
|
||||
private QuoteOverviewCtrl overviewCtrl;
|
||||
private Scene overview;
|
||||
|
||||
private AddQuoteCtrl addCtrl;
|
||||
private Scene add;
|
||||
|
||||
public void initialize(Stage primaryStage, Pair<QuoteOverviewCtrl, Parent> overview,
|
||||
Pair<AddQuoteCtrl, Parent> add) {
|
||||
this.primaryStage = primaryStage;
|
||||
this.overviewCtrl = overview.getKey();
|
||||
this.overview = new Scene(overview.getValue());
|
||||
|
||||
this.addCtrl = add.getKey();
|
||||
this.add = new Scene(add.getValue());
|
||||
|
||||
showOverview();
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
public void showOverview() {
|
||||
primaryStage.setTitle("Quotes: Overview");
|
||||
primaryStage.setScene(overview);
|
||||
overviewCtrl.refresh();
|
||||
}
|
||||
|
||||
public void showAdd() {
|
||||
primaryStage.setTitle("Quotes: Adding Quote");
|
||||
primaryStage.setScene(add);
|
||||
add.setOnKeyPressed(e -> addCtrl.keyPressed(e));
|
||||
}
|
||||
}
|
||||
71
client/src/main/java/client/scenes/QuoteOverviewCtrl.java
Normal file
71
client/src/main/java/client/scenes/QuoteOverviewCtrl.java
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client.scenes;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import client.utils.ServerUtils;
|
||||
import commons.Quote;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.scene.control.TableView;
|
||||
|
||||
public class QuoteOverviewCtrl implements Initializable {
|
||||
|
||||
private final ServerUtils server;
|
||||
private final MainCtrl mainCtrl;
|
||||
|
||||
private ObservableList<Quote> data;
|
||||
|
||||
@FXML
|
||||
private TableView<Quote> table;
|
||||
@FXML
|
||||
private TableColumn<Quote, String> colFirstName;
|
||||
@FXML
|
||||
private TableColumn<Quote, String> colLastName;
|
||||
@FXML
|
||||
private TableColumn<Quote, String> colQuote;
|
||||
|
||||
@Inject
|
||||
public QuoteOverviewCtrl(ServerUtils server, MainCtrl mainCtrl) {
|
||||
this.server = server;
|
||||
this.mainCtrl = mainCtrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
colFirstName.setCellValueFactory(q -> new SimpleStringProperty(q.getValue().person.firstName));
|
||||
colLastName.setCellValueFactory(q -> new SimpleStringProperty(q.getValue().person.lastName));
|
||||
colQuote.setCellValueFactory(q -> new SimpleStringProperty(q.getValue().quote));
|
||||
}
|
||||
|
||||
public void addQuote() {
|
||||
mainCtrl.showAdd();
|
||||
}
|
||||
|
||||
public void refresh() {
|
||||
var quotes = server.getQuotes();
|
||||
data = FXCollections.observableList(quotes);
|
||||
table.setItems(data);
|
||||
}
|
||||
}
|
||||
77
client/src/main/java/client/utils/ServerUtils.java
Normal file
77
client/src/main/java/client/utils/ServerUtils.java
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client.utils;
|
||||
|
||||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.ConnectException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
import org.glassfish.jersey.client.ClientConfig;
|
||||
|
||||
import commons.Quote;
|
||||
import jakarta.ws.rs.ProcessingException;
|
||||
import jakarta.ws.rs.client.ClientBuilder;
|
||||
import jakarta.ws.rs.client.Entity;
|
||||
import jakarta.ws.rs.core.GenericType;
|
||||
|
||||
public class ServerUtils {
|
||||
|
||||
private static final String SERVER = "http://localhost:8080/";
|
||||
|
||||
public void getQuotesTheHardWay() throws IOException, URISyntaxException {
|
||||
var url = new URI("http://localhost:8080/api/quotes").toURL();
|
||||
var is = url.openConnection().getInputStream();
|
||||
var br = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Quote> getQuotes() {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get(new GenericType<List<Quote>>() {});
|
||||
}
|
||||
|
||||
public Quote addQuote(Quote quote) {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.post(Entity.entity(quote, APPLICATION_JSON), Quote.class);
|
||||
}
|
||||
|
||||
public boolean isServerAvailable() {
|
||||
try {
|
||||
ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER) //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get();
|
||||
} catch (ProcessingException e) {
|
||||
if (e.getCause() instanceof ConnectException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
19
client/src/main/resources/client/scenes/AddQuote.fxml
Normal file
19
client/src/main/resources/client/scenes/AddQuote.fxml
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="146.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddQuoteCtrl">
|
||||
<children>
|
||||
<Button layoutX="419.0" layoutY="105.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
|
||||
<Button layoutX="350.0" layoutY="105.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
|
||||
<TextField fx:id="firstName" layoutX="109.0" layoutY="24.0" prefHeight="26.0" prefWidth="79.0" />
|
||||
<TextField fx:id="lastName" layoutX="286.0" layoutY="24.0" />
|
||||
<TextField fx:id="quote" layoutX="109.0" layoutY="61.0" prefHeight="26.0" prefWidth="345.0" />
|
||||
<Label layoutX="216.0" layoutY="29.0" text="Last Name" />
|
||||
<Label layoutX="28.0" layoutY="29.0" text="First Name" />
|
||||
<Label layoutX="28.0" layoutY="66.0" text="Quote" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
20
client/src/main/resources/client/scenes/QuoteOverview.fxml
Normal file
20
client/src/main/resources/client/scenes/QuoteOverview.fxml
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.TableColumn?>
|
||||
<?import javafx.scene.control.TableView?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.QuoteOverviewCtrl">
|
||||
<children>
|
||||
<Button layoutX="503.0" layoutY="360.0" mnemonicParsing="false" onAction="#addQuote" text="Add Quote" />
|
||||
<Button layoutX="431.0" layoutY="360.0" mnemonicParsing="false" onAction="#refresh" text="Refresh" />
|
||||
<TableView fx:id="table" layoutX="14.0" layoutY="14.0" prefHeight="337.0" prefWidth="572.0">
|
||||
<columns>
|
||||
<TableColumn fx:id="colFirstName" prefWidth="113.0" text="First Name" />
|
||||
<TableColumn fx:id="colLastName" prefWidth="114.0" text="Last Name" />
|
||||
<TableColumn fx:id="colQuote" prefWidth="344.0" text="Quote" />
|
||||
</columns>
|
||||
</TableView>
|
||||
</children>
|
||||
</AnchorPane>
|
||||
35
client/src/test/java/client/scenes/MainCtrlTest.java
Normal file
35
client/src/test/java/client/scenes/MainCtrlTest.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package client.scenes;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MainCtrlTest {
|
||||
|
||||
private MainCtrl sut;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
sut = new MainCtrl();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeSomeTests() {
|
||||
// TODO create replacement objects and write some tests
|
||||
// sut.initialize(null, null, null);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue