Added JSON syntax highlighting for request composer

This commit is contained in:
Rohit Awate 2018-06-20 22:18:11 +05:30
parent 5c7a0411de
commit 45f4ca5cb1
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
5 changed files with 83 additions and 49 deletions

1
.gitignore vendored
View file

@ -12,4 +12,5 @@ dependency-reduced-pom.xml
/BugReporter/src/META-INF/
BugReporter/src/META-INF/
Everest/
out/

View file

@ -16,6 +16,8 @@
package com.rohitawate.everest.controllers;
import com.rohitawate.everest.controllers.responsearea.EverestCodeArea;
import com.rohitawate.everest.controllers.responsearea.EverestCodeArea.HighlightMode;
import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.models.requests.DataDispatchRequest;
import com.rohitawate.everest.util.Services;
@ -24,9 +26,14 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import org.fxmisc.flowless.VirtualizedScrollPane;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
@ -46,12 +53,13 @@ public class BodyTabController implements Initializable {
@FXML
ComboBox<String> rawInputTypeBox;
@FXML
TextArea rawInputArea;
@FXML
Tab rawTab, binaryTab, formTab, urlTab;
@FXML
TextField filePathField;
@FXML
private VBox rawVBox;
EverestCodeArea rawInputArea;
FormDataTabController formDataTabController;
URLTabController urlTabController;
@ -60,6 +68,29 @@ public class BodyTabController implements Initializable {
rawInputTypeBox.getItems().addAll("PLAIN TEXT", "JSON", "XML", "HTML");
rawInputTypeBox.getSelectionModel().select(0);
rawInputArea = new EverestCodeArea();
rawInputArea.setPrefHeight(1500); // Hack to make the EverestCodeArea stretch with the Composer
rawVBox.getChildren().add(new VirtualizedScrollPane<>(rawInputArea));
rawInputTypeBox.valueProperty().addListener(change -> {
String type = rawInputTypeBox.getValue();
HighlightMode mode;
switch (type) {
case "JSON":
mode = HighlightMode.JSON;
break;
case "XML":
mode = HighlightMode.XML;
break;
case "HTML":
mode = HighlightMode.HTML;
break;
default:
mode = HighlightMode.NONE;
}
rawInputArea.setMode(mode);
});
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/homewindow/FormDataTab.fxml"));
formTab.setContent(loader.load());
@ -172,7 +203,23 @@ public class BodyTabController implements Initializable {
}
private void setRawTab(DashboardState dashboardState, String contentType) {
rawInputArea.setText(dashboardState.getBody());
HighlightMode mode;
switch (contentType) {
case MediaType.APPLICATION_JSON:
mode = HighlightMode.JSON;
break;
case MediaType.APPLICATION_XML:
mode = HighlightMode.XML;
break;
case MediaType.TEXT_HTML:
mode = HighlightMode.HTML;
break;
default:
mode = HighlightMode.NONE;
}
rawInputArea.setText(dashboardState.getBody(), mode);
rawInputTypeBox.getSelectionModel().select(contentType);
bodyTabPane.getSelectionModel().select(rawTab);
}

View file

@ -8,27 +8,30 @@ import java.time.Duration;
public class EverestCodeArea extends CodeArea {
public enum HighlightMode {
JSON, XML, NONE
JSON, XML, HTML, NONE
}
private Highlighter highlighter;
private JSONHighlighter jsonHighlighter;
public EverestCodeArea() {
this.getStylesheets().add(getClass().getResource("/css/syntax/Ganges.css").toString());
this.getStylesheets().add(getClass().getResource("/css/syntax/Moondust.css").toString());
this.getStyleClass().add("everest-code-area");
jsonHighlighter = new JSONHighlighter();
this.multiPlainChanges()
.successionEnds(Duration.ofMillis(1))
.subscribe(ignore -> this.setStyleSpans(0, highlighter.computeHighlighting(getText())));
}
jsonHighlighter = new JSONHighlighter();
public void setMode(HighlightMode mode) {
highlighter = mode == HighlightMode.JSON ? jsonHighlighter : jsonHighlighter;
}
public void setText(String text, HighlightMode mode) {
clear();
appendText(text);
highlighter = mode == HighlightMode.JSON ? jsonHighlighter : jsonHighlighter;
setMode(mode);
}
}

View file

@ -1,3 +1,17 @@
.everest-code-area {
-fx-background-color: #282828;
}
.everest-code-area .text {
-fx-fill: azure;
-fx-font-family: "Liberation Mono";
-fx-font-size: 17px;
}
.everest-code-area .caret {
-fx-stroke: white;
}
.json_curly {
-fx-fill: coral !important;
}
@ -24,17 +38,3 @@
-fx-fill: greenyellow !important;
-fx-font-weight: bold;
}
.everest-code-area {
-fx-background-color: #282828;
}
.everest-code-area .text {
-fx-fill: azure;
-fx-font-family: "Liberation Mono";
-fx-font-size: 17px;
}
.everest-code-area .caret {
-fx-stroke: white;
}

View file

@ -28,32 +28,15 @@
<Tab fx:id="urlTab" text="URL ENCODED"/>
<Tab fx:id="rawTab" text="RAW">
<content>
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER">
<content>
<VBox alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" VBox.vgrow="ALWAYS">
<children>
<ComboBox fx:id="rawInputTypeBox" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</HBox.margin>
</ComboBox>
</children>
<VBox.margin>
<Insets/>
</VBox.margin>
</HBox>
<TextArea fx:id="rawInputArea" promptText="Raw data goes here..." wrapText="true"
VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0"/>
</VBox.margin>
</TextArea>
</children>
</VBox>
</content>
</ScrollPane>
<VBox fx:id="rawVBox">
<children>
<ComboBox fx:id="rawInputTypeBox" maxHeight="30.0" visibleRowCount="5" VBox.vgrow="SOMETIMES">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</VBox.margin>
</ComboBox>
</children>
</VBox>
</content>
</Tab>
<Tab fx:id="binaryTab" text="BINARY">