Skip to content

Commit

Permalink
sonar fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ozkanpakdil committed Jul 17, 2023
1 parent d370fc6 commit 8aee0c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
16 changes: 4 additions & 12 deletions src/main/java/com/mascix/swaggerific/SwaggerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand All @@ -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());
Expand Down
31 changes: 12 additions & 19 deletions src/main/java/com/mascix/swaggerific/ui/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class MainController implements Initializable {
JsonNode jsonRoot;
JsonColorizer jsonColorizer = new JsonColorizer();
XmlColorizer xmlColorizer = new XmlColorizer();
TreeItem<String> root = new TreeItem<>("base root");
TreeItem<String> treeItemRoot = new TreeItem<>("base root");
String urlTarget;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("loader.fxml"));
private VBox boxLoader;
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -424,7 +417,7 @@ private void returnTreeItemsForTheMethod(PathItem pathItem, ObservableList<TreeI
}

public void treeOnClick(MouseEvent mouseEvent) {
if (root.getChildren().size() == 0)// if no item there open swagger.json loader
if (treeItemRoot.getChildren().size() == 0)// if no item there open swagger.json loader
menuFileOpenSwagger(null);
}

Expand All @@ -448,7 +441,7 @@ public void onClose() {
try {
FileOutputStream out = new FileOutputStream(SESSION);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(new TreeItemSerialisationWrapper(root));
oos.writeObject(new TreeItemSerialisationWrapper(treeItemRoot));
oos.flush();
} catch (Exception e) {
log.error("Problem serializing", e);
Expand All @@ -459,8 +452,8 @@ public void onOpening() {
if (Paths.get(SESSION).toFile().isFile()) {
try (ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(Files.readAllBytes(Path.of(SESSION))))) {
root = (TreeItem<String>) ois.readObject();
treePaths.setRoot(root);
treeItemRoot = (TreeItem<String>) ois.readObject();
treePaths.setRoot(treeItemRoot);
treePaths.setShowRoot(false);
} catch (Exception e) {
log.error("Problem deserializing", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

public class BracketHighlighter {
private final CustomCodeArea codeArea;
private final List<BracketPair> bracketPairs = new ArrayList<>();
private final List<String> LOOP_STYLE = Collections.singletonList("loop");
private final List<String> MATCH_STYLE = Arrays.asList("match", "loop");
private final String BRACKET_PAIRS_REGEX = "[(){}\\[\\]<>]";
private final List<BracketPair> bracketPairList = new ArrayList<>();
private final List<String> loopStyle = Collections.singletonList("loop");
private final List<String> matchStyle = Arrays.asList("match", "loop");
private final String bracketPairsRegex = "[(){}\\[\\]<>]";

/**
* Parameterized constructor
Expand All @@ -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);
}
}

Expand All @@ -51,16 +51,16 @@ 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
// if even (opening bracket) then step forwards, otherwise step backwards
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;
Expand All @@ -87,9 +87,9 @@ public void highlightBracket() {
* Clear the existing highlighted bracket styles
*/
public void clearBracket() {
Iterator<BracketPair> iterator = this.bracketPairs.iterator();
Iterator<BracketPair> iterator = this.bracketPairList.iterator();
while (iterator.hasNext()) {
styleBrackets(iterator.next(), LOOP_STYLE);
styleBrackets(iterator.next(), loopStyle);
iterator.remove();
}
}
Expand All @@ -102,7 +102,7 @@ private void styleBrackets(BracketPair pair, List<String> styles) {
private void styleBracket(int pos, List<String> 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);
}
}
Expand Down

0 comments on commit 8aee0c3

Please sign in to comment.