corefx - cleaned up sessions dialog and added a delete session button

This commit is contained in:
Blake J. Harnden 2018-11-30 11:14:45 -08:00
parent 4675f3a836
commit e1e4322a23
3 changed files with 100 additions and 25 deletions

View file

@ -14,12 +14,16 @@ import java.util.Set;
public interface ICoreClient { public interface ICoreClient {
void setUrl(String url); void setUrl(String url);
Integer currentSession();
void updateSession(Integer sessionId); void updateSession(Integer sessionId);
void updateState(SessionState state); void updateState(SessionState state);
SessionOverview createSession() throws IOException; SessionOverview createSession() throws IOException;
boolean deleteSession(Integer sessionId) throws IOException;
List<SessionOverview> getSessions() throws IOException; List<SessionOverview> getSessions() throws IOException;
Session getSession(Integer sessionId) throws IOException; Session getSession(Integer sessionId) throws IOException;

View file

@ -23,6 +23,11 @@ public class CoreRestClient implements ICoreClient {
this.baseUrl = url; this.baseUrl = url;
} }
@Override
public Integer currentSession() {
return sessionId;
}
@Override @Override
public void updateState(SessionState state) { public void updateState(SessionState state) {
sessionState = state; sessionState = state;
@ -43,6 +48,13 @@ public class CoreRestClient implements ICoreClient {
return WebUtils.post(url, SessionOverview.class); 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<String, List<String>> getServices() throws IOException { public Map<String, List<String>> getServices() throws IOException {
String url = getUrl("services"); String url = getUrl("services");
GetServices getServices = WebUtils.getJson(url, GetServices.class); GetServices getServices = WebUtils.getJson(url, GetServices.class);

View file

@ -15,6 +15,8 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class SessionsDialog extends StageDialog { public class SessionsDialog extends StageDialog {
@ -23,47 +25,98 @@ public class SessionsDialog extends StageDialog {
@FXML private TableColumn<SessionRow, Integer> sessionIdColumn; @FXML private TableColumn<SessionRow, Integer> sessionIdColumn;
@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 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
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"); JFXButton createButton = createButton("New");
createButton.setOnAction(event -> { createButton.setOnAction(event -> {
close(); logger.info("creating new session");
new Thread(() -> { executorService.submit(() -> {
try { try {
SessionOverview sessionOverview = getCoreClient().createSession(); SessionOverview sessionOverview = getCoreClient().createSession();
controller.joinSession(sessionOverview.getId()); getController().joinSession(sessionOverview.getId());
Toast.success(String.format("Created Session %s", sessionOverview.getId())); Toast.success(String.format("Created Session %s", sessionOverview.getId()));
} catch (IOException ex) { } catch (IOException ex) {
Toast.error("Error creating new session", 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(); close();
}); });
joinButton.setDisable(true); }
addCancelButton();
sessionIdColumn.setCellValueFactory(new PropertyValueFactory<>("id")); private JFXButton createJoinButton() {
stateColumn.setCellValueFactory(new PropertyValueFactory<>("state")); JFXButton button = createButton("Join");
nodeCountColumn.setCellValueFactory(new PropertyValueFactory<>("nodes")); button.setDisable(true);
sessionsTable.getSelectionModel().selectedItemProperty().addListener((ov, prev, current) -> { button.setOnAction(event -> {
joinButton.setDisable(current == null); 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 @Data
@ -72,7 +125,7 @@ public class SessionsDialog extends StageDialog {
private String state; private String state;
private Integer nodes; private Integer nodes;
public SessionRow(SessionOverview sessionOverview) { SessionRow(SessionOverview sessionOverview) {
id = sessionOverview.getId(); id = sessionOverview.getId();
state = SessionState.get(sessionOverview.getState()).name(); state = SessionState.get(sessionOverview.getState()).name();
nodes = sessionOverview.getNodes(); nodes = sessionOverview.getNodes();
@ -85,4 +138,10 @@ public class SessionsDialog extends StageDialog {
sessionsTable.getItems().setAll(rows); sessionsTable.getItems().setAll(rows);
show(); show();
} }
@Override
public void close() {
sessionsTable.getSelectionModel().clearSelection();
super.close();
}
} }