From e1e4322a236df775cdf674688c28b70f581830fa Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Fri, 30 Nov 2018 11:14:45 -0800 Subject: [PATCH] corefx - cleaned up sessions dialog and added a delete session button --- .../java/com/core/client/ICoreClient.java | 4 + .../com/core/client/rest/CoreRestClient.java | 12 ++ .../com/core/ui/dialogs/SessionsDialog.java | 109 ++++++++++++++---- 3 files changed, 100 insertions(+), 25 deletions(-) diff --git a/corefx/src/main/java/com/core/client/ICoreClient.java b/corefx/src/main/java/com/core/client/ICoreClient.java index 283ed107..707e70c4 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -14,12 +14,16 @@ import java.util.Set; public interface ICoreClient { void setUrl(String url); + Integer currentSession(); + void updateSession(Integer sessionId); void updateState(SessionState state); SessionOverview createSession() throws IOException; + boolean deleteSession(Integer sessionId) throws IOException; + List getSessions() throws IOException; Session getSession(Integer sessionId) throws IOException; diff --git a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java index b79c690f..b6021696 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -23,6 +23,11 @@ public class CoreRestClient implements ICoreClient { this.baseUrl = url; } + @Override + public Integer currentSession() { + return sessionId; + } + @Override public void updateState(SessionState state) { sessionState = state; @@ -43,6 +48,13 @@ public class CoreRestClient implements ICoreClient { return WebUtils.post(url, SessionOverview.class); } + @Override + public boolean deleteSession(Integer sessionId) throws IOException { + String path = String.format("sessions/%s", sessionId); + String url = getUrl(path); + return WebUtils.delete(url); + } + public Map> getServices() throws IOException { String url = getUrl("services"); GetServices getServices = WebUtils.getJson(url, GetServices.class); diff --git a/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java b/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java index 528fa77b..22627f07 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java @@ -15,6 +15,8 @@ import org.apache.logging.log4j.Logger; import java.io.IOException; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Collectors; public class SessionsDialog extends StageDialog { @@ -23,47 +25,98 @@ public class SessionsDialog extends StageDialog { @FXML private TableColumn sessionIdColumn; @FXML private TableColumn stateColumn; @FXML private TableColumn nodeCountColumn; + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + private final JFXButton joinButton; + private final JFXButton deleteButton; public SessionsDialog(Controller controller) { super(controller, "/fxml/sessions_dialog.fxml"); - setTitle("Sessions"); + // add dialog buttons + addCreateButton(); + deleteButton = createDeleteButton(); + joinButton = createJoinButton(); + addCancelButton(); + + // update table cell factories + sessionIdColumn.setCellValueFactory(new PropertyValueFactory<>("id")); + stateColumn.setCellValueFactory(new PropertyValueFactory<>("state")); + nodeCountColumn.setCellValueFactory(new PropertyValueFactory<>("nodes")); + + // handle table row selection + sessionsTable.getSelectionModel().selectedItemProperty().addListener((ov, prev, current) -> { + if (current != null) { + boolean isCurrentSession = current.getId().equals(controller.getCoreClient().currentSession()); + deleteButton.setDisable(isCurrentSession); + joinButton.setDisable(isCurrentSession); + } else { + deleteButton.setDisable(true); + joinButton.setDisable(true); + } + }); + } + + private void addCreateButton() { JFXButton createButton = createButton("New"); createButton.setOnAction(event -> { - close(); - new Thread(() -> { + logger.info("creating new session"); + executorService.submit(() -> { try { SessionOverview sessionOverview = getCoreClient().createSession(); - controller.joinSession(sessionOverview.getId()); + getController().joinSession(sessionOverview.getId()); Toast.success(String.format("Created Session %s", sessionOverview.getId())); } catch (IOException ex) { Toast.error("Error creating new session", ex); } - }).start(); - }); - JFXButton joinButton = createButton("Join"); - joinButton.setOnAction(event -> { - SessionRow row = sessionsTable.getSelectionModel().getSelectedItem(); - logger.info("selected session: {}", row); - try { - controller.joinSession(row.getId()); - Toast.info(String.format("Joined Session %s", row.getId())); - } catch (IOException ex) { - Toast.error(String.format("Error joining session: %s", row.getId()), ex); - } - + }); close(); }); - joinButton.setDisable(true); - addCancelButton(); + } - sessionIdColumn.setCellValueFactory(new PropertyValueFactory<>("id")); - stateColumn.setCellValueFactory(new PropertyValueFactory<>("state")); - nodeCountColumn.setCellValueFactory(new PropertyValueFactory<>("nodes")); - sessionsTable.getSelectionModel().selectedItemProperty().addListener((ov, prev, current) -> { - joinButton.setDisable(current == null); + private JFXButton createJoinButton() { + JFXButton button = createButton("Join"); + button.setDisable(true); + button.setOnAction(event -> { + SessionRow row = sessionsTable.getSelectionModel().getSelectedItem(); + Integer sessionId = row.getId(); + logger.info("joining session: {}", sessionId); + executorService.submit(() -> { + try { + getController().joinSession(sessionId); + Toast.info(String.format("Joined Session %s", sessionId)); + } catch (IOException ex) { + Toast.error(String.format("Error joining session: %s", sessionId), ex); + } + }); + close(); }); + return button; + } + + private JFXButton createDeleteButton() { + JFXButton button = createButton("Delete"); + button.setDisable(true); + button.setOnAction(event -> { + SessionRow row = sessionsTable.getSelectionModel().getSelectedItem(); + Integer sessionId = row.getId(); + logger.info("deleting session: {}", sessionId); + executorService.submit(() -> { + try { + boolean result = getCoreClient().deleteSession(sessionId); + if (result) { + sessionsTable.getItems().remove(row); + sessionsTable.getSelectionModel().clearSelection(); + Toast.info(String.format("Deleted Session %s", sessionId)); + } else { + Toast.error(String.format("Failure to delete session %s", sessionId)); + } + } catch (IOException ex) { + Toast.error(String.format("Error deleting session: %s", sessionId), ex); + } + }); + }); + return button; } @Data @@ -72,7 +125,7 @@ public class SessionsDialog extends StageDialog { private String state; private Integer nodes; - public SessionRow(SessionOverview sessionOverview) { + SessionRow(SessionOverview sessionOverview) { id = sessionOverview.getId(); state = SessionState.get(sessionOverview.getState()).name(); nodes = sessionOverview.getNodes(); @@ -85,4 +138,10 @@ public class SessionsDialog extends StageDialog { sessionsTable.getItems().setAll(rows); show(); } + + @Override + public void close() { + sessionsTable.getSelectionModel().clearSelection(); + super.close(); + } }