Skip to content

Commit e6d5573

Browse files
Benjamin PeterBenjamin Peter
authored andcommitted
AbstractValueIndicator: prevent label editing when setEditable(false)
- Affects XValueIndicator, YValueIndicator, YWatchValueIndicator
1 parent f8a5094 commit e6d5573

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractValueIndicator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected AbstractValueIndicator(Axis axis, final String text) {
103103
});
104104
// mouse handler to edit the indicator on right click
105105
label.setOnMouseClicked(evt -> {
106-
if (evt.getButton().equals(MouseButton.SECONDARY)) {
106+
if (evt.getButton().equals(MouseButton.SECONDARY) && isEditable()) {
107107
label.setVisible(false);
108108
getChartChildren().add(labelEdit);
109109
labelEdit.requestFocus();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.fair_acc.chartfx.plugins;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.hasSize;
5+
6+
import java.util.Set;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.testfx.api.FxRobot;
11+
import org.testfx.framework.junit5.ApplicationExtension;
12+
import org.testfx.framework.junit5.Start;
13+
14+
import io.fair_acc.chartfx.XYChart;
15+
import io.fair_acc.dataset.DataSet;
16+
import io.fair_acc.dataset.testdata.spi.CosineFunction;
17+
import javafx.scene.Node;
18+
import javafx.scene.Scene;
19+
import javafx.scene.input.MouseButton;
20+
import javafx.stage.Stage;
21+
22+
@ExtendWith(ApplicationExtension.class)
23+
class XValueIndicatorTest {
24+
25+
private XYChart chart;
26+
private XValueIndicator indicator;
27+
28+
@Start
29+
public void start(Stage stage) {
30+
chart = new XYChart();
31+
indicator = new XValueIndicator(chart.getXAxis(), 5, "POI");
32+
indicator.setLabelPosition(0.5);
33+
chart.getPlugins().add(indicator);
34+
chart.setPrefWidth(400);
35+
chart.setPrefHeight(300);
36+
37+
DataSet ds = new CosineFunction("Cosine", 30);
38+
chart.getDatasets().add(ds);
39+
40+
Scene scene = new Scene(chart, 400, 300);
41+
stage.setScene(scene);
42+
stage.show();
43+
}
44+
45+
@Test
46+
void testThatIndicatorEditLabelTextFieldIsShownWhenEditable(final FxRobot fxRobot) { // NOPMD JUnitTestsShouldIncludeAssert
47+
fxRobot.interrupt();
48+
49+
assertThat(lookupIndicatorTextFields(fxRobot), hasSize(0));
50+
fxRobot.moveTo(lookupFirstIndicatorLabel(fxRobot));
51+
fxRobot.clickOn(MouseButton.SECONDARY);
52+
53+
assertThat(lookupIndicatorTextFields(fxRobot), hasSize(1));
54+
}
55+
56+
@Test
57+
void testThatIndicatorNoEditLabelTextFieldIsShownWhenNotEditable(final FxRobot fxRobot) { // NOPMD JUnitTestsShouldIncludeAssert
58+
fxRobot.interrupt();
59+
60+
indicator.setEditable(false);
61+
62+
assertThat(lookupIndicatorTextFields(fxRobot), hasSize(0));
63+
fxRobot.moveTo(lookupFirstIndicatorLabel(fxRobot));
64+
fxRobot.clickOn(MouseButton.SECONDARY);
65+
66+
assertThat(lookupIndicatorTextFields(fxRobot), hasSize(0));
67+
}
68+
69+
private Node lookupFirstIndicatorLabel(final FxRobot fxRobot) {
70+
return fxRobot.from(chart).lookup("." + AbstractSingleValueIndicator.STYLE_CLASS_LABEL).query();
71+
}
72+
73+
private Set<Node> lookupIndicatorTextFields(final FxRobot fxRobot) {
74+
return fxRobot.from(chart).lookup(".text-field").queryAll();
75+
}
76+
77+
}

0 commit comments

Comments
 (0)