From 0efab96a1c69bbd408eb7b2f760fe6dc2da63e40 Mon Sep 17 00:00:00 2001 From: Sarah Grebing Date: Mon, 19 Jun 2017 20:43:16 +0200 Subject: [PATCH] line highlighting preparation --- .../kit/formal/gui/controls/ScriptArea.java | 85 ++++++++++++++++++- .../formal/gui/controls/ScriptAreaTab.java | 3 +- .../edu/kit/formal/gui/model/RootModel.java | 10 ++- .../edu/kit/formal/gui/debugger-ui.less | 4 + 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/kit/formal/gui/controls/ScriptArea.java b/src/main/java/edu/kit/formal/gui/controls/ScriptArea.java index b0ad25ca..c183adcd 100644 --- a/src/main/java/edu/kit/formal/gui/controls/ScriptArea.java +++ b/src/main/java/edu/kit/formal/gui/controls/ScriptArea.java @@ -12,10 +12,14 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.ObservableSet; +import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Point2D; import javafx.scene.Node; +import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; +import javafx.scene.control.MenuItem; +import javafx.scene.input.MouseEvent; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.HBox; @@ -26,29 +30,43 @@ import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.Token; +import org.fxmisc.richtext.CharacterHit; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.MouseOverTextEvent; +import org.fxmisc.richtext.model.NavigationActions; +import org.fxmisc.richtext.model.Paragraph; +import org.fxmisc.richtext.model.StyledText; import org.reactfx.collection.LiveList; import org.reactfx.value.Val; import java.time.Duration; +import java.util.Collection; import java.util.Collections; import java.util.function.IntFunction; /** - * ScriptArea is the textarea on the left side of the GUI. It displays the script code and allows highlighting of lines and setting of breakpoints + * ScriptArea is the textarea on the left side of the GUI. + * It displays the script code and allows highlighting of lines and setting of breakpoints + * */ public class ScriptArea extends CodeArea { + /** + * Lines to highlight? + */ private ObservableSet markedLines = FXCollections.observableSet(); private GutterFactory gutter; private ANTLR4LexerHighlighter highlighter; private ListProperty problems = new SimpleListProperty<>(FXCollections.observableArrayList()); - + private SimpleObjectProperty currentMouseOver = new SimpleObjectProperty<>(); public ScriptArea() { init(); } + public void setCurrentMouseOver(CharacterHit i) { + currentMouseOver.set(i); + } + private void init() { this.setWrapText(true); gutter = new GutterFactory(); @@ -77,6 +95,21 @@ public class ScriptArea extends CodeArea { }).subscribe(s -> setStyleSpans(0, s));*/ getStyleClass().add("script-area"); installPopup(); + + + this.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> { + CharacterHit hit = this.hit(e.getX(), e.getY()); + currentMouseOver.set(this.hit(e.getX(), e.getY())); + int characterPosition = hit.getInsertionIndex(); + //System.out.println("characterPosition = " + characterPosition); + // move the caret to that character's position + this.moveTo(characterPosition, NavigationActions.SelectionPolicy.CLEAR); + + }); + + ScriptAreaContextMenu cm = new ScriptAreaContextMenu(currentMouseOver); + this.setContextMenu(cm); + } private void installPopup() { @@ -152,6 +185,25 @@ public class ScriptArea extends CodeArea { } } + /** + * Highlight line given by the characterindex + * + * @param chrIdx + */ + public void highlightLine(int chrIdx) { + //calculate line number from characterindex + int lineNumber = this.offsetToPosition(chrIdx, Bias.Forward).getMajor(); + Paragraph, StyledText>, Collection> paragraph = this.getParagraph(lineNumber); + //calculate start and endposition + int startPos = getAbsolutePosition(this.offsetToPosition(chrIdx, Bias.Forward).getMajor(), 0); + int length = paragraph.length(); + //highlight line + + this.setStyle(startPos, startPos + length, Collections.singleton("line-highlight-postmortem")); + + } + + public class GutterFactory implements IntFunction { private final Background defaultBackground = new Background(new BackgroundFill(Color.web("#ddd"), null, null)); @@ -232,4 +284,33 @@ public class ScriptArea extends CodeArea { } } + + + private class ScriptAreaContextMenu extends ContextMenu { + MenuItem showPostmortemTab = new MenuItem("Show this State"); + //MenuItem showPostmortemTab = new MenuItem("Show this State"); + + SimpleObjectProperty currentIdx = new SimpleObjectProperty<>(); + + public ScriptAreaContextMenu(SimpleObjectProperty currentMouseOver) { + super(); + this.addEventHandler(ActionEvent.ACTION, e -> { + ScriptAreaContextMenu cm = (ScriptAreaContextMenu) e.getSource(); + ScriptArea area = (ScriptArea) cm.getOwnerNode(); + int chrIdx = currentIdx.get().getCharacterIndex().orElse(0); + if (chrIdx != 0) { + area.highlightLine(chrIdx); + } + }); + + currentIdx.bind(currentMouseOver); + showPostmortemTab.addEventHandler(ActionEvent.ACTION, e -> { + System.out.println(currentIdx.get()); + }); + this.getItems().add(showPostmortemTab); + + } + + + } } \ No newline at end of file diff --git a/src/main/java/edu/kit/formal/gui/controls/ScriptAreaTab.java b/src/main/java/edu/kit/formal/gui/controls/ScriptAreaTab.java index e84a0ade..8f8ea857 100644 --- a/src/main/java/edu/kit/formal/gui/controls/ScriptAreaTab.java +++ b/src/main/java/edu/kit/formal/gui/controls/ScriptAreaTab.java @@ -24,9 +24,8 @@ public class ScriptAreaTab extends Tab { try { loader.load(); } catch (IOException e) { - e.printStackTrace(); - } + } } public ScriptArea getScriptArea() { diff --git a/src/main/java/edu/kit/formal/gui/model/RootModel.java b/src/main/java/edu/kit/formal/gui/model/RootModel.java index e5f0cba5..e6463f43 100644 --- a/src/main/java/edu/kit/formal/gui/model/RootModel.java +++ b/src/main/java/edu/kit/formal/gui/model/RootModel.java @@ -35,10 +35,6 @@ public class RootModel { */ private final SimpleObjectProperty keYFile = new SimpleObjectProperty<>(); - /** - * Property: current loaded script string - */ - //private ObservableValue currentScript; /** * ListProperty: list of goal nodes in the current state (depending on interpreter state) @@ -50,8 +46,14 @@ public class RootModel { */ private final SimpleObjectProperty> currentSelectedGoalNode = new SimpleObjectProperty<>(); + /** + * Loaded contracts + */ private final SimpleListProperty loadedContracts = new SimpleListProperty<>(FXCollections.observableArrayList()); + /** + * Chosen contract for problem + */ private final SimpleObjectProperty chosenContract = new SimpleObjectProperty<>(); public RootModel() { diff --git a/src/main/resources/edu/kit/formal/gui/debugger-ui.less b/src/main/resources/edu/kit/formal/gui/debugger-ui.less index f9ad80cc..3f9da7fd 100644 --- a/src/main/resources/edu/kit/formal/gui/debugger-ui.less +++ b/src/main/resources/edu/kit/formal/gui/debugger-ui.less @@ -79,6 +79,10 @@ -rtfx-background-color: @magenta; -fx-underline: true; } + + .line-highlight-postmortem{ + -rtfx-background-color: @blue; + } } /**********************************************************************************************************************/ -- GitLab