corefx - updated terminal and session dialogs to leverage javafx tasks for running background work
This commit is contained in:
parent
3025287486
commit
e37caedf61
2 changed files with 96 additions and 66 deletions
|
@ -5,6 +5,7 @@ import com.core.data.SessionOverview;
|
||||||
import com.core.data.SessionState;
|
import com.core.data.SessionState;
|
||||||
import com.core.ui.Toast;
|
import com.core.ui.Toast;
|
||||||
import com.jfoenix.controls.JFXButton;
|
import com.jfoenix.controls.JFXButton;
|
||||||
|
import javafx.concurrent.Task;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.TableColumn;
|
import javafx.scene.control.TableColumn;
|
||||||
import javafx.scene.control.TableView;
|
import javafx.scene.control.TableView;
|
||||||
|
@ -26,17 +27,36 @@ public class SessionsDialog extends StageDialog {
|
||||||
@FXML private TableColumn<SessionRow, String> stateColumn;
|
@FXML private TableColumn<SessionRow, String> stateColumn;
|
||||||
@FXML private TableColumn<SessionRow, Integer> nodeCountColumn;
|
@FXML private TableColumn<SessionRow, Integer> nodeCountColumn;
|
||||||
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
private final JFXButton joinButton;
|
|
||||||
private final JFXButton deleteButton;
|
|
||||||
|
|
||||||
public SessionsDialog(Controller controller) {
|
public SessionsDialog(Controller controller) {
|
||||||
super(controller, "/fxml/sessions_dialog.fxml");
|
super(controller, "/fxml/sessions_dialog.fxml");
|
||||||
setTitle("Sessions");
|
setTitle("Sessions");
|
||||||
|
|
||||||
// add dialog buttons
|
// add dialog buttons
|
||||||
addCreateButton();
|
JFXButton createButton = createButton("New");
|
||||||
deleteButton = createDeleteButton();
|
createButton.setOnAction(event -> {
|
||||||
joinButton = createJoinButton();
|
logger.info("creating new session");
|
||||||
|
executorService.submit(new CreateSessionTask());
|
||||||
|
});
|
||||||
|
|
||||||
|
JFXButton deleteButton = createButton("Delete");
|
||||||
|
deleteButton.setDisable(true);
|
||||||
|
deleteButton.setOnAction(event -> {
|
||||||
|
SessionRow row = sessionsTable.getSelectionModel().getSelectedItem();
|
||||||
|
Integer sessionId = row.getId();
|
||||||
|
logger.info("deleting session: {}", sessionId);
|
||||||
|
executorService.submit(new DeleteSessionTask(row, sessionId));
|
||||||
|
});
|
||||||
|
|
||||||
|
JFXButton joinButton = createButton("Join");
|
||||||
|
joinButton.setDisable(true);
|
||||||
|
joinButton.setOnAction(event -> {
|
||||||
|
SessionRow row = sessionsTable.getSelectionModel().getSelectedItem();
|
||||||
|
Integer sessionId = row.getId();
|
||||||
|
logger.info("joining session: {}", sessionId);
|
||||||
|
executorService.submit(new JoinSessionTask(sessionId));
|
||||||
|
});
|
||||||
|
|
||||||
addCancelButton();
|
addCancelButton();
|
||||||
|
|
||||||
// update table cell factories
|
// update table cell factories
|
||||||
|
@ -57,66 +77,81 @@ public class SessionsDialog extends StageDialog {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCreateButton() {
|
private class CreateSessionTask extends Task<Integer> {
|
||||||
JFXButton createButton = createButton("New");
|
@Override
|
||||||
createButton.setOnAction(event -> {
|
protected Integer call() throws Exception {
|
||||||
logger.info("creating new session");
|
|
||||||
executorService.submit(() -> {
|
|
||||||
try {
|
|
||||||
SessionOverview sessionOverview = getCoreClient().createSession();
|
SessionOverview sessionOverview = getCoreClient().createSession();
|
||||||
getController().joinSession(sessionOverview.getId());
|
Integer sessionId = sessionOverview.getId();
|
||||||
Toast.success(String.format("Created Session %s", sessionOverview.getId()));
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Toast.error("Error creating new session", ex);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
close();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
getController().joinSession(sessionId);
|
||||||
Toast.info(String.format("Joined Session %s", sessionId));
|
return sessionId;
|
||||||
} catch (IOException ex) {
|
|
||||||
Toast.error(String.format("Error joining session: %s", sessionId), ex);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
close();
|
|
||||||
});
|
|
||||||
return button;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JFXButton createDeleteButton() {
|
@Override
|
||||||
JFXButton button = createButton("Delete");
|
protected void succeeded() {
|
||||||
button.setDisable(true);
|
Toast.success(String.format("Created Session %s", getValue()));
|
||||||
button.setOnAction(event -> {
|
close();
|
||||||
SessionRow row = sessionsTable.getSelectionModel().getSelectedItem();
|
}
|
||||||
Integer sessionId = row.getId();
|
|
||||||
logger.info("deleting session: {}", sessionId);
|
@Override
|
||||||
executorService.submit(() -> {
|
protected void failed() {
|
||||||
try {
|
Toast.error("Error creating new session", new RuntimeException(getException()));
|
||||||
boolean result = getCoreClient().deleteSession(sessionId);
|
}
|
||||||
if (result) {
|
}
|
||||||
|
|
||||||
|
private class JoinSessionTask extends Task<Void> {
|
||||||
|
private Integer sessionId;
|
||||||
|
|
||||||
|
JoinSessionTask(Integer sessionId) {
|
||||||
|
this.sessionId = sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void call() throws Exception {
|
||||||
|
getController().joinSession(sessionId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void succeeded() {
|
||||||
|
Toast.info(String.format("Joined Session %s", sessionId));
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void failed() {
|
||||||
|
Toast.error(String.format("Error joining session: %s", sessionId), new RuntimeException(getException()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DeleteSessionTask extends Task<Boolean> {
|
||||||
|
private SessionRow row;
|
||||||
|
private Integer sessionId;
|
||||||
|
|
||||||
|
DeleteSessionTask(SessionRow row, Integer sessionId) {
|
||||||
|
this.row = row;
|
||||||
|
this.sessionId = sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Boolean call() throws Exception {
|
||||||
|
return getCoreClient().deleteSession(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void succeeded() {
|
||||||
|
if (getValue()) {
|
||||||
sessionsTable.getItems().remove(row);
|
sessionsTable.getItems().remove(row);
|
||||||
sessionsTable.getSelectionModel().clearSelection();
|
sessionsTable.getSelectionModel().clearSelection();
|
||||||
Toast.info(String.format("Deleted Session %s", sessionId));
|
Toast.info(String.format("Deleted Session %s", sessionId));
|
||||||
} else {
|
} else {
|
||||||
Toast.error(String.format("Failure to delete session %s", sessionId));
|
Toast.error(String.format("Failure to delete session %s", sessionId));
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
|
||||||
Toast.error(String.format("Error deleting session: %s", sessionId), ex);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
@Override
|
||||||
return button;
|
protected void failed() {
|
||||||
|
Toast.error(String.format("Error deleting session: %s", sessionId), new RuntimeException(getException()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@ -135,13 +170,8 @@ public class SessionsDialog extends StageDialog {
|
||||||
public void showDialog() throws IOException {
|
public void showDialog() throws IOException {
|
||||||
List<SessionOverview> sessions = getCoreClient().getSessions();
|
List<SessionOverview> sessions = getCoreClient().getSessions();
|
||||||
List<SessionRow> rows = sessions.stream().map(SessionRow::new).collect(Collectors.toList());
|
List<SessionRow> rows = sessions.stream().map(SessionRow::new).collect(Collectors.toList());
|
||||||
|
sessionsTable.getSelectionModel().clearSelection();
|
||||||
sessionsTable.getItems().setAll(rows);
|
sessionsTable.getItems().setAll(rows);
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
sessionsTable.getSelectionModel().clearSelection();
|
|
||||||
super.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class TerminalDialog extends StageDialog {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void failed() {
|
protected void failed() {
|
||||||
Toast.error("Failed sending terminal command");
|
Toast.error("Failed sending terminal command", new RuntimeException(getException()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue