Refactoring: moving more GUI logic to each controller class
-Adding a handler for SSDP -CoderClient giving control to each controller to be able to show itself on the main stage
This commit is contained in:
parent
02efc6c22f
commit
09d15b4a80
12 changed files with 244 additions and 243 deletions
|
|
@ -5,45 +5,34 @@ import java.util.Map;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.coder.client.gui.GuiWindow;
|
||||
import com.coder.client.gui.editor.EditorWindow;
|
||||
import com.coder.client.gui.login.LoginDialog;
|
||||
import com.coder.client.gui.newProject.NewProjectDialog;
|
||||
import com.coder.client.gui.selectProject.SelectProjectDialog;
|
||||
import com.coder.client.gui.selectServer.SelectServerDialog;
|
||||
import com.coder.client.project.ProjectHandler;
|
||||
import com.coder.client.project.ProjectListener;
|
||||
import com.coder.client.session.ProjectRspMsgListener;
|
||||
import com.coder.client.session.SessionHandler;
|
||||
import com.coder.client.session.SessionListener;
|
||||
import com.coder.server.message.CoderMessage;
|
||||
import com.coder.server.message.ProjectCreateReqMsg;
|
||||
import com.coder.server.message.ProjectReqMsg;
|
||||
import com.coder.server.message.ProjectRspMsg;
|
||||
|
||||
import zutil.log.CompactLogFormatter;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.ssdp.SSDPClient;
|
||||
import zutil.net.ssdp.SSDPClient.SSDPServiceListener;
|
||||
import zutil.net.ssdp.StandardSSDPInfo;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class CoderClient extends Application{
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
private SessionHandler sessionHandler = new SessionHandler();
|
||||
private ProjectHandler projectHandler = new ProjectHandler();
|
||||
private SessionHandler sessionHandler;
|
||||
private ProjectHandler projectHandler;
|
||||
private SSDPHandler ssdpHandler;
|
||||
|
||||
//GUI elements
|
||||
private Stage mainStage;
|
||||
private EditorWindow editorWindow;
|
||||
private LoginDialog loginDialog;
|
||||
private SelectProjectDialog selectProjectDialog;
|
||||
private SelectServerDialog selectServerDialog;
|
||||
private NewProjectDialog newProjectDialog;
|
||||
|
||||
//services
|
||||
SSDPClient ssdpClient;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Application.launch(args);
|
||||
}
|
||||
|
|
@ -59,7 +48,18 @@ public class CoderClient extends Application{
|
|||
LogUtil.setFormatter("zutil", formatter);
|
||||
LogUtil.setGlobalFormatter(formatter);
|
||||
|
||||
//setup handlers
|
||||
try{
|
||||
this.sessionHandler = new SessionHandler();
|
||||
this.projectHandler = new ProjectHandler();
|
||||
this.ssdpHandler = new SSDPHandler();
|
||||
}catch(Exception e){
|
||||
logger.log(Level.SEVERE, "could not load all handlers", e);
|
||||
exit();
|
||||
}
|
||||
|
||||
//setup GUI elements
|
||||
this.mainStage = mainStage;
|
||||
mainStage.setTitle("CoderClient");
|
||||
try{
|
||||
this.selectServerDialog = new SelectServerDialog(this);
|
||||
|
|
@ -69,7 +69,7 @@ public class CoderClient extends Application{
|
|||
this.editorWindow = new EditorWindow(this);
|
||||
}catch(IOException e){
|
||||
logger.log(Level.SEVERE, "could not load all GUI elements", e);
|
||||
System.exit(1);
|
||||
exit();
|
||||
}
|
||||
|
||||
//parse program arguments
|
||||
|
|
@ -92,170 +92,25 @@ public class CoderClient extends Application{
|
|||
}
|
||||
}
|
||||
|
||||
//setup SSDP client
|
||||
try{
|
||||
setupSSDPClient();
|
||||
}catch(IOException e){
|
||||
logger.log(Level.SEVERE, "could not setup SSDP client", e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
//add session listeners
|
||||
sessionHandler.addSessionListener(new SessionListener() {
|
||||
@Override
|
||||
public void sessionDisconnected(boolean retryToEstablishConnection) {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session disconnected, will show the select server dialog on the main stage");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void sessionConnectionSuccess() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session connection successfull, will show the login dialog on the main stage");
|
||||
loginDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void sessionConnectionFailure() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session connection failure, will show the select server dialog on the main stage");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void sessionAuthenticationSuccess() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session authentication successfull, will show the select project dialog on the main stage");
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void sessionAuthenticationFailure() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session authentication failure, will show the select server dialog on the main stage");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void sessionAuthenticationCancel() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("session authentication canceled, will show the select server dialog on the main stage");
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//add project listeners
|
||||
projectHandler.addprojectListener(new ProjectListener() {
|
||||
@Override
|
||||
public void selectProject() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("select project requested, will show the select project dialog on the main stage");
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void openProject(String projectName) {
|
||||
CoderMessage msg = new CoderMessage();
|
||||
msg.ProjectReq = new ProjectReqMsg();
|
||||
msg.ProjectReq.name = projectName;
|
||||
sessionHandler.sendMessage(msg);
|
||||
}
|
||||
@Override
|
||||
public void createNewProject(String name, String type, String description) {
|
||||
CoderMessage msg = new CoderMessage();
|
||||
msg.ProjectCreateReq = new ProjectCreateReqMsg();
|
||||
msg.ProjectCreateReq.name = name;
|
||||
msg.ProjectCreateReq.type = type;
|
||||
msg.ProjectCreateReq.description = description;
|
||||
sessionHandler.sendMessage(msg);
|
||||
}
|
||||
@Override
|
||||
public void createNewProject() {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("new project requested, will show the new project dialog on the main stage");
|
||||
newProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void openProjectFailed(String errorMsg) {
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("open project failed, will show the select project dialog on the main stage");
|
||||
selectProjectDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//add message listeners
|
||||
sessionHandler.addMessageListener(new ProjectRspMsgListener() {
|
||||
@Override
|
||||
public void messageReceived(final ProjectRspMsg msg) {
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(msg.error != null){
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}else{
|
||||
editorWindow.showOnStage(mainStage);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//start program logic
|
||||
selectServerDialog.setServerPort(-1);
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
showOnStage(selectServerDialog);
|
||||
|
||||
}
|
||||
|
||||
private void setupSSDPClient() throws IOException{
|
||||
this.ssdpClient = new SSDPClient();
|
||||
ssdpClient.setListener(new SSDPServiceListener() {
|
||||
|
||||
public void showOnStage(final GuiWindow gui){
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void newService(final StandardSSDPInfo service) {
|
||||
if(selectServerDialog != null){
|
||||
Platform.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String ip = service.getInetAddress().getHostAddress();
|
||||
selectServerDialog.addServerToList(ip, 1337);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
logger.warning("New Service found ("+service.getLocation()+") when stage closed.");
|
||||
}
|
||||
public void run() {
|
||||
Scene scene = gui.getScene();
|
||||
mainStage.setScene(scene);
|
||||
mainStage.setTitle(gui.getTitle());
|
||||
logger.fine("about to show " + gui.getDescriptiveName() + " on stage");
|
||||
gui.willShow();
|
||||
mainStage.show();
|
||||
logger.fine("showing " + gui.getDescriptiveName());
|
||||
}
|
||||
});
|
||||
ssdpClient.start();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
|
|
@ -263,10 +118,6 @@ public class CoderClient extends Application{
|
|||
Platform.exit();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public SSDPClient getSSDPClient() {
|
||||
return this.ssdpClient;
|
||||
}
|
||||
|
||||
|
||||
public SessionHandler getSessionHandler() {
|
||||
|
|
@ -276,5 +127,9 @@ public class CoderClient extends Application{
|
|||
public ProjectHandler getProjectHandler(){
|
||||
return this.projectHandler;
|
||||
}
|
||||
|
||||
public SSDPHandler getSSDPHandler(){
|
||||
return this.ssdpHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue