diff --git a/src/main/java/com/mascix/swaggerific/SwaggerApplication.java b/src/main/java/com/mascix/swaggerific/SwaggerApplication.java index ed815d1..b99296f 100644 --- a/src/main/java/com/mascix/swaggerific/SwaggerApplication.java +++ b/src/main/java/com/mascix/swaggerific/SwaggerApplication.java @@ -36,9 +36,6 @@ public void start(Stage stage) throws IOException { root.setStyle("-fx-font-family:'" + userPrefs.get(SELECTED_FONT, "Verdana") + "'"); MainController mainController = fxmlLoader.getController(); mainController.onOpening(); -// CustomCodeArea codeArea = new CustomCodeArea(); -// BracketHighlighter bracketHighlighter = new BracketHighlighter(codeArea); -// mainController.setCodeJsonResponse(codeArea); Scene scene = new Scene(root, 800, 600); stage.setTitle("Swaggerific"); stage.getIcons().add(new Image(SwaggerApplication.class.getResourceAsStream("/applogo.png"))); @@ -48,19 +45,14 @@ public void start(Stage stage) throws IOException { } private void loadingWindowLookAndLocation() { - double x = userPrefs.getDouble(STAGE_X, 0); - double y = userPrefs.getDouble(STAGE_Y, 0); - double w = userPrefs.getDouble(STAGE_WIDTH, 800); - double h = userPrefs.getDouble(STAGE_HEIGHT, 600); - primaryStage.setX(x); - primaryStage.setY(y); - primaryStage.setWidth(w); - primaryStage.setHeight(h); + primaryStage.setX(userPrefs.getDouble(STAGE_X, 0)); + primaryStage.setY(userPrefs.getDouble(STAGE_Y, 0)); + primaryStage.setWidth(userPrefs.getDouble(STAGE_WIDTH, 800)); + primaryStage.setHeight(userPrefs.getDouble(STAGE_HEIGHT, 600)); } @Override public void stop() { - Preferences userPrefs = Preferences.userNodeForPackage(getClass()); userPrefs.putDouble(STAGE_X, primaryStage.getX()); userPrefs.putDouble(STAGE_Y, primaryStage.getY()); userPrefs.putDouble(STAGE_WIDTH, primaryStage.getWidth()); diff --git a/src/main/java/com/mascix/swaggerific/ui/MainController.java b/src/main/java/com/mascix/swaggerific/ui/MainController.java index 5f61089..f16685d 100644 --- a/src/main/java/com/mascix/swaggerific/ui/MainController.java +++ b/src/main/java/com/mascix/swaggerific/ui/MainController.java @@ -103,7 +103,7 @@ public class MainController implements Initializable { JsonNode jsonRoot; JsonColorizer jsonColorizer = new JsonColorizer(); XmlColorizer xmlColorizer = new XmlColorizer(); - TreeItem root = new TreeItem<>("base root"); + TreeItem treeItemRoot = new TreeItem<>("base root"); String urlTarget; FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("loader.fxml")); private VBox boxLoader; @@ -117,31 +117,24 @@ public void initialize(URL url, ResourceBundle resourceBundle) { BracketHighlighter bracketHighlighter = new BracketHighlighter(codeJsonResponse); codeJsonResponse.setOnKeyTyped(keyEvent -> { - // clear bracket highlighting bracketHighlighter.clearBracket(); - - // get typed character + /* + //TODO this bock may be used in json request in the future String character = keyEvent.getCharacter(); - - // add a ] if [ is typed if (character.equals("[")) { int position = codeJsonResponse.getCaretPosition(); codeJsonResponse.insert(position, "]", "loop"); codeJsonResponse.moveTo(position); - } - // remove next ] if ] is typed - else if (character.equals("]")) { + } else if (character.equals("]")) { int position = codeJsonResponse.getCaretPosition(); if (position != codeJsonResponse.getLength()) { String nextChar = codeJsonResponse.getText(position, position + 1); if (nextChar.equals("]")) codeJsonResponse.deleteText(position, position + 1); } - } + }*/ - // refresh bracket highlighting bracketHighlighter.highlightBracket(); }); -// setCodeJsonResponse(codeArea); codeResponseJsonSettings(codeJsonRequest, "/css/json-highlighting.css"); codeResponseJsonSettings(codeJsonResponse, "/css/json-highlighting.css"); treePaths.getSelectionModel() @@ -368,10 +361,10 @@ private void setIsOffloading() { @SneakyThrows private void openSwaggerUrl(String urlSwagger) { - root.getChildren().clear(); + treeItemRoot.getChildren().clear(); URL urlApi = new URL(urlSwagger); urlTarget = urlSwagger.replace("swagger.json", ""); - treePaths.setRoot(root); + treePaths.setRoot(treeItemRoot); try { jsonRoot = mapper.readTree(urlApi); jsonModal = mapper.readValue(urlApi, SwaggerModal.class); @@ -386,7 +379,7 @@ private void openSwaggerUrl(String urlSwagger) { returnTreeItemsForTheMethod(pathItem, path.getChildren(), urlApi, jsonModal, it2); } }); - root.getChildren().add(tag); + treeItemRoot.getChildren().add(tag); }); } catch (Exception e) { throw new RuntimeException(e); @@ -424,7 +417,7 @@ private void returnTreeItemsForTheMethod(PathItem pathItem, ObservableList) ois.readObject(); - treePaths.setRoot(root); + treeItemRoot = (TreeItem) ois.readObject(); + treePaths.setRoot(treeItemRoot); treePaths.setShowRoot(false); } catch (Exception e) { log.error("Problem deserializing", e); diff --git a/src/main/java/com/mascix/swaggerific/ui/textfx/BracketHighlighter.java b/src/main/java/com/mascix/swaggerific/ui/textfx/BracketHighlighter.java index 78be4a0..69b415f 100644 --- a/src/main/java/com/mascix/swaggerific/ui/textfx/BracketHighlighter.java +++ b/src/main/java/com/mascix/swaggerific/ui/textfx/BracketHighlighter.java @@ -6,10 +6,10 @@ public class BracketHighlighter { private final CustomCodeArea codeArea; - private final List bracketPairs = new ArrayList<>(); - private final List LOOP_STYLE = Collections.singletonList("loop"); - private final List MATCH_STYLE = Arrays.asList("match", "loop"); - private final String BRACKET_PAIRS_REGEX = "[(){}\\[\\]<>]"; + private final List bracketPairList = new ArrayList<>(); + private final List loopStyle = Collections.singletonList("loop"); + private final List matchStyle = Arrays.asList("match", "loop"); + private final String bracketPairsRegex = "[(){}\\[\\]<>]"; /** * Parameterized constructor @@ -31,13 +31,13 @@ public BracketHighlighter(CustomCodeArea codeArea) { private void highlightBracket(int newVal) { this.clearBracket(); String prevChar = (newVal > 0) ? codeArea.getText(newVal - 1, newVal) : ""; - if (prevChar.matches(BRACKET_PAIRS_REGEX)) --newVal; + if (prevChar.matches(bracketPairsRegex)) --newVal; Integer other = getMatchingBracket(newVal); if (other != null) { BracketPair pair = new BracketPair(newVal, other); - styleBrackets(pair, MATCH_STYLE); - this.bracketPairs.add(pair); + styleBrackets(pair, matchStyle); + this.bracketPairList.add(pair); } } @@ -51,8 +51,8 @@ private Integer getMatchingBracket(int index) { if (index == codeArea.getLength()) return null; char initialBracket = codeArea.getText(index, index + 1).charAt(0); - String BRACKET_PAIRS = "(){}[]<>"; - int bracketTypePosition = BRACKET_PAIRS.indexOf(initialBracket); // "(){}[]<>" + String bracketPairs = "(){}[]<>"; + int bracketTypePosition = bracketPairs.indexOf(initialBracket); // "(){}[]<>" if (bracketTypePosition < 0) return null; // even numbered bracketTypePositions are opening brackets, and odd positions are closing @@ -60,7 +60,7 @@ private Integer getMatchingBracket(int index) { int stepDirection = (bracketTypePosition % 2 == 0) ? +1 : -1; // the matching bracket to look for, the opposite of initialBracket - char match = BRACKET_PAIRS.charAt(bracketTypePosition + stepDirection); + char match = bracketPairs.charAt(bracketTypePosition + stepDirection); index += stepDirection; int bracketCount = 1; @@ -87,9 +87,9 @@ public void highlightBracket() { * Clear the existing highlighted bracket styles */ public void clearBracket() { - Iterator iterator = this.bracketPairs.iterator(); + Iterator iterator = this.bracketPairList.iterator(); while (iterator.hasNext()) { - styleBrackets(iterator.next(), LOOP_STYLE); + styleBrackets(iterator.next(), loopStyle); iterator.remove(); } } @@ -102,7 +102,7 @@ private void styleBrackets(BracketPair pair, List styles) { private void styleBracket(int pos, List styles) { if (pos < codeArea.getLength()) { String text = codeArea.getText(pos, pos + 1); - if (text.matches(BRACKET_PAIRS_REGEX)) { + if (text.matches(bracketPairsRegex)) { codeArea.setStyle(pos, pos + 1, styles); } }