Commit 0efab96a authored by Sarah Grebing's avatar Sarah Grebing

line highlighting preparation

parent 89d49cfb
Pipeline #11253 failed with stage
in 2 minutes and 21 seconds
...@@ -12,10 +12,14 @@ import javafx.beans.property.SimpleObjectProperty; ...@@ -12,10 +12,14 @@ import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.collections.ObservableSet; import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
...@@ -26,29 +30,43 @@ import javafx.scene.text.Font; ...@@ -26,29 +30,43 @@ import javafx.scene.text.Font;
import javafx.scene.text.FontPosture; import javafx.scene.text.FontPosture;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.fxmisc.richtext.CharacterHit;
import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.MouseOverTextEvent; 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.collection.LiveList;
import org.reactfx.value.Val; import org.reactfx.value.Val;
import java.time.Duration; import java.time.Duration;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.function.IntFunction; 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 { public class ScriptArea extends CodeArea {
/**
* Lines to highlight?
*/
private ObservableSet<Integer> markedLines = FXCollections.observableSet(); private ObservableSet<Integer> markedLines = FXCollections.observableSet();
private GutterFactory gutter; private GutterFactory gutter;
private ANTLR4LexerHighlighter highlighter; private ANTLR4LexerHighlighter highlighter;
private ListProperty<LintProblem> problems = new SimpleListProperty<>(FXCollections.observableArrayList()); private ListProperty<LintProblem> problems = new SimpleListProperty<>(FXCollections.observableArrayList());
private SimpleObjectProperty<CharacterHit> currentMouseOver = new SimpleObjectProperty<>();
public ScriptArea() { public ScriptArea() {
init(); init();
} }
public void setCurrentMouseOver(CharacterHit i) {
currentMouseOver.set(i);
}
private void init() { private void init() {
this.setWrapText(true); this.setWrapText(true);
gutter = new GutterFactory(); gutter = new GutterFactory();
...@@ -77,6 +95,21 @@ public class ScriptArea extends CodeArea { ...@@ -77,6 +95,21 @@ public class ScriptArea extends CodeArea {
}).subscribe(s -> setStyleSpans(0, s));*/ }).subscribe(s -> setStyleSpans(0, s));*/
getStyleClass().add("script-area"); getStyleClass().add("script-area");
installPopup(); 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() { private void installPopup() {
...@@ -152,6 +185,25 @@ public class ScriptArea extends CodeArea { ...@@ -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<Collection<String>, StyledText<Collection<String>>, Collection<String>> 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<Node> { public class GutterFactory implements IntFunction<Node> {
private final Background defaultBackground = private final Background defaultBackground =
new Background(new BackgroundFill(Color.web("#ddd"), null, null)); new Background(new BackgroundFill(Color.web("#ddd"), null, null));
...@@ -232,4 +284,33 @@ public class ScriptArea extends CodeArea { ...@@ -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<CharacterHit> currentIdx = new SimpleObjectProperty<>();
public ScriptAreaContextMenu(SimpleObjectProperty<CharacterHit> 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
...@@ -24,10 +24,9 @@ public class ScriptAreaTab extends Tab { ...@@ -24,10 +24,9 @@ public class ScriptAreaTab extends Tab {
try { try {
loader.load(); loader.load();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
}
} }
}
public ScriptArea getScriptArea() { public ScriptArea getScriptArea() {
return scriptArea; return scriptArea;
......
...@@ -35,10 +35,6 @@ public class RootModel { ...@@ -35,10 +35,6 @@ public class RootModel {
*/ */
private final SimpleObjectProperty<File> keYFile = new SimpleObjectProperty<>(); private final SimpleObjectProperty<File> keYFile = new SimpleObjectProperty<>();
/**
* Property: current loaded script string
*/
//private ObservableValue<String> currentScript;
/** /**
* ListProperty: list of goal nodes in the current state (depending on interpreter state) * ListProperty: list of goal nodes in the current state (depending on interpreter state)
...@@ -50,8 +46,14 @@ public class RootModel { ...@@ -50,8 +46,14 @@ public class RootModel {
*/ */
private final SimpleObjectProperty<GoalNode<KeyData>> currentSelectedGoalNode = new SimpleObjectProperty<>(); private final SimpleObjectProperty<GoalNode<KeyData>> currentSelectedGoalNode = new SimpleObjectProperty<>();
/**
* Loaded contracts
*/
private final SimpleListProperty<Contract> loadedContracts = new SimpleListProperty<>(FXCollections.observableArrayList()); private final SimpleListProperty<Contract> loadedContracts = new SimpleListProperty<>(FXCollections.observableArrayList());
/**
* Chosen contract for problem
*/
private final SimpleObjectProperty<Contract> chosenContract = new SimpleObjectProperty<>(); private final SimpleObjectProperty<Contract> chosenContract = new SimpleObjectProperty<>();
public RootModel() { public RootModel() {
......
...@@ -79,6 +79,10 @@ ...@@ -79,6 +79,10 @@
-rtfx-background-color: @magenta; -rtfx-background-color: @magenta;
-fx-underline: true; -fx-underline: true;
} }
.line-highlight-postmortem{
-rtfx-background-color: @blue;
}
} }
/**********************************************************************************************************************/ /**********************************************************************************************************************/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment