Skip to content

Commit

Permalink
Add Interview font size management #98
Browse files Browse the repository at this point in the history
  • Loading branch information
JeridiOmar committed May 12, 2023
1 parent 26d8d35 commit bb9a816
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.beans.InvalidationListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -56,9 +57,12 @@ public class InterviewTextController implements Initializable {
private InterviewTextCommandFactory interviewTextCommandFactory;
private RichTextAreaController richTextAreaController;
private SearchToolController searchToolController;
private PoliceSizeController increaseSizeController;
private PoliceSizeController decreaseSizeController;
private final Interview interview;
private Pane paneDragText;
private SimpleBooleanProperty isSearchClicked;
private SimpleIntegerProperty fontSize;

private InterviewTextController(Interview interview) {
this.interview = interview;
Expand Down Expand Up @@ -87,7 +91,9 @@ public void initialize(URL location, ResourceBundle resources) {
annotationColorList.add(new AnnotationColor("blue", "#7084B0"));
annotationColorList.add(new AnnotationColor("green", "#7BCF7B"));
this.isSearchClicked = new SimpleBooleanProperty(false);
richTextAreaController = new RichTextAreaController(interview.getInterviewText(), annotationColorList, isSearchClicked);
this.fontSize = new SimpleIntegerProperty(1);
richTextAreaController = new RichTextAreaController(interview.getInterviewText(), annotationColorList,
isSearchClicked, fontSize);

stackPaneInterview.getChildren().add(richTextAreaController.getNode());

Expand Down Expand Up @@ -115,6 +121,10 @@ public void initialize(URL location, ResourceBundle resources) {
toolBarController.setSelectedToolProperty(selectionToolController);
hboxAnnotation.getChildren().add(toolBarController);
searchToolController = new SearchToolController(hboxAnnotation.getChildren(), isSearchClicked);
this.increaseSizeController = new PoliceSizeController(
hboxAnnotation.getChildren(), fontSize, PoliceSizeController.FontAction.INCREASE);
this.increaseSizeController = new PoliceSizeController(
hboxAnnotation.getChildren(), fontSize, PoliceSizeController.FontAction.DECREASE);

// On click release on text area: add a pane over the text area
richTextAreaController.getUserSelection().addListener((change) -> toolBarController.getSelectedToolProperty().get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import application.configuration.Configuration;
import components.interviewPanel.ContextMenus.ContextMenuFactory;
import components.interviewPanel.ToolBar.tools.Controllers.PoliceSizeController;
import components.interviewPanel.search.ButtonSearchType;
import components.interviewPanel.search.SearchButtonHandler;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.ListChangeListener;
Expand All @@ -32,6 +34,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static utils.GlobalVariables.getGlobalVariables;

Expand All @@ -46,12 +50,16 @@ public class RichTextAreaController {
private final List<AnnotationColor> annotationColorList;
private SearchResult searchResult;
private SimpleBooleanProperty isSearchClicked;
private SimpleIntegerProperty fontSize;

public RichTextAreaController(InterviewText interviewText, List<AnnotationColor> annotationColorList, SimpleBooleanProperty isSearchClicked) {

public RichTextAreaController(InterviewText interviewText, List<AnnotationColor> annotationColorList,
SimpleBooleanProperty isSearchClicked, SimpleIntegerProperty fontSize) {
this.interviewText = interviewText;
this.annotationColorList = annotationColorList;
this.isSearchClicked = isSearchClicked;
userSelection = new SimpleObjectProperty<>();
this.fontSize = fontSize;
area = new InlineCssTextArea();
area.setWrapText(true);
area.setEditable(false);
Expand Down Expand Up @@ -80,6 +88,23 @@ public RichTextAreaController(InterviewText interviewText, List<AnnotationColor>
showFindDialog(area);
}
});
this.fontSize.addListener((obs, oldValue, newValue) -> {
String currentStyle = this.area.getStyle();

// Extract the font size from the style using regular expressions
Pattern pattern = Pattern.compile("-fx-font-size: (\\d+)em;");
Matcher matcher = pattern.matcher(currentStyle);
if (matcher.find()) {
// Extract the font size as an integer
int currentFontSize = newValue.intValue();
// Update the style with the new font size
String newStyle = currentStyle.replaceAll("-fx-font-size: \\d+em;", "-fx-font-size: " + currentFontSize + "em;");
this.area.setStyle(newStyle);
} else {
// If the pattern doesn't match, set the font size to the default value (12 px in this example)
this.area.setStyle(currentStyle+"\n-fx-font-size: "+this.fontSize.get()+"em;\n");
}
});

}

Expand Down Expand Up @@ -140,7 +165,7 @@ private void showFindDialog(InlineCssTextArea richTextArea) {
s += resultCount + " ";
s += Configuration.langBundle.getString("matches_found");
return s;
}, this.searchResult.resultCountProperty(),this.searchResult.getCurrentSearchIndexProperty()));
}, this.searchResult.resultCountProperty(), this.searchResult.getCurrentSearchIndexProperty()));
} else {
this.searchResult.resetSearch();
matchCountLabel.setVisible(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package components.interviewPanel.ToolBar.tools.Controllers;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

public class PoliceSizeController {
final String increaseIcon = "/images/increase-font.png";
final String decreaseIcon = "/images/decrease-font.png";
final String increaseIconDisabled = "/images/increase-font-disabled.png";
final String decreaseIconDisabled = "/images/decrease-font-disabled.png";
private VBox vbox;
private Canvas canvas;
private GraphicsContext gc;
private SimpleIntegerProperty fontSize;
private ObservableList<Node> hboxChildrens;
private FontAction fontAction;

public enum FontAction {
INCREASE,
DECREASE
}

public PoliceSizeController(ObservableList<Node> hboxChildrens, SimpleIntegerProperty fontSize, FontAction fontAction) {
this.hboxChildrens = hboxChildrens;
vbox = new VBox();
canvas = new Canvas(35, 35);
this.fontAction = fontAction;
this.fontSize = fontSize;
this.initGraphic();
}

public void initGraphic() {
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(3));
gc = canvas.getGraphicsContext2D();
vbox.getChildren().add(canvas);
gc.setFill(Color.TRANSPARENT);
gc.fillRect(0, 0, 35, 35);
if (this.fontAction.equals(FontAction.INCREASE)) {
gc.drawImage(new Image(increaseIcon), 0, 0, 35, 35);
} else {
gc.drawImage(new Image(decreaseIconDisabled), 0, 0, 35, 35);
}

vbox.setOnMouseClicked(
(e) -> {
if (this.fontAction.equals(FontAction.INCREASE)) {
fontSize.set(fontSize.get() + 1);
} else {
if (fontSize.get() > 1) {
fontSize.set(fontSize.get() - 1);
}

}
});
this.fontSize.addListener((obs, oldValue, newValue) -> {
if (this.fontAction.equals(FontAction.DECREASE)) {
if (newValue.intValue() <= 1) {
this.gc.drawImage(new Image(decreaseIconDisabled), 0, 0, 35, 35);
}else{
this.gc.drawImage(new Image(decreaseIcon), 0, 0, 35, 35);
}
}
});
hboxChildrens.add(vbox);
}

}
Binary file added src/main/resources/images/decrease-font-disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/decrease-font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/increase-font-disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/increase-font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bb9a816

Please sign in to comment.