Merge branch 'develop' into f/notify_user_of_empty_IDGenerator

This commit is contained in:
delvh 2020-06-27 11:25:49 +02:00 committed by GitHub
commit 3aace6d255
6 changed files with 51 additions and 54 deletions

View File

@ -1,6 +1,5 @@
package envoy.client.ui; package envoy.client.ui;
import java.io.IOException;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.EnumSet; import java.util.EnumSet;
@ -26,10 +25,9 @@ public class IconUtil {
* *
* @param path the path to the icon inside the resource folder * @param path the path to the icon inside the resource folder
* @return the icon * @return the icon
* @throws IOException if the loading process failed
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public static Image load(String path) throws IOException { return new Image(IconUtil.class.getResource(path).toExternalForm()); } public static Image load(String path) { return new Image(IconUtil.class.getResource(path).toExternalForm()); }
/** /**
* Loads an icon from the resource folder and scales it to a given size. * Loads an icon from the resource folder and scales it to a given size.
@ -37,10 +35,9 @@ public class IconUtil {
* @param path the path to the icon inside the resource folder * @param path the path to the icon inside the resource folder
* @param size the size to scale the icon to * @param size the size to scale the icon to
* @return the scaled icon * @return the scaled icon
* @throws IOException if the loading process failed
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public static Image load(String path, int size) throws IOException { public static Image load(String path, int size) {
return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true); return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
} }
@ -56,13 +53,12 @@ public class IconUtil {
* @param size the size to scale the icons to * @param size the size to scale the icons to
* @return a map containing the loaded icons with the corresponding enum * @return a map containing the loaded icons with the corresponding enum
* constants as keys * constants as keys
* @throws IOException if the loading process failed
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public static <T extends Enum<T>> EnumMap<T, Image> loadByEnum(Class<T> enumClass, int size) throws IOException { public static <T extends Enum<T>> EnumMap<T, Image> loadByEnum(Class<T> enumClass, int size) {
var icons = new EnumMap<T, Image>(enumClass); final var icons = new EnumMap<T, Image>(enumClass);
var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/"; final var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
for (var e : EnumSet.allOf(enumClass)) for (final var e : EnumSet.allOf(enumClass))
icons.put(e, load(path + e.toString().toLowerCase() + ".png", size)); icons.put(e, load(path + e.toString().toLowerCase() + ".png", size));
return icons; return icons;
} }

View File

@ -1,9 +1,7 @@
package envoy.client.ui; package envoy.client.ui;
import java.io.IOException;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@ -15,7 +13,6 @@ import javafx.scene.layout.VBox;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.data.User; import envoy.data.User;
import envoy.util.EnvoyLog;
/** /**
* Displays a single message inside the message list. * Displays a single message inside the message list.
@ -29,18 +26,9 @@ import envoy.util.EnvoyLog;
*/ */
public class MessageListCell extends ListCell<Message> { public class MessageListCell extends ListCell<Message> {
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
private static Map<MessageStatus, Image> statusImages;
private static User client; private static User client;
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
static { private static final Map<MessageStatus, Image> statusImages = IconUtil.loadByEnum(MessageStatus.class, 16);
try {
statusImages = IconUtil.loadByEnum(MessageStatus.class, 16);
} catch (final IOException e) {
e.printStackTrace();
EnvoyLog.getLogger(MessageListCell.class).log(Level.WARNING, "could not load status icons: ", e);
}
}
/** /**
* Displays the text, the data of creation and the status of a message. * Displays the text, the data of creation and the status of a message.

View File

@ -95,11 +95,7 @@ public final class ChatScene {
messageList.setCellFactory(listView -> new MessageListCell()); messageList.setCellFactory(listView -> new MessageListCell());
userList.setCellFactory(listView -> new ContactListCell()); userList.setCellFactory(listView -> new ContactListCell());
try {
settingsButton.setGraphic(new ImageView(IconUtil.load("/icons/settings.png", 16))); settingsButton.setGraphic(new ImageView(IconUtil.load("/icons/settings.png", 16)));
} catch (final IOException e2) {
logger.log(Level.WARNING, "Could not load settings icon", e2);
}
// Listen to received messages // Listen to received messages
eventBus.register(MessageCreationEvent.class, e -> { eventBus.register(MessageCreationEvent.class, e -> {

View File

@ -5,15 +5,16 @@ import java.util.stream.Collectors;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
import envoy.client.event.SendEvent; import envoy.client.event.SendEvent;
import envoy.client.ui.ContactListCell; import envoy.client.ui.ContactListCell;
import envoy.client.ui.SceneContext; import envoy.client.ui.SceneContext;
import envoy.data.Contact; import envoy.data.Contact;
import envoy.data.User;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.GroupCreation; import envoy.event.GroupCreation;
import envoy.util.Bounds;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -25,21 +26,18 @@ import envoy.event.GroupCreation;
*/ */
public class GroupCreationScene { public class GroupCreationScene {
@FXML
private Button backButton;
@FXML @FXML
private Button createButton; private Button createButton;
@FXML @FXML
private TextField groupNameBar; private TextField groupNameField;
@FXML @FXML
private ListView<Contact> contactList; private ListView<Contact> contactList;
private SceneContext sceneContext; private SceneContext sceneContext;
private static EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
@FXML @FXML
private void initialize() { private void initialize() {
@ -56,22 +54,36 @@ public class GroupCreationScene {
public void initializeData(SceneContext sceneContext, LocalDB localDB) { public void initializeData(SceneContext sceneContext, LocalDB localDB) {
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
Platform.runLater(() -> contactList.getItems() Platform.runLater(() -> contactList.getItems()
.addAll(localDB.getUsers() .addAll(localDB.getUsers().values().stream().filter(c -> c.getID() != localDB.getUser().getID()).collect(Collectors.toList())));
.values()
.stream()
.filter(c -> c instanceof User && c.getID() != localDB.getUser().getID())
.collect(Collectors.toList())));
} }
/** /**
* Sends a {@link GroupCreation} to the server. * Enables the {@code createButton} if at least one contact is selected.
* *
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@FXML @FXML
private void sendGroupObject() { private void contactListClicked() { createButton.setDisable(contactList.getSelectionModel().isEmpty()); }
eventBus.dispatch(new SendEvent(new GroupCreation(groupNameBar.getText(),
/**
* Sends a {@link GroupCreation} to the server and closes this scene.
* <p>
* If the given group name is not valid, an error is displayed instead.
*
* @since Envoy Client v0.1-beta
*/
@FXML
private void createButtonClicked() {
final var name = groupNameField.getText();
if (!Bounds.isValidContactName(name)) {
new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
groupNameField.clear();
} else {
eventBus.dispatch(new SendEvent(new GroupCreation(name,
contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet()))));
new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait();
sceneContext.pop();
}
} }
@FXML @FXML

View File

@ -23,6 +23,7 @@ import envoy.event.EventBus;
import envoy.event.HandshakeRejection; import envoy.event.HandshakeRejection;
import envoy.event.MessageStatusChange; import envoy.event.MessageStatusChange;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.Bounds;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -115,7 +116,11 @@ public final class LoginScene {
if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
repeatPasswordField.clear(); repeatPasswordField.clear();
} else performHandshake( } else if (!Bounds.isValidContactName(userTextField.getText())) {
new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
userTextField.clear();
} else
performHandshake(
new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION)); new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION));
} }

View File

@ -14,7 +14,7 @@
<children> <children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0"> <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children> <children>
<TextField fx:id="groupNameBar" prefColumnCount="22" promptText="Enter Group Name"> <TextField fx:id="groupNameField" prefColumnCount="22" promptText="Enter Group Name">
<HBox.margin> <HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin> </HBox.margin>
@ -38,21 +38,21 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding> </padding>
</Label> </Label>
<ListView fx:id="contactList" prefHeight="314.0" prefWidth="600.0"> <ListView fx:id="contactList" onMouseClicked="#contactListClicked" prefHeight="314.0" prefWidth="600.0">
<VBox.margin> <VBox.margin>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" /> <Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
</VBox.margin> </VBox.margin>
<padding> <padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding></ListView> </padding></ListView>
<Button mnemonicParsing="false" onAction="#sendGroupObject" text="Create"> <Button fx:id="createButton" defaultButton="true" disable="true" mnemonicParsing="false" onAction="#createButtonClicked" text="Create">
<VBox.margin> <VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin> </VBox.margin>
<padding> <padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding></Button> </padding></Button>
<Button fx:id="backButton" cancelButton="true" mnemonicParsing="true" onAction="#backButtonClicked" text="_Back"> <Button cancelButton="true" mnemonicParsing="true" onAction="#backButtonClicked" text="_Back">
<VBox.margin> <VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin> </VBox.margin>