now using JavaFX instead of Swing

This commit is contained in:
Daniel Collin 2015-10-20 14:26:58 +00:00
parent 83e0196c50
commit 3177714fc5
22 changed files with 518 additions and 686 deletions

View file

@ -7,6 +7,13 @@ import java.util.logging.Logger;
import javax.swing.JOptionPane;
import com.coder.client.Session;
import com.coder.client.gui.editor.EditorWindow;
import com.coder.client.gui.editor.EditorWindowListener;
import com.coder.client.gui.login.LoginDialog;
import com.coder.client.gui.login.LoginDialogAction;
import com.coder.client.gui.project.SelectProjectDialog;
import com.coder.client.gui.project.SelectProjectDialogListener;
import com.coder.server.CoderServer;
import com.coder.server.message.CoderMessage;
import com.coder.server.message.ConfigData;
@ -16,31 +23,126 @@ import com.coder.server.message.ProjectRspMsg;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CoderClient extends Thread{
public class CoderClient extends Application{
public static final Logger logger = LogUtil.getLogger();
private int port = CoderServer.SERVER_PORT;
private static final int DEFAULT_CONNECTION_RETRIES = 5;
private String url;
private int port;
private Session session;
private ProjectEditorWindow projectEditorWindow;
private ProjectSelectDialog projectSelectDialog;
private String url;
private Stage mainStage;
private String username = null;
private char[] password = null;
private int connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES; //if zero, try forever
private EditorWindow editorWindow;
private LoginDialog loginDialog;
private SelectProjectDialog selectProjectDialog;
public CoderClient(String url, int port) {
this.url = url;
this.port = port;
this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
this.projectSelectDialog = new ProjectSelectDialog(null);
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage mainStage) throws Exception {
//setup logging
LogUtil.setGlobalLevel(Level.INFO);
LogUtil.setGlobalFormatter(new CompactLogFormatter());
//parse program arguments
Map<String, String> params = this.getParameters().getNamed();
String username = null;
char[] password = null;
boolean reconnectForever = false;
for(String key : params.keySet()){
String value = params.get(key);
if(key.equals("username")){
username = value;
}else if(key.equals("password")){
password = value.toCharArray();
}else if(key.equals("retry-connect-forever")){
reconnectForever = true;
}
}
//create a client instance
this.url = "127.0.0.1";
if(username != null){
this.username = username;
if(password != null){
this.password = password;
}
}
if(reconnectForever){
this.connectionRetriesLimit = 0;
}
//loading GUI
this.mainStage = mainStage;
mainStage.setTitle("CoderClient");
this.editorWindow = new EditorWindow();
this.loginDialog = new LoginDialog();
this.selectProjectDialog = new SelectProjectDialog();
//register listeners
registerListeners();
//start program logic
run();
}
private void registerListeners() {
this.selectProjectDialog.addSelectProjectDialogListener(new SelectProjectDialogListener() {
@Override
public void willShow() {
//clear current list of project
selectProjectDialog.clearProjectList();
//send a request for a new list of projects
CoderMessage msg = new CoderMessage();
msg.ProjectListReq = new ProjectListReqMsg();
try {
session.send(msg);
} catch (IOException e) {
logger.log(Level.SEVERE, "Unable to send ProjectListReqMsg", e);
closeCurrentSession();
}
}
@Override
public void openProject(String selectedProjectName) {
selectProjectDialog.close();
editorWindow.showOnStage(mainStage);
}
@Override
public void newProject() {
selectProjectDialog.close();
//TODO
}
@Override
public void cancel() {
selectProjectDialog.close();
}
});
this.editorWindow.addMessageSentListener(new EditorWindowListener() {
});
}
private void closeCurrentSession(){
if(this.session != null){
session.close();
session = null;
}
}
public void run(){
//keep track of the number of connection retries to the server
int connectionRetries = 0;
@ -80,10 +182,9 @@ public class CoderClient extends Thread{
}else{
//ask for username and password in a dialog window
logger.info("All or some login credentials have not been given. Using a dialog for input.");
LoginDialog loginDialog = new LoginDialog(username, null);
loginDialog.setVisible(true); //blocking
loginDialog.dispose();
if(loginDialog.getAction() == LoginDialog.LoginDialogAction.CANCEL){
loginDialog.setUsername(username);
loginDialog.showModal(this.mainStage);
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
logger.fine("Login dialog closed or canceled by the user. terminating.");
break;
}
@ -100,19 +201,13 @@ public class CoderClient extends Thread{
continue;
}
//forget the password
for(int i = 0; i < password.length; ++i){
password[i] = '*';
}
password = null;
//resister a message listener to the session
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
@Override
public void projectListRspReceived(Map<String, ProjectListData> projectListRsp) {
for(String projectName : projectListRsp.keySet()){
ProjectListData projectData = projectListRsp.get(projectName);
projectSelectDialog.addProjectToList(projectName, projectData.type);
selectProjectDialog.addProjectToList(projectName, projectData.type);
}
}
@Override
@ -128,136 +223,24 @@ public class CoderClient extends Thread{
//start receiving traffic from the server
session.start();
//clear the current local project list
this.projectSelectDialog.clearProjectList();
//request a new project list from the server
CoderMessage msg = new CoderMessage();
msg.ProjectListReq = new ProjectListReqMsg();
try {
session.send(msg);
} catch (IOException e) {
logger.log(Level.SEVERE, "Unable to send ProjectListReqMsg", e);
continue;
}
//show the project selector dialog
this.projectSelectDialog.setVisible(true); //blocking
if(this.projectSelectDialog.getAction() == ProjectSelectDialog.ProjectSelectDialogAction.CANCEL){
logger.fine("Project select dialog closed or canceled by the user. terminating.");
break;
}
String selectedProjectName = this.projectSelectDialog.getSelecteProjectName();
logger.info("Project \"" +selectedProjectName+ "\" was selected.");
//add a listener to forward messages from the editor GUI to the server
this.projectEditorWindow.addMessageSentListener(new GUIMessageSentListener(){
@Override
public void sendMessage(CoderMessage msg) {
try {
session.send(msg);
} catch (IOException e) {
logger.log(Level.SEVERE, "could not forward message from editor to the server", e);
}
}
});
//show the user the editor GUI
this.projectEditorWindow.setVisible(true);
try {Thread.sleep(1000);} catch (InterruptedException e) {}
this.selectProjectDialog.showModal(mainStage);
//wait here until the session is closed for some reason
while(session.isConnected()){
Thread.yield();
}
//hide the main GUI
logger.fine("The socket was closed.");
this.projectEditorWindow.setVisible(false);
mainStage.close();
Thread.yield();
}
logger.info("The program till now terminate");
closeCurrentSession();
this.projectEditorWindow.dispose();
// this.projectEditorWindow.dispose();
System.exit(0);
}
private void closeCurrentSession(){
if(this.session != null){
session.close();
session = null;
}
}
public void setPassword(char[] password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setRetryConnectForever(boolean tryForever) {
if(tryForever){
this.connectionRetriesLimit = 0;
}else{
this.connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES;
}
}
public static void main(String[] args){
int port = CoderServer.SERVER_PORT;
String username = null;
char[] password = null;
boolean reconnectForever = false;
//parse program arguments
for(int i = 0; i < args.length; ++i){
String arg = args[i];
if(arg.equals("-h") || arg.equals("--help")){
System.out.println("Usage: CoderClient [options]");
System.out.println("Options:");
System.out.println("-h|--help Print this help.");
System.out.println("-u|--username <username> Set the username to use when authenticating.");
System.out.println("-p|--password <password> Set the password used when authenticating. The password will not be safetly");
System.out.println(" stored and ths option should not be used in anything but debuging.");
System.out.println(" A password cannot be defined without a username. If booth");
System.out.println(" a username and a password has been defined by arguments the software will not.");
System.out.println(" prompt for this.");
System.out.println(" If not a port have been defined, the deafult port will be used.");
System.out.println("--retry-connect-forever Make the software never stop trying to reconnect to its peer.");
System.exit(0);
}else if(arg.equals("-u") || arg.equals("--username")){
i++;
if(i < args.length){
username = args[i];
}else{
System.err.println("missing username argument");
System.exit(1);
}
}else if(arg.equals("-p") || arg.equals("--password")){
i++;
if(i < args.length){
password = args[i].toCharArray();
}else{
System.err.println("missing password argument");
System.exit(1);
}
}else if(arg.equals("--retry-connect-forever")){
reconnectForever = true;
}
}
//create a client instance
CoderClient client = new CoderClient("127.0.0.1", port);
if(username != null){
client.setUsername(username);
if(password != null){
client.setPassword(password);
}
}
client.setRetryConnectForever(reconnectForever);
client.start();
}
}