Moved the LoginDialogController class inside the LoginDialog class

This commit is contained in:
Kai S. K. Engelbart 2020-04-10 11:01:03 +02:00
parent 0b05d54953
commit 2b09a2dbde
5 changed files with 125 additions and 121 deletions

View File

@ -46,9 +46,10 @@ public class Client implements Closeable {
private volatile Set<? extends Contact> contacts; private volatile Set<? extends Contact> contacts;
private volatile boolean rejected; private volatile boolean rejected;
// Configuration and logging // Configuration, logging and event management
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Client.class); private static final Logger logger = EnvoyLog.getLogger(Client.class);
private static final EventBus eventBus = EventBus.getInstance();
/** /**
* Enters the online mode by acquiring a user ID from the server. As a * Enters the online mode by acquiring a user ID from the server. As a
@ -80,7 +81,7 @@ public class Client implements Closeable {
// Register user creation processor, contact list processor and message cache // Register user creation processor, contact list processor and message cache
receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); }); receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); });
receiver.registerProcessor(Message.class, receivedMessageCache); receiver.registerProcessor(Message.class, receivedMessageCache);
receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; EventBus.getInstance().dispatch(evt); }); receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; eventBus.dispatch(evt); });
rejected = false; rejected = false;
@ -146,20 +147,19 @@ public class Client implements Closeable {
receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator); receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator);
// Process name changes // Process name changes
receiver.registerProcessor(NameChangeEvent.class, evt -> { localDB.replaceContactName(evt); EventBus.getInstance().dispatch(evt); }); receiver.registerProcessor(NameChangeEvent.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); });
// Process contact searches // Process contact searches
receiver.registerProcessor(ContactSearchResult.class, EventBus.getInstance()::dispatch); receiver.registerProcessor(ContactSearchResult.class, eventBus::dispatch);
receiver.registerProcessor(Contact.class, receiver.registerProcessor(Contact.class,
contacts -> EventBus.getInstance() contacts -> eventBus.dispatch(new ContactOperationEvent(contacts.getContacts().iterator().next(), ElementOperation.ADD)));
.dispatch(new ContactOperationEvent(contacts.getContacts().iterator().next(), ElementOperation.ADD)));
// Process group size changes // Process group size changes
receiver.registerProcessor(GroupResizeEvent.class, evt -> { localDB.updateGroup(evt); EventBus.getInstance().dispatch(evt); }); receiver.registerProcessor(GroupResizeEvent.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); });
// Send event // Send event
EventBus.getInstance().register(SendEvent.class, evt -> { eventBus.register(SendEvent.class, evt -> {
try { try {
sendEvent(evt.get()); sendEvent(evt.get());
} catch (IOException e) { } catch (IOException e) {

View File

@ -2,6 +2,7 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?> <?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
@ -12,52 +13,37 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" <DialogPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="envoy.client.ui.LoginDialog$Controller">
prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" <content>
xmlns:fx="http://javafx.com/fxml/1" <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0">
fx:controller="envoy.client.ui.LoginDialogController"> <children>
<children> <Label text="User Login">
<Label text="User Login"> <font>
<font> <Font size="26.0" />
<Font size="26.0" /> </font>
</font> </Label>
</Label> <GridPane>
<GridPane> <columnConstraints>
<columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="40.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
minWidth="10.0" percentWidth="40.0" prefWidth="100.0" /> </columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" <rowConstraints>
minWidth="10.0" prefWidth="100.0" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</columnConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<rowConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" </rowConstraints>
vgrow="SOMETIMES" /> <children>
<RowConstraints minHeight="10.0" prefHeight="30.0" <Label text="User Name:" />
vgrow="SOMETIMES" /> <Label text="Password" GridPane.rowIndex="1" />
<RowConstraints minHeight="10.0" prefHeight="30.0" <Label fx:id="repeatPasswordLabel" text="Repeat Password:" visible="false" GridPane.rowIndex="2" />
vgrow="SOMETIMES" /> <TextField fx:id="userTextField" GridPane.columnIndex="1" />
</rowConstraints> <PasswordField fx:id="repeatPasswordField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<children> <PasswordField fx:id="repeatPasswordField" visible="false" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="User Name:" /> </children>
<Label text="Password" GridPane.rowIndex="1" /> </GridPane>
<Label text="Repeat Password:" visible="false" <CheckBox fx:id="registerCheckBox" mnemonicParsing="false" onAction="#registerCheckboxChanged" text="Register" />
GridPane.rowIndex="2" fx:id="repeatPasswordLabel" /> </children>
<TextField GridPane.columnIndex="1" /> </VBox>
<PasswordField GridPane.columnIndex="1" </content>
GridPane.rowIndex="1" fx:id="repeatPasswordField"/> </DialogPane>
<PasswordField visible="false"
GridPane.columnIndex="1" GridPane.rowIndex="2" fx:id="repeatPasswordField"/>
</children>
</GridPane>
<CheckBox mnemonicParsing="false" text="Register" fx:id="registerCheckBox" onAction="#registerCheckboxChanged"/>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button cancelButton="true" mnemonicParsing="false"
text="Cancel" onAction="#cancelButtonClicked"/>
<Button defaultButton="true" mnemonicParsing="false"
text="Submit" disable="true" onAction="#submitButtonClicked"/>
</children>
</HBox>
</children>
</VBox>

View File

@ -0,0 +1,70 @@
package envoy.client.ui;
import java.io.IOException;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>LoginDialog.java</strong><br>
* Created: <strong>03.04.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public final class LoginDialog extends Dialog<Void> {
/**
* Loads the login dialog using the FXML file {@code LoginDialog.fxml}.
*
* @throws IOException if an exception occurs during loading
* @since Envoy Client v0.1-beta
*/
public LoginDialog() throws IOException {
final var dialogPane = FXMLLoader.<DialogPane>load(getClass().getResource("LoginDialog.fxml"));
// Configure buttons
dialogPane.getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.OK);
setDialogPane(dialogPane);
}
public static class Controller {
@FXML
private TextField userTextField;
@FXML
private PasswordField passwordField;
@FXML
private PasswordField repeatPasswordField;
@FXML
private Label repeatPasswordLabel;
@FXML
private CheckBox registerCheckBox;
@FXML
private void initialize() {
// Set initial cursor
Platform.runLater(userTextField::requestFocus);
}
@FXML
private void registerCheckboxChanged() {
// Make repeat password field and label visible / invisible
repeatPasswordField.setVisible(registerCheckBox.isSelected());
repeatPasswordLabel.setVisible(registerCheckBox.isSelected());
// Clear repeat password field if registration cancelled
if (!registerCheckBox.isSelected()) repeatPasswordField.clear();
}
}
}

View File

@ -1,53 +0,0 @@
package envoy.client.ui;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>LoginDialogController.java</strong><br>
* Created: <strong>03.04.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public final class LoginDialogController {
@FXML
private PasswordField passwordField;
@FXML
private PasswordField repeatPasswordField;
@FXML
private Label repeatPasswordLabel;
@FXML
private CheckBox registerCheckBox;
@FXML
private void registerCheckboxChanged() {
// Make repeat password field and label visible / invisible
repeatPasswordField.setVisible(registerCheckBox.isSelected());
repeatPasswordLabel.setVisible(registerCheckBox.isSelected());
// Clear repeat password field if registration cancelled
if (!registerCheckBox.isSelected()) repeatPasswordField.clear();
}
@FXML
private void submitButtonClicked() {
}
@FXML
private void cancelButtonClicked(ActionEvent e) { closeStage(e); }
private void closeStage(ActionEvent e) { ((Stage) ((Node) e.getSource()).getScene().getWindow()).close(); }
}

View File

@ -12,7 +12,6 @@ import javax.swing.JOptionPane;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.net.WriteProxy; import envoy.client.net.WriteProxy;
import envoy.client.ui.container.LoginDialog;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.User; import envoy.data.User;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
@ -91,9 +90,18 @@ public final class Startup extends Application {
// Initialize client and unread message cache // Initialize client and unread message cache
client = new Client(); client = new Client();
cache = new Cache<>(); cache = new Cache<>();
}
/**
* {@inheritDoc}
*/
@Override
public void start(Stage stage) throws Exception {
// Try to connect to the server // Try to connect to the server
new LoginDialog(client, localDB, cache); // new LoginDialog(client, localDB, cache);
new LoginDialog().showAndWait();
System.exit(0);
// Set client user in local database // Set client user in local database
localDB.setUser(client.getSender()); localDB.setUser(client.getSender());
@ -128,13 +136,6 @@ public final class Startup extends Application {
.filter(u -> u instanceof User && u != localDB.getUser()) .filter(u -> u instanceof User && u != localDB.getUser())
.map(User.class::cast) .map(User.class::cast)
.forEach(u -> u.setStatus(UserStatus.OFFLINE)); .forEach(u -> u.setStatus(UserStatus.OFFLINE));
}
/**
* {@inheritDoc}
*/
@Override
public void start(Stage stage) throws Exception {
// Prepare stage and load ChatScene // Prepare stage and load ChatScene
var loader = new FXMLLoader(getClass().getResource("ChatScene.fxml")); var loader = new FXMLLoader(getClass().getResource("ChatScene.fxml"));