diff --git a/app/gui-javafx/src/main/java/org/applecommander/javafx/FileContentWindow.java b/app/gui-javafx/src/main/java/org/applecommander/javafx/FileContentWindow.java index 156f7782..cd084f5e 100644 --- a/app/gui-javafx/src/main/java/org/applecommander/javafx/FileContentWindow.java +++ b/app/gui-javafx/src/main/java/org/applecommander/javafx/FileContentWindow.java @@ -34,6 +34,7 @@ import javafx.scene.input.ClipboardContent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.scene.text.Font; +import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; import org.applecommander.converter.ApplesoftVariables; @@ -71,6 +72,8 @@ public class FileContentWindow { private final ObjectProperty contentTypeProperty = new SimpleObjectProperty<>(); private final ObjectProperty contentSubtypeProperty = new SimpleObjectProperty<>(); private final ObjectProperty viewTypeProperty = new SimpleObjectProperty<>(); + + private ToggleButton basicFormatButton; private double fontSize; private final FileEntry fileEntry; @@ -88,7 +91,7 @@ public class FileContentWindow { this.statusBar.setAlignment(Pos.CENTER_LEFT); this.fileEntry = fileEntry; - this.fontSize = Font.getDefault().getSize(); + this.fontSize = Font.getDefault().getSize() + 1.0; textScrollPane = new ScrollPane(content); textScrollPane.setFitToHeight(true); @@ -209,16 +212,23 @@ public class FileContentWindow { } }); }); + Button copy = FxUtils.createButton("copy.png", "Copy", _ -> { Clipboard clipboard = Clipboard.getSystemClipboard(); ClipboardContent content = new ClipboardContent(); content.putString(textView.getContentAsString()); clipboard.setContent(content); }); + basicFormatButton = FxUtils.createOnOffButton("basic-list-normal.png", "basic-list-pretty.png", + "Listing", e -> handleTextView()); + basicFormatButton.visibleProperty().bind(contentSubtypeProperty.isEqualTo(Subtype.BASIC)); + basicFormatButton.managedProperty().bind(basicFormatButton.visibleProperty()); this.toolBar.getItems().addAll(viewAsText, viewAsBasic, viewAsHex, new Separator(), - decreaseSize, increaseSize, copy); + decreaseSize, increaseSize, copy, + new Separator(), + basicFormatButton); } private void showPlainText(String value) { @@ -261,7 +271,8 @@ public class FileContentWindow { } private void showBASIC(BASICConverter converter) { - BASICEventHandler handler = new BASICEventHandler(fontSize, textScrollPane); + boolean prettyPrint = basicFormatButton.isSelected(); + BASICEventHandler handler = new BASICEventHandler(fontSize, textScrollPane, prettyPrint); converter.generate(handler); textView.getChildren().clear(); @@ -285,28 +296,44 @@ public class FileContentWindow { } public static class BASICEventHandler implements BASICConverter.EventHandler { - private final Font font; + private final Font normalFont; + private final Font boldFont; private final List components = new ArrayList<>(); private final ScrollPane scrollPane; private final Map lineNumbers = new HashMap<>(); + private final boolean prettyPrint; + private boolean startOfLine; - public BASICEventHandler(double fontSize, ScrollPane scrollPane) { - this.font = Font.font("monospaced", fontSize); + public BASICEventHandler(double fontSize, ScrollPane scrollPane, boolean prettyPrint) { + this.normalFont = Font.font("monospaced", fontSize); + this.boldFont = Font.font("monospaced", FontWeight.BOLD, fontSize); this.scrollPane = scrollPane; + this.prettyPrint = prettyPrint; } public List getComponents() { return components; } - private void addText(String value) { - if (components.isEmpty() || !(components.getLast() instanceof Text text)) { - addNewText(value); + private void addText(String value, Font font) { + if (startOfLine) { + value = value.stripLeading(); + startOfLine = false; } - else { + if (components.isEmpty() || !(components.getLast() instanceof Text text)) { + addNewText(value, font); + } + else if (text.getFont() == font) { text.setText(text.getText() + value); } + else { + addNewText(value, font); + } } - private Node addNewText(String value) { + private Node addNewText(String value, Font font) { + if (startOfLine) { + value = value.stripLeading(); + startOfLine = false; + } Text text = new Text(value); text.setFont(font); components.add(text); @@ -314,7 +341,7 @@ public class FileContentWindow { } private void addLink(String value) { Hyperlink link = new Hyperlink(); - link.setFont(font); + link.setFont(normalFont); link.setText(value); link.setOnAction(_ -> { Node node = lineNumbers.get(value.trim()); @@ -327,28 +354,32 @@ public class FileContentWindow { @Override public void startOfLine(String lineNumber) { - Node node = addNewText(lineNumber); + if (prettyPrint) { + lineNumber = lineNumber.trim() + "\t "; + } + Node node = addNewText(lineNumber, normalFont); lineNumbers.put(lineNumber.trim(), node); + startOfLine = true; } @Override public void rawBytes(byte[] data) { - addText("*** HEX - TODO ***"); + addText("*** HEX - TODO ***", normalFont); } @Override public void token(String text) { - addText(text); + addText(text, boldFont); } @Override public void comment(String comment) { - addText(comment); + addText(comment, normalFont); } @Override public void endOfLine() { - addText("\n"); + addText("\n", normalFont); } @Override public void string(String string) { - addText(string); + addText(string, normalFont); } @Override public void targetLine(String lineNumber) { @@ -356,15 +387,21 @@ public class FileContentWindow { } @Override public void identifier(String identifier) { - addText(identifier); + addText(identifier, normalFont); } @Override public void number(String number) { - addText(number); + addText(number, normalFont); } @Override public void syntax(String syntax) { - addText(syntax); + if (prettyPrint && ":".equals(syntax)) { + addText("\n\t: ", normalFont); + startOfLine = true; + } + else { + addText(syntax, normalFont); + } } } } diff --git a/app/gui-javafx/src/main/resources/images/basic-list-normal.png b/app/gui-javafx/src/main/resources/images/basic-list-normal.png new file mode 100644 index 00000000..a773930d Binary files /dev/null and b/app/gui-javafx/src/main/resources/images/basic-list-normal.png differ diff --git a/app/gui-javafx/src/main/resources/images/basic-list-pretty.png b/app/gui-javafx/src/main/resources/images/basic-list-pretty.png new file mode 100644 index 00000000..6739ea65 Binary files /dev/null and b/app/gui-javafx/src/main/resources/images/basic-list-pretty.png differ