Clean tab-text reflection logic and add ErrorLayer maintenance while switching tabs

This commit is contained in:
Rohit Awate 2018-07-21 17:14:42 +05:30
parent 772ac377bd
commit df1f61a5d8
3 changed files with 106 additions and 73 deletions

View file

@ -104,8 +104,9 @@ public class DashboardController implements Initializable {
private DELETERequest deleteRequest; private DELETERequest deleteRequest;
private HashMap<String, String> params; private HashMap<String, String> params;
private EverestCodeArea responseArea; private EverestCodeArea responseArea;
private ResponseLayer visibleLayer;
private enum ResponseLayer { public enum ResponseLayer {
PROMPT, LOADING, RESPONSE, ERROR PROMPT, LOADING, RESPONSE, ERROR
} }
@ -131,7 +132,7 @@ public class DashboardController implements Initializable {
snackbar = new JFXSnackbar(dashboard); snackbar = new JFXSnackbar(dashboard);
displayLayer(ResponseLayer.PROMPT); showLayer(ResponseLayer.PROMPT);
httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.getItems().addAll(httpMethods);
// Select GET by default // Select GET by default
@ -209,7 +210,6 @@ public class DashboardController implements Initializable {
return; return;
} }
promptLayer.setVisible(false);
if (responseBox.getChildren().size() == 2) { if (responseBox.getChildren().size() == 2) {
responseBox.getChildren().remove(0); responseBox.getChildren().remove(0);
responseArea.clear(); responseArea.clear();
@ -219,7 +219,7 @@ public class DashboardController implements Initializable {
String address = addressField.getText().trim(); String address = addressField.getText().trim();
if (address.equals("")) { if (address.equals("")) {
promptLayer.setVisible(true); showLayer(ResponseLayer.PROMPT);
snackbar.show("Please enter an address.", 3000); snackbar.show("Please enter an address.", 3000);
return; return;
} }
@ -312,15 +312,15 @@ public class DashboardController implements Initializable {
requestManager.start(); requestManager.start();
break; break;
default: default:
loadingLayer.setVisible(false); showLayer(ResponseLayer.PROMPT);
} }
Services.historyManager.saveHistory(getState().composer); Services.historyManager.saveHistory(getState().composer);
} catch (MalformedURLException MURLE) { } catch (MalformedURLException MURLE) {
promptLayer.setVisible(true); showLayer(ResponseLayer.PROMPT);
snackbar.show("Invalid address. Please verify and try again.", 3000); snackbar.show("Invalid address. Please verify and try again.", 3000);
} catch (Exception E) { } catch (Exception E) {
Services.loggingService.logSevere("Request execution failed.", E, LocalDateTime.now()); Services.loggingService.logSevere("Request execution failed.", E, LocalDateTime.now());
errorLayer.setVisible(true); showLayer(ResponseLayer.ERROR);
errorTitle.setText("Oops... That's embarrassing!"); errorTitle.setText("Oops... That's embarrassing!");
errorDetails.setText("Something went wrong. Try to make another request.\nRestart Everest if that doesn't work."); errorDetails.setText("Something went wrong. Try to make another request.\nRestart Everest if that doesn't work.");
} }
@ -335,8 +335,7 @@ public class DashboardController implements Initializable {
} }
private void onFailed() { private void onFailed() {
loadingLayer.setVisible(false); showLayer(ResponseLayer.ERROR);
promptLayer.setVisible(false);
Throwable throwable = requestManager.getException(); Throwable throwable = requestManager.getException();
Exception exception = (Exception) throwable; Exception exception = (Exception) throwable;
Services.loggingService.logWarning(httpMethodBox.getValue() + " request could not be processed.", exception, LocalDateTime.now()); Services.loggingService.logWarning(httpMethodBox.getValue() + " request could not be processed.", exception, LocalDateTime.now());
@ -367,28 +366,29 @@ public class DashboardController implements Initializable {
} }
} }
errorLayer.setVisible(true);
requestManager.reset(); requestManager.reset();
} }
private void onCancelled() { private void onCancelled() {
displayLayer(ResponseLayer.PROMPT); showLayer(ResponseLayer.PROMPT);
snackbar.show("Request canceled.", 2000); snackbar.show("Request canceled.", 2000);
requestManager.reset(); requestManager.reset();
} }
private void onSucceeded() { private void onSucceeded() {
displayResponse(requestManager.getValue()); showResponse(requestManager.getValue());
displayLayer(ResponseLayer.RESPONSE); showLayer(ResponseLayer.RESPONSE);
requestManager.reset(); requestManager.reset();
} }
private void whileRunning() { private void whileRunning() {
responseArea.clear(); responseArea.clear();
displayLayer(ResponseLayer.LOADING); showLayer(ResponseLayer.LOADING);
} }
private void displayLayer(ResponseLayer layer) { private void showLayer(ResponseLayer layer) {
this.visibleLayer = layer;
switch (layer) { switch (layer) {
case ERROR: case ERROR:
errorLayer.setVisible(true); errorLayer.setVisible(true);
@ -418,7 +418,7 @@ public class DashboardController implements Initializable {
} }
} }
private void displayResponse(EverestResponse response) { private void showResponse(EverestResponse response) {
prettifyResponseBody(response); prettifyResponseBody(response);
statusCode.setText(Integer.toString(response.getStatusCode())); statusCode.setText(Integer.toString(response.getStatusCode()));
statusCodeDescription.setText(Response.Status.fromStatusCode(response.getStatusCode()).getReasonPhrase()); statusCodeDescription.setText(Response.Status.fromStatusCode(response.getStatusCode()).getReasonPhrase());
@ -427,17 +427,17 @@ public class DashboardController implements Initializable {
responseHeadersViewer.populate(response); responseHeadersViewer.populate(response);
} }
private void displayResponse(DashboardState state) { private void showResponse(DashboardState state) {
prettifyResponseBody(state.responseBody, state.responseType); prettifyResponseBody(state.responseBody, state.responseType);
statusCode.setText(Integer.toString(state.statusCode)); statusCode.setText(Integer.toString(state.responseCode));
statusCodeDescription.setText(Response.Status.fromStatusCode(state.statusCode).getReasonPhrase()); statusCodeDescription.setText(Response.Status.fromStatusCode(state.responseCode).getReasonPhrase());
responseTime.setText(Long.toString(state.responseTime) + " ms"); responseTime.setText(Long.toString(state.responseTime) + " ms");
responseSize.setText(Integer.toString(state.responseSize) + " B"); responseSize.setText(Integer.toString(state.responseSize) + " B");
responseHeadersViewer.populate(state.responseHeaders); responseHeadersViewer.populate(state.responseHeaders);
} }
private void prettifyResponseBody(String body, String contentType) { private void prettifyResponseBody(String body, String contentType) {
displayLayer(ResponseLayer.RESPONSE); showLayer(ResponseLayer.RESPONSE);
visualizerTab.setDisable(true); visualizerTab.setDisable(true);
visualizer.clear(); visualizer.clear();
@ -485,7 +485,7 @@ public class DashboardController implements Initializable {
} catch (Exception e) { } catch (Exception e) {
snackbar.show("Response could not be parsed.", 5000); snackbar.show("Response could not be parsed.", 5000);
Services.loggingService.logSevere("Response could not be parsed.", e, LocalDateTime.now()); Services.loggingService.logSevere("Response could not be parsed.", e, LocalDateTime.now());
errorLayer.setVisible(true); showLayer(ResponseLayer.ERROR);
errorTitle.setText("Parsing Error"); errorTitle.setText("Parsing Error");
errorDetails.setText("Everest could not parse the response."); errorDetails.setText("Everest could not parse the response.");
} }
@ -509,7 +509,7 @@ public class DashboardController implements Initializable {
private void clearResponseArea() { private void clearResponseArea() {
responseBox.getChildren().remove(0); responseBox.getChildren().remove(0);
responseArea.clear(); responseArea.clear();
promptLayer.setVisible(true); showLayer(ResponseLayer.PROMPT);
} }
@FXML @FXML
@ -619,38 +619,44 @@ public class DashboardController implements Initializable {
composerState.params = getParamFieldStates(); composerState.params = getParamFieldStates();
dashboardState.composer = composerState; dashboardState.composer = composerState;
dashboardState.showResponse = responseLayer.isVisible(); dashboardState.visibleLayer = visibleLayer;
if (dashboardState.showResponse) { switch (visibleLayer) {
dashboardState.responseHeaders = responseHeadersViewer.getHeaders(); case RESPONSE:
dashboardState.statusCode = Integer.parseInt(statusCode.getText()); dashboardState.responseHeaders = responseHeadersViewer.getHeaders();
dashboardState.responseCode = Integer.parseInt(statusCode.getText());
String temp = responseSize.getText(); String temp = responseSize.getText();
temp = temp.substring(0, temp.length() - 2); temp = temp.substring(0, temp.length() - 2);
dashboardState.responseSize = Integer.parseInt(temp); dashboardState.responseSize = Integer.parseInt(temp);
temp = responseTime.getText(); temp = responseTime.getText();
temp = temp.substring(0, temp.length() - 3); temp = temp.substring(0, temp.length() - 3);
dashboardState.responseTime = Integer.parseInt(temp); dashboardState.responseTime = Integer.parseInt(temp);
dashboardState.responseBody = responseArea.getText(); dashboardState.responseBody = responseArea.getText();
String contentType; String contentType;
switch (responseTypeBox.getValue()) { switch (responseTypeBox.getValue()) {
case "JSON": case "JSON":
contentType = MediaType.APPLICATION_JSON; contentType = MediaType.APPLICATION_JSON;
break; break;
case "XML": case "XML":
contentType = MediaType.APPLICATION_XML; contentType = MediaType.APPLICATION_XML;
break; break;
case "HTML": case "HTML":
contentType = MediaType.TEXT_HTML; contentType = MediaType.TEXT_HTML;
break; break;
default: default:
contentType = MediaType.TEXT_PLAIN; contentType = MediaType.TEXT_PLAIN;
} }
dashboardState.responseType = contentType; dashboardState.responseType = contentType;
break;
case ERROR:
dashboardState.errorTitle = errorTitle.getText();
dashboardState.errorDetails = errorDetails.getText();
break;
} }
return dashboardState; return dashboardState;
@ -665,8 +671,22 @@ public class DashboardController implements Initializable {
if (state == null) if (state == null)
return; return;
if (state.showResponse) if (state.visibleLayer != null) {
displayResponse(state); switch (state.visibleLayer) {
case RESPONSE:
showResponse(state);
break;
case ERROR:
errorTitle.setText(state.errorTitle);
errorDetails.setText(state.errorDetails);
showLayer(ResponseLayer.ERROR);
break;
}
if (state.visibleLayer.equals(ResponseLayer.RESPONSE))
showResponse(state);
else
showLayer(state.visibleLayer);
}
if (state.composer == null) if (state.composer == null)
return; return;
@ -718,7 +738,7 @@ public class DashboardController implements Initializable {
clearParams(); clearParams();
bodyTabController.reset(); bodyTabController.reset();
responseArea.clear(); responseArea.clear();
displayLayer(ResponseLayer.PROMPT); showLayer(ResponseLayer.PROMPT);
responseTabPane.getSelectionModel().select(0); responseTabPane.getSelectionModel().select(0);
} }

View file

@ -24,6 +24,7 @@ import com.rohitawate.everest.misc.KeyMap;
import com.rohitawate.everest.misc.Services; import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager; import com.rohitawate.everest.misc.ThemeManager;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -94,28 +95,22 @@ public class HomeWindowController implements Initializable {
tabPane.getSelectionModel().selectedItemProperty().addListener(this::onTabSwitched); tabPane.getSelectionModel().selectedItemProperty().addListener(this::onTabSwitched);
initAddressReflection(); addressProperty.addListener(this::onTargetChanged);
} }
private void initAddressReflection() { /**
addressProperty.addListener(((observable, oldValue, newValue) -> { * Sets up the reflection of the address in the selected tab.
Tab activeTab = tabPane.getSelectionModel().getSelectedItem(); * Displays the current target if it is not empty, "New Tab" otherwise.
if (activeTab != null) { */
if (newValue.equals("")) private void onTargetChanged(Observable observable, String oldValue, String newValue) {
activeTab.setText("New Tab"); Tab activeTab = tabPane.getSelectionModel().getSelectedItem();
else if (activeTab == null)
activeTab.setText(newValue); return;
}
}));
// Initialize the text of tabs loaded by the state recovery logic if (newValue.equals(""))
tabPane.getTabs().forEach(tab -> { activeTab.setText("New Tab");
String target = tabStateMap.get(tab).composer.target; else
if (target.equals("")) activeTab.setText(newValue);
tab.setText("New Tab");
else
tab.setText(target);
});
} }
/** /**
@ -162,7 +157,16 @@ public class HomeWindowController implements Initializable {
*/ */
private void addTab(ComposerState composerState) { private void addTab(ComposerState composerState) {
Tab newTab = new Tab(); Tab newTab = new Tab();
newTab.setText("New Tab");
/*
Initializing the tab text based on the target in the ComposerState.
Further handling of the tab text is done by onTargetChanged().
*/
String target = composerState.target;
if (target == null || target.equals(""))
newTab.setText("New Tab");
else
newTab.setText(target);
DashboardState newState = new DashboardState(composerState); DashboardState newState = new DashboardState(composerState);
tabStateMap.put(newTab, newState); tabStateMap.put(newTab, newState);

View file

@ -16,16 +16,25 @@
package com.rohitawate.everest.controllers.state; package com.rohitawate.everest.controllers.state;
import com.rohitawate.everest.controllers.DashboardController.ResponseLayer;
import java.util.HashMap; import java.util.HashMap;
public class DashboardState { public class DashboardState {
public ComposerState composer; public ComposerState composer;
public boolean showResponse; public ResponseLayer visibleLayer;
public int statusCode;
// ResponseLayer parameters
public int responseCode;
public String responseType; public String responseType;
public String responseBody; public String responseBody;
public int responseTime; public int responseTime;
public int responseSize; public int responseSize;
// ErrorLayer parameters
public String errorTitle;
public String errorDetails;
public HashMap<String, String> responseHeaders; public HashMap<String, String> responseHeaders;
public DashboardState() { public DashboardState() {