Added ability to click on a line number and jump to that line number.

This commit is contained in:
a2geek committed 2026-11-15 13:01:13 -06:00
1 parent c6a790b673
commit acaebfbe75
5 files changed
+131 -59

No files matched your search

@@ -26,6 +26,7 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.Clipboard;
@@ -45,6 +46,7 @@ import org.applecommander.filestore.FileEntry;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -59,6 +61,7 @@ public class FileContentWindow {
private final ToolBar toolBar;
private final Label statusBar;
private final StackPane content;
private final ScrollPane textScrollPane;
private final SelectableTextFlow textView;
private Stage stage;
@@ -87,13 +90,13 @@ public class FileContentWindow {
this.fileEntry = fileEntry;
this.fontSize = Font.getDefault().getSize();
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
textScrollPane = new ScrollPane(content);
textScrollPane.setFitToHeight(true);
textScrollPane.setFitToWidth(true);
this.window = new BorderPane();
this.window.setTop(toolBar);
this.window.setCenter(scrollPane);
this.window.setCenter(textScrollPane);
this.window.setBottom(statusBar);
initView();
@@ -258,55 +261,11 @@ public class FileContentWindow {
}
private void showBASIC(BASICConverter converter) {
List<Text> components = new ArrayList<>();
converter.generate(new BASICConverter.EventHandler() {
@Override
public void startOfLine(String lineNumber) {
components.add(new Text(lineNumber));
}
@Override
public void rawBytes(byte[] data) {
components.add(new Text("*** HEX - TODO ***"));
}
@Override
public void token(String text) {
components.add(new Text(text));
}
@Override
public void comment(String comment) {
components.add(new Text(comment));
}
@Override
public void endOfLine() {
components.add(new Text("\n"));
}
@Override
public void string(String string) {
components.add(new Text(string));
}
@Override
public void targetLine(String lineNumber) {
components.add(new Text(lineNumber));
}
@Override
public void identifier(String identifier) {
components.add(new Text(identifier));
}
@Override
public void number(String number) {
components.add(new Text(number));
}
@Override
public void syntax(String syntax) {
components.add(new Text(syntax));
}
});
Font font = Font.font("monospaced", fontSize);
components.forEach(t -> t.setFont(font));
BASICEventHandler handler = new BASICEventHandler(fontSize, textScrollPane);
converter.generate(handler);
textView.getChildren().clear();
textView.getChildren().addAll(components);
textView.getChildren().addAll(handler.getComponents());
}
private void updateStatusBar() {
@@ -315,7 +274,7 @@ public class FileContentWindow {
return;
}
switch (viewTypeProperty.getValue()) {
case TEXT, HEX -> statusBar.setText(String.format("Font size: %1.0f pt", fontSize));
case TEXT, HEX, BASIC -> statusBar.setText(String.format("Font size: %1.0f pt", fontSize));
}
}
@@ -324,4 +283,88 @@ public class FileContentWindow {
HEX,
BASIC
}
public static class BASICEventHandler implements BASICConverter.EventHandler {
private final Font font;
private final List<Node> components = new ArrayList<>();
private final ScrollPane scrollPane;
private final Map<String,Node> lineNumbers = new HashMap<>();
public BASICEventHandler(double fontSize, ScrollPane scrollPane) {
this.font = Font.font("monospaced", fontSize);
this.scrollPane = scrollPane;
}
public List<Node> getComponents() {
return components;
}
private void addText(String value) {
if (components.isEmpty() || !(components.getLast() instanceof Text text)) {
addNewText(value);
}
else {
text.setText(text.getText() + value);
}
}
private Node addNewText(String value) {
Text text = new Text(value);
text.setFont(font);
components.add(text);
return text;
}
private void addLink(String value) {
Hyperlink link = new Hyperlink();
link.setFont(font);
link.setText(value);
link.setOnAction(_ -> {
Node node = lineNumbers.get(value.trim());
if (node != null) {
FxUtils.centerNodeInScrollPane(scrollPane, node);
}
});
components.add(link);
}
@Override
public void startOfLine(String lineNumber) {
Node node = addNewText(lineNumber);
lineNumbers.put(lineNumber.trim(), node);
}
@Override
public void rawBytes(byte[] data) {
addText("*** HEX - TODO ***");
}
@Override
public void token(String text) {
addText(text);
}
@Override
public void comment(String comment) {
addText(comment);
}
@Override
public void endOfLine() {
addText("\n");
}
@Override
public void string(String string) {
addText(string);
}
@Override
public void targetLine(String lineNumber) {
addLink(lineNumber);
}
@Override
public void identifier(String identifier) {
addText(identifier);
}
@Override
public void number(String number) {
addText(number);
}
@Override
public void syntax(String syntax) {
addText(syntax);
}
}
}
@@ -23,6 +23,7 @@ import javafx.beans.property.BooleanProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
@@ -156,4 +157,14 @@ public class FxUtils {
return alert.showAndWait().map(ButtonType.YES::equals).orElse(false);
}
// Source - https://stackoverflow.com/a/23518314
public static void centerNodeInScrollPane(ScrollPane scrollPane, Node node) {
double h = scrollPane.getContent().getBoundsInLocal().getHeight();
double y = (node.getBoundsInParent().getMaxY() +
node.getBoundsInParent().getMinY()) / 2.0;
double v = scrollPane.getViewportBounds().getHeight();
scrollPane.setVvalue(scrollPane.getVmax() * ((y - 0.5 * v) / (h - v)));
}
}
@@ -55,6 +55,7 @@ public class ApplesoftConverter implements BASICConverter {
int nextAddress = 0;
State state = State.START;
boolean done = false;
boolean inGotoGosub = false;
while (program.hasRemaining() && !done) {
switch (state) {
case START -> {
@@ -66,6 +67,7 @@ public class ApplesoftConverter implements BASICConverter {
int lineNumber = program.readUnsignedShort();
handler.startOfLine(lineNumber + " ");
state = State.CODE;
inGotoGosub = false;
}
}
case CODE -> {
@@ -89,7 +91,7 @@ public class ApplesoftConverter implements BASICConverter {
}
else if (token == Token.GOTO || token == Token.GOSUB) {
handler.token(" " + token.text() + " ");
handler.targetLine(scanWhile(program, -1, Character::isDigit));
inGotoGosub = true;
}
else if (token != null) {
handler.token(" " + token.text() + " ");
@@ -103,10 +105,16 @@ public class ApplesoftConverter implements BASICConverter {
ch -> isCharacter(ch) || isDigit(ch) || ch == '$' || ch == '%'));
}
else if (isDigit(value) || value == '.') {
handler.number(scanWhile(program, value, ch -> isDigit(ch) || ch == '.' || ch == '-'));
if (inGotoGosub && isDigit(value)) {
handler.targetLine(scanWhile(program, value, Character::isDigit));
}
else {
handler.number(scanWhile(program, value, ch -> isDigit(ch) || ch == '.' || ch == '-'));
}
}
else {
handler.syntax(Character.toString(value));
if (value != ',') inGotoGosub = false;
}
}
}
@@ -31,7 +31,7 @@ public interface BASICConverter {
/// Test if this character is an ASCII character between A and Z. Java's tests are for Unicode.
default boolean isCharacter(int ch) {
return ch >= 'A' && ch <= 'Z';
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}
/// Test if this character is a digit. Just in case the digits have extra codes for Unicode.
@@ -134,6 +134,7 @@ public class BusinessBASICConverter implements BASICConverter {
boolean done = false;
int indent = 0;
boolean needsSpace = false;
boolean inGotoGosub = false;
while (program.hasRemaining() && !done) {
switch (state) {
case START -> {
@@ -146,6 +147,7 @@ public class BusinessBASICConverter implements BASICConverter {
handler.startOfLine(lineNumber + Strings.repeat(" ", indent+2));
state = State.CODE;
needsSpace = false;
inGotoGosub = false;
}
}
case CODE -> {
@@ -167,7 +169,8 @@ public class BusinessBASICConverter implements BASICConverter {
token = TOKENS[value & 0x7f];
}
if (needsSpace && value != ':' && value != '=' && value != ',' && value != ';' && value != '(' && value != ')') {
final String skipSpace = ":=,;()+-*/^";
if (needsSpace && skipSpace.indexOf(value) == -1) {
handler.syntax(" ");
}
needsSpace = false;
@@ -179,9 +182,9 @@ public class BusinessBASICConverter implements BASICConverter {
needsSpace = true;
}
else if (value == GOTO || value == GOSUB) {
handler.token(token + " ");
handler.targetLine(scanWhile(program, -1, Character::isDigit));
handler.token(token);
needsSpace = true;
inGotoGosub = true;
}
else if (token != null) {
handler.token(token);
@@ -193,14 +196,21 @@ public class BusinessBASICConverter implements BASICConverter {
else if (isCharacter(value)) {
handler.identifier(scanWhile(program, value,
ch -> isCharacter(ch) || isDigit(ch) || ch == '$' || ch == '%'));
needsSpace = true;
}
else if (isDigit(value) || value == '.') {
handler.number(scanWhile(program, value, ch -> isDigit(ch) || ch == '.' || ch == '-'));
if (inGotoGosub && isDigit(value)) {
handler.targetLine(scanWhile(program, value, Character::isDigit));
}
else {
handler.number(scanWhile(program, value, ch -> isDigit(ch) || ch == '.' || ch == '-'));
}
needsSpace = true;
}
else {
handler.syntax(Character.toString(value));
needsSpace = value == ')';
if (value != ',') inGotoGosub = false;
}
}
}