Moving state variables from CoderClient to each GUI window.
This commit is contained in:
parent
6db9f94014
commit
eb71b4da59
6 changed files with 107 additions and 112 deletions
|
|
@ -1,9 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry excluding="com/coder/client/gui/GUIManager.java|com/coder/client/CoderClient2.java|com/coder/client/LoginDialog.java|com/coder/client/ProjectEditorWindow.java|com/coder/client/ProjectFileTreeModel.java|com/coder/client/LoginCridentials.java|com/coder/client/ProjectSelectDialog.java|com/coder/client/gui/GUI_TEST_MAIN.java" kind="src" path="src"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/ZUtil"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/CoderServer"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-8-openjdk-i386"/>
|
||||
<classpathentry kind="lib" path="lib/controlsfx-8.20.9.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/Zutil-java"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
|||
9
.project
9
.project
|
|
@ -10,15 +10,6 @@
|
|||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
|
||||
<arguments>
|
||||
<dictionary>
|
||||
<key>LaunchConfigHandle</key>
|
||||
<value><project>/.externalToolBuilders/ANT Build.launch</value>
|
||||
</dictionary>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.coder.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
|
@ -38,63 +39,34 @@ import javafx.stage.Stage;
|
|||
|
||||
public class CoderClient extends Application{
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
|
||||
private Session session;
|
||||
private Stage mainStage;
|
||||
|
||||
|
||||
//GUI elements
|
||||
private EditorWindow editorWindow;
|
||||
private LoginDialog loginDialog;
|
||||
private SelectProjectDialog selectProjectDialog;
|
||||
private SelectServerDialog selectServerDialog;
|
||||
private NewProjectDialog newProjectDialog;
|
||||
|
||||
|
||||
//state variables
|
||||
private String serverURL = null;
|
||||
private int serverPort = CoderServer.SERVER_PORT;
|
||||
private String username = null;
|
||||
private char[] password = null; //should only be anything else than null if defined by program argument
|
||||
private String project = null;
|
||||
private GuiWindow projectSelectionWindow = null; //points to the GUI that selected a project for the editor. If any error occurs while creating/loading the project, this is the window we will go back to.
|
||||
|
||||
|
||||
//services
|
||||
SSDPClient ssdpClient;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Application.launch(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();
|
||||
for(String key : params.keySet()){
|
||||
String value = params.get(key);
|
||||
if(key.equals("username")){
|
||||
this.username = value;
|
||||
}else if(key.equals("password")){
|
||||
this.password = value.toCharArray();
|
||||
}else if(key.equals("url")){
|
||||
this.serverURL = value;
|
||||
}else if(key.equals("port")){
|
||||
try{
|
||||
this.serverPort = Integer.parseInt(value);
|
||||
}catch(NumberFormatException e){
|
||||
logger.warning("port argument to program is not of a integer type. using default port.");
|
||||
}
|
||||
}else if(key.equals("project")){
|
||||
this.project = value;
|
||||
}
|
||||
}
|
||||
if(this.username == null){ //ignore the password if no username was set
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
||||
|
||||
//setup GUI elements
|
||||
this.mainStage = mainStage;
|
||||
mainStage.setTitle("CoderClient");
|
||||
|
|
@ -108,7 +80,29 @@ public class CoderClient extends Application{
|
|||
logger.log(Level.SEVERE, "could not load all GUI elements", e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
//parse program arguments
|
||||
Map<String, String> params = this.getParameters().getNamed();
|
||||
for(String key : params.keySet()){
|
||||
String value = params.get(key);
|
||||
if(key.equals("username")){
|
||||
loginDialog.setUsername(value);
|
||||
}else if(key.equals("password")){
|
||||
loginDialog.setPassword(value.toCharArray());
|
||||
}else if(key.equals("url")){
|
||||
selectServerDialog.setServerAddress(value);
|
||||
}else if(key.equals("port")){
|
||||
try{
|
||||
selectServerDialog.setServerPort(Integer.parseInt(value));
|
||||
}catch(NumberFormatException e){
|
||||
logger.warning("port argument to program is not of a integer type. using default port.");
|
||||
selectServerDialog.setServerPort(CoderServer.SERVER_PORT);
|
||||
}
|
||||
}else if(key.equals("project")){
|
||||
selectProjectDialog.setProject(value);
|
||||
}
|
||||
}
|
||||
|
||||
//setup SSDP client
|
||||
try{
|
||||
setupSSDPClient();
|
||||
|
|
@ -116,12 +110,13 @@ public class CoderClient extends Application{
|
|||
logger.log(Level.SEVERE, "could not setup SSDP client", e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
//start program logic
|
||||
selectServerDialog.setServerPort(CoderServer.SERVER_PORT);
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void setupSelectServerDialog() throws IOException{
|
||||
this.selectServerDialog = new SelectServerDialog();
|
||||
this.selectServerDialog.addSelectProjectDialogListener(new SelectServerDialogListener() {
|
||||
|
|
@ -135,8 +130,6 @@ public class CoderClient extends Application{
|
|||
}else{
|
||||
logger.severe("could not send a SSDP request since the client is not setup");
|
||||
}
|
||||
selectServerDialog.setServerAddress(serverURL);
|
||||
selectServerDialog.setServerPort(serverPort);
|
||||
}
|
||||
@Override
|
||||
public void exit() {
|
||||
|
|
@ -145,15 +138,13 @@ public class CoderClient extends Application{
|
|||
}
|
||||
@Override
|
||||
public void connect(String address, int port) {
|
||||
serverURL = address;
|
||||
serverPort = port;
|
||||
//connect session
|
||||
session = Session.setupConnection(serverURL, serverPort);
|
||||
session = Session.setupConnection(address, port);
|
||||
if(session == null){
|
||||
logger.warning("Could not setup a connection to " + serverURL + ":" + port);
|
||||
serverURL = null;
|
||||
serverPort = CoderServer.SERVER_PORT;
|
||||
logger.warning("Could not setup a connection to " + address + ":" + port);
|
||||
selectServerDialog.setServerAddress(null);
|
||||
selectServerDialog.setErrorMessage("ERROR: Unable to connect to remote host");
|
||||
loginDialog.setErrorMessage("");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}else{
|
||||
selectServerDialog.setErrorMessage("");
|
||||
|
|
@ -166,11 +157,12 @@ public class CoderClient extends Application{
|
|||
if(session == null || !session.isConnected()){
|
||||
logger.fine("session guard: no connection");
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@Override
|
||||
public void run() {
|
||||
selectServerDialog.setErrorMessage("The current session was disconnected");
|
||||
closeCurrentSession();
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
|
@ -183,30 +175,26 @@ public class CoderClient extends Application{
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void setupLoginDialog() throws IOException {
|
||||
this.loginDialog = new LoginDialog();
|
||||
this.loginDialog.addLoginDialogListener(new LoginDialogListener(){
|
||||
@Override
|
||||
public void willShow() {
|
||||
loginDialog.setUsername(username);
|
||||
loginDialog.setPassword(password);
|
||||
|
||||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
serverURL = null;
|
||||
serverPort = CoderServer.SERVER_PORT;
|
||||
selectServerDialog.setServerAddress(null);
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
public void login(String uname, char[] paswd) {
|
||||
username = uname;
|
||||
password = paswd;
|
||||
public void login(String username, char[] password) {
|
||||
//authenticate session
|
||||
boolean authenticated = session.authenticate(username, password);
|
||||
if(!authenticated){
|
||||
logger.severe("Authentication failed: wrong username or password");
|
||||
password = null;
|
||||
loginDialog.setPassword(null);
|
||||
loginDialog.setErrorMessage("Wrong username or password");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
return;
|
||||
|
|
@ -219,7 +207,7 @@ public class CoderClient extends Application{
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* To be called after a session has been connected and authenticated
|
||||
*/
|
||||
|
|
@ -237,84 +225,86 @@ public class CoderClient extends Application{
|
|||
public void projectListRspReceived(final Map<String, ProjectListData> projectListRsp) {
|
||||
logger.fine("a ProjectListRsp received");
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for(String projectName : projectListRsp.keySet()){
|
||||
@Override
|
||||
public void run() {
|
||||
for(String projectName : projectListRsp.keySet()){
|
||||
ProjectListData projectData = projectListRsp.get(projectName);
|
||||
selectProjectDialog.addProjectToList(projectName, projectData);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void projectRspReceived(final ProjectRspMsg projectRspMsg) {
|
||||
logger.fine("a ProjectRspMsg received");
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(projectRspMsg.error != null){
|
||||
@Override
|
||||
public void run() {
|
||||
if(projectRspMsg.error != null){
|
||||
logger.severe("Server responded on the project request with the following error message: " + projectRspMsg.error);
|
||||
project = null;
|
||||
selectProjectDialog.setProject(null);
|
||||
if(projectSelectionWindow != null){
|
||||
projectSelectionWindow.setErrorMessage("ERROR: " + projectRspMsg.error);
|
||||
projectSelectionWindow.showOnStage(mainStage);
|
||||
return;
|
||||
}else{
|
||||
logger.severe("Undefined error message handler. Unrecoverable state. Terminating");
|
||||
Platform.exit();
|
||||
selectProjectDialog.setErrorMessage("ERROR: " + projectRspMsg.error);
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
if(projectSelectionWindow != null){
|
||||
projectSelectionWindow.setErrorMessage("");
|
||||
}
|
||||
ConfigData projectConfig = projectRspMsg.config;
|
||||
String projectDescription = projectRspMsg.description;
|
||||
List<String> projectFileList = projectRspMsg.fileList;
|
||||
String projectName = projectRspMsg.name;
|
||||
String projectType = projectRspMsg.type;
|
||||
//TODO: handle msg
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void projectTypeRspReceived(final Map<String, ConfigData> projectTypeRsp) {
|
||||
logger.fine("a ProjectTypeRspMsg received");
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for(String typeName : projectTypeRsp.keySet()){
|
||||
ConfigData typeData = projectTypeRsp.get(typeName);
|
||||
newProjectDialog.addProjectTypeToList(typeName, typeData);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
for(String typeName : projectTypeRsp.keySet()){
|
||||
ConfigData typeData = projectTypeRsp.get(typeName);
|
||||
newProjectDialog.addProjectTypeToList(typeName, typeData);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void setupSelectProjectDialog() throws IOException {
|
||||
this.selectProjectDialog = new SelectProjectDialog();
|
||||
this.selectProjectDialog.addSelectProjectDialogListener(new SelectProjectDialogListener() {
|
||||
@Override
|
||||
public void willShow() {
|
||||
selectProjectDialog.clearProjectList();
|
||||
if(project == null || project.isEmpty()){
|
||||
if(!selectProjectDialog.isProjectSelected()){
|
||||
sendProjectListReq();
|
||||
}
|
||||
selectProjectDialog.setProject(project);
|
||||
}
|
||||
@Override
|
||||
public void open(String selectedProjectName) {
|
||||
projectSelectionWindow = selectProjectDialog;
|
||||
project = selectedProjectName;
|
||||
editorWindow.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
public void newProject() {
|
||||
project = null;
|
||||
selectProjectDialog.setProject(null);
|
||||
newProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
serverURL = null;
|
||||
serverPort = CoderServer.SERVER_PORT;
|
||||
selectServerDialog.setServerAddress(null);
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
|
|
@ -335,7 +325,7 @@ public class CoderClient extends Application{
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void setupNewProjectDialog() throws IOException {
|
||||
this.newProjectDialog = new NewProjectDialog();
|
||||
this.newProjectDialog.addNewProjectDialogListener(new NewProjectDialogListener(){
|
||||
|
|
@ -348,7 +338,7 @@ public class CoderClient extends Application{
|
|||
public void create(String newProjectName, String projectType) {
|
||||
projectSelectionWindow = newProjectDialog;
|
||||
sendProjectCreateReqMsg(newProjectName, projectType);
|
||||
project = newProjectName;
|
||||
selectProjectDialog.setProject(newProjectName);
|
||||
editorWindow.showOnStage(mainStage);
|
||||
}
|
||||
private void sendProjectTypeReqMsg(){
|
||||
|
|
@ -375,12 +365,12 @@ public class CoderClient extends Application{
|
|||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
project = null;
|
||||
selectProjectDialog.setProject(null);
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void setupEditWindow() throws IOException {
|
||||
this.editorWindow = new EditorWindow();
|
||||
this.editorWindow.addEditorWindowListener(new EditorWindowListener() {
|
||||
|
|
@ -397,9 +387,13 @@ public class CoderClient extends Application{
|
|||
//TODO
|
||||
}
|
||||
private void sendProjectReqMsg(){
|
||||
if(!selectProjectDialog.isProjectSelected()){
|
||||
logger.severe("Cannot send a project request when no project is selected");
|
||||
return;
|
||||
}
|
||||
CoderMessage msg = new CoderMessage();
|
||||
msg.ProjectReq = new ProjectReqMsg();
|
||||
msg.ProjectReq.name = project;
|
||||
msg.ProjectReq.name = selectProjectDialog.getSelectedProject();
|
||||
try {
|
||||
session.send(msg);
|
||||
} catch (IOException e) {
|
||||
|
|
@ -409,12 +403,12 @@ public class CoderClient extends Application{
|
|||
}
|
||||
@Override
|
||||
public void changeProject() {
|
||||
project = null;
|
||||
selectProjectDialog.setProject(null);
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void setupSSDPClient() throws IOException{
|
||||
this.ssdpClient = new SSDPClient();
|
||||
ssdpClient.setListener(new SSDPServiceListener() {
|
||||
|
|
@ -422,18 +416,18 @@ public class CoderClient extends Application{
|
|||
public void newService(final StandardSSDPInfo service) {
|
||||
if(selectServerDialog != null){
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@Override
|
||||
public void run() {
|
||||
String ip = service.getInetAddress().getHostAddress();
|
||||
selectServerDialog.addServerToList(ip);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
ssdpClient.start();
|
||||
}
|
||||
|
||||
|
||||
private void closeCurrentSession(){
|
||||
if(this.session != null){
|
||||
logger.info("disconnecting from server");
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ public class LoginDialog extends GuiWindow {
|
|||
passwordPasswordField.requestFocus();
|
||||
}else{
|
||||
usernameTextField.requestFocus();
|
||||
passwordPasswordField.setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ public class NewProjectDialog extends GuiWindow {
|
|||
for(NewProjectDialogListener listener : this.listeners){
|
||||
listener.willShow();
|
||||
}
|
||||
//TODO: if(errorMessage == null || errorMessage.isEmpty()){project = ""; type = "";} //keep field data if there is an error
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public class SelectProjectDialog extends GuiWindow {
|
|||
for(SelectProjectDialogListener listener : this.listeners){
|
||||
listener.willShow();
|
||||
}
|
||||
if(this.projectName != null){
|
||||
if(this.projectName != null && !this.projectName.isEmpty()){
|
||||
openButton.setDisable(false);
|
||||
openButton.fire();
|
||||
}
|
||||
|
|
@ -134,7 +134,7 @@ public class SelectProjectDialog extends GuiWindow {
|
|||
|
||||
@FXML
|
||||
protected void open(ActionEvent event){
|
||||
if(this.projectName == null){
|
||||
if(this.projectName == null || this.projectName.isEmpty()){
|
||||
ProjectListItem selectedItem = projectListView.getSelectionModel().getSelectedItem();
|
||||
projectName = selectedItem.getName();
|
||||
}
|
||||
|
|
@ -173,5 +173,13 @@ public class SelectProjectDialog extends GuiWindow {
|
|||
public void setErrorMessage(String errorMsg){
|
||||
this.errorLabel.setText(errorMsg);
|
||||
}
|
||||
|
||||
public boolean isProjectSelected() {
|
||||
return this.projectName != null && !this.projectName.isEmpty();
|
||||
}
|
||||
|
||||
public String getSelectedProject() {
|
||||
return this.projectName;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue