using listeners instead of a main thread for main logic
This commit is contained in:
parent
3177714fc5
commit
3634a61ee5
11 changed files with 229 additions and 47 deletions
|
|
@ -52,12 +52,9 @@ public abstract class GuiWindow implements Initializable{
|
|||
modalStage.showAndWait();
|
||||
}
|
||||
|
||||
/**
|
||||
* hide the stage
|
||||
*/
|
||||
public void close(){
|
||||
public void hide(){
|
||||
if(stage != null){
|
||||
stage.close();
|
||||
this.stage.hide();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.coder.client.gui.login;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -16,18 +17,20 @@ import javafx.scene.input.KeyCode;
|
|||
import javafx.scene.input.KeyEvent;
|
||||
|
||||
import com.coder.client.gui.GuiWindow;
|
||||
import com.coder.client.gui.selectProject.SelectProjectDialogListener;
|
||||
|
||||
public class LoginDialog extends GuiWindow {
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
private HashSet<LoginDialogListener> listeners;
|
||||
|
||||
@FXML private TextField usernameTextField;
|
||||
@FXML private PasswordField passwordPasswordField;
|
||||
@FXML private Button cancelButton;
|
||||
@FXML private Button loginButton;
|
||||
private LoginDialogAction action;
|
||||
|
||||
public LoginDialog() throws IOException {
|
||||
super(LoginDialog.class.getResource("LoginDialog.fxml"));
|
||||
this.listeners = new HashSet<LoginDialogListener>();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -37,8 +40,12 @@ public class LoginDialog extends GuiWindow {
|
|||
|
||||
@Override
|
||||
protected void willShow(){
|
||||
action = LoginDialogAction.CANCEL;
|
||||
passwordPasswordField.setText("");
|
||||
for(LoginDialogListener listener : this.listeners){
|
||||
listener.willShow();
|
||||
}
|
||||
if(!usernameTextField.getText().isEmpty() && !passwordPasswordField.getText().isEmpty()){
|
||||
loginButton.fire();
|
||||
}
|
||||
if(!usernameTextField.getText().isEmpty()){
|
||||
passwordPasswordField.requestFocus();
|
||||
}
|
||||
|
|
@ -57,31 +64,28 @@ public class LoginDialog extends GuiWindow {
|
|||
|
||||
@FXML
|
||||
protected void login(ActionEvent event){
|
||||
action = LoginDialogAction.LOGIN;
|
||||
this.close();
|
||||
for(LoginDialogListener listener : this.listeners){
|
||||
listener.login(usernameTextField.getText(), passwordPasswordField.getText().toCharArray());
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void cancel(ActionEvent event){
|
||||
action = LoginDialogAction.CANCEL;
|
||||
this.close();
|
||||
}
|
||||
|
||||
public LoginDialogAction getAction(){
|
||||
return action;
|
||||
for(LoginDialogListener listener : this.listeners){
|
||||
listener.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public void setUsername(String username){
|
||||
usernameTextField.setText(username);
|
||||
}
|
||||
|
||||
public String getUsername(){
|
||||
return usernameTextField.getText();
|
||||
public void setPassword(char[] password){
|
||||
passwordPasswordField.setText(new String(password));
|
||||
}
|
||||
|
||||
public char[] getPassword(){
|
||||
//TODO: fix this since is not safe since the password will be in a string
|
||||
return passwordPasswordField.getText().toCharArray();
|
||||
|
||||
public void addLoginDialogListener(LoginDialogListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
package com.coder.client.gui.login;
|
||||
|
||||
public enum LoginDialogAction {
|
||||
LOGIN,
|
||||
CANCEL
|
||||
}
|
||||
9
src/com/coder/client/gui/login/LoginDialogListener.java
Normal file
9
src/com/coder/client/gui/login/LoginDialogListener.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package com.coder.client.gui.login;
|
||||
|
||||
public interface LoginDialogListener {
|
||||
|
||||
public void cancel();
|
||||
public void login(String username, char[] password);
|
||||
public void willShow();
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.coder.client.gui.project;
|
||||
package com.coder.client.gui.selectProject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.coder.client.gui.project;
|
||||
package com.coder.client.gui.selectProject;
|
||||
|
||||
public interface SelectProjectDialogListener {
|
||||
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import java.lang.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
|
||||
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="217.0" prefWidth="396.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
|
||||
<children>
|
||||
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER">
|
||||
<content>
|
||||
<ListView fx:id="serverListView" prefHeight="200.0" prefWidth="200.0" />
|
||||
</content>
|
||||
</ScrollPane>
|
||||
<GridPane prefHeight="77.0" prefWidth="396.0">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
|
||||
</columnConstraints>
|
||||
<rowConstraints>
|
||||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
|
||||
</rowConstraints>
|
||||
<children>
|
||||
<HBox alignment="CENTER_RIGHT" prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="1">
|
||||
<children>
|
||||
<Button fx:id="cancelButton" mnemonicParsing="false" onAction="#cancel" text="Cancel">
|
||||
<HBox.margin>
|
||||
<Insets right="10.0" />
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
<Button fx:id="connectButton" mnemonicParsing="false" onAction="#connect" text="Connect">
|
||||
<HBox.margin>
|
||||
<Insets right="10.0" />
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
</children>
|
||||
</HBox>
|
||||
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" />
|
||||
</children>
|
||||
</GridPane>
|
||||
</children>
|
||||
</VBox>
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package com.coder.client.gui.selectServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import com.coder.client.gui.GuiWindow;
|
||||
|
||||
public class SelectServerDialog extends GuiWindow {
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
private HashSet<SelectServerDialogListener> listeners;
|
||||
private String url = null;
|
||||
|
||||
@FXML private Button cancelButton;
|
||||
@FXML private Button connectButton;
|
||||
|
||||
public SelectServerDialog() throws IOException {
|
||||
super(SelectServerDialog.class.getResource("SelectServerDialog.fxml"));
|
||||
listeners = new HashSet<SelectServerDialogListener>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void willShow() {
|
||||
for(SelectServerDialogListener listener : this.listeners){
|
||||
listener.willShow();
|
||||
}
|
||||
if(url != null){
|
||||
connectButton.fire();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void cancel(ActionEvent event){
|
||||
for(SelectServerDialogListener listener : this.listeners){
|
||||
listener.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void connect(ActionEvent event){
|
||||
for(SelectServerDialogListener listener : this.listeners){
|
||||
listener.connect(url);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSelectProjectDialogListener(SelectServerDialogListener listener){
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
public void setURL(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.coder.client.gui.selectServer;
|
||||
|
||||
public interface SelectServerDialogListener {
|
||||
|
||||
void willShow();
|
||||
|
||||
void cancel();
|
||||
|
||||
void connect(String url);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue