Skip to content

Commit

Permalink
Merge 8a5ae04 into 62cdfb5
Browse files Browse the repository at this point in the history
  • Loading branch information
JeridiOmar authored Apr 10, 2023
2 parents 62cdfb5 + 8a5ae04 commit 5ede3b9
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package components.interviewPanel.Controllers;

import application.configuration.Configuration;
import components.interviewPanel.ContextMenus.ContextMenuFactory;
import components.interviewPanel.search.ButtonSearchType;
import components.interviewPanel.search.SearchButtonHandler;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.control.IndexRange;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Popup;
import models.*;
import org.fxmisc.flowless.VirtualizedScrollPane;
Expand All @@ -32,6 +42,7 @@ public class RichTextAreaController {
private final HashSet<Moment> emphasizedMoments = new HashSet<>(); // used temporary when over a descripteme
private ContextMenuFactory contextMenuFactory;
private final List<AnnotationColor> annotationColorList;
private SearchResult searchResult;

public RichTextAreaController(InterviewText interviewText, List<AnnotationColor> annotationColorList) {
this.interviewText = interviewText;
Expand All @@ -43,22 +54,111 @@ public RichTextAreaController(InterviewText interviewText, List<AnnotationColor>
area.setParagraphGraphicFactory(LineNumberFactory.get(area));
area.appendText(interviewText.getText());
area.setShowCaret(Caret.CaretVisibility.ON);

this.searchResult = new SearchResult(this.interviewText.getText());
setUpClick();
setUpPopUp();

bind();

// Watch for new descriptemes
getGlobalVariables()
.getDescriptemeChangedProperty()
.addListener(newValue -> this.updateDescripteme());

// Initialize view annotation
//interviewText.getAnnotationsProperty().forEach(annotation -> applyStyle(annotation.getStartIndex(), annotation.getEndIndex()));
applyStyleInitialize();
//Add ctrl+F Listener to launch the research
area.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.F && e.isControlDown()) {
showFindDialog(area);
}
});
}

private void showFindDialog(InlineCssTextArea richTextArea) {
// Set up the dialog
Dialog<String> dialog = new Dialog<>();
dialog.getDialogPane().setPrefWidth(450);
dialog.setResizable(true);
dialog.setTitle(Configuration.langBundle.getString("find"));
dialog.setHeaderText(Configuration.langBundle.getString("find"));
dialog.setResizable(false);

// Set up the buttons
ButtonType findPreviousButtonType = new ButtonType(Configuration.langBundle.getString("previous"), ButtonBar.ButtonData.OK_DONE);
ButtonType findNextButtonType = new ButtonType(Configuration.langBundle.getString("next"), ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(findNextButtonType, findPreviousButtonType, ButtonType.CLOSE);

// Set up the text field and label
TextField findTextField = new TextField();
Label findLabel = new Label(Configuration.langBundle.getString("find"));
findLabel.setLabelFor(findTextField);

// Set up the match count label
Label matchCountLabel = new Label();
matchCountLabel.setVisible(false);

// Set up the grid pane
HBox gridPane = new HBox();
gridPane.setSpacing(15);
gridPane.setAlignment(Pos.BASELINE_CENTER);
gridPane.getChildren().add(findLabel);
gridPane.getChildren().add(findTextField);
gridPane.getChildren().add(matchCountLabel);
dialog.getDialogPane().setContent(gridPane);

Button findPreviousButton = (Button) dialog.getDialogPane().lookupButton(findPreviousButtonType);
Button findNextButton = (Button) dialog.getDialogPane().lookupButton(findNextButtonType);
//Init Buttons on disabled
findNextButton.setDisable(true);
findPreviousButton.setDisable(true);

//handle search result label
findTextField.textProperty().addListener((obs, oldText, newText) -> {
if (!newText.isEmpty()) {
this.searchResult.countOccurrences(this.interviewText.getText(), newText);
matchCountLabel.setVisible(true);

matchCountLabel.textProperty().bind(Bindings.createStringBinding(() -> {
String s = "";
int resultCount = this.searchResult.getResultCount();
s += resultCount + " ";
s += Configuration.langBundle.getString("matches_found") + ".";
return s;
}, this.searchResult.resultCountProperty()));
} else {
this.searchResult.resetSearch();
matchCountLabel.setVisible(false);
}

});


//Handle buttons disabled property listeners
this.searchResult.resultCountProperty().addListener((obs, oldCount, newCount) -> {
findNextButton.setDisable(newCount.intValue() <= 0);
});
this.searchResult.resultPositionProperty().addListener((obs, oldCount, newCount) -> {
if (!this.searchResult.hasNext()) {
findNextButton.setDisable(true);
} else if (!this.searchResult.hasPrevious()) {
findPreviousButton.setDisable(true);
} else {
findNextButton.setDisable(false);
findPreviousButton.setDisable(false);
}
});
//Set up search buttons actions
findNextButton.addEventFilter(ActionEvent.ACTION, new SearchButtonHandler(
ButtonSearchType.NEXT, searchResult, richTextArea));
findPreviousButton.addEventFilter(ActionEvent.ACTION, new SearchButtonHandler(
ButtonSearchType.PREVIOUS, searchResult, richTextArea));

// Show the dialog and reset the search result
dialog.setOnCloseRequest(e -> {
this.searchResult.resetSearch();
});
dialog.show();
}


public void setContextMenuFactory(ContextMenuFactory contextMenuFactory) {
this.contextMenuFactory = contextMenuFactory;
}
Expand Down Expand Up @@ -152,10 +252,10 @@ private void setUpPopUp() {
descripteme.getEmphasizeProperty().set(true);
emphasizedMoments.addAll(getGlobalVariables().getMomentsByDescripteme(descripteme));
}
for(Moment moment : emphasizedMoments) {
for (Moment moment : emphasizedMoments) {
moment.getEmphasizeProperty().set(true);
}
message += + emphasizedMoments.size() + " moment(s)";
message += +emphasizedMoments.size() + " moment(s)";

popupMsg.setText(message);
popup.show(area, pos.getX(), pos.getY() + 10);
Expand All @@ -172,7 +272,7 @@ private void setUpPopUp() {
emphasizedDescriptemes.clear();
}
if (!emphasizedMoments.isEmpty()) {
for(Moment moment : emphasizedMoments) {
for (Moment moment : emphasizedMoments) {
moment.getEmphasizeProperty().set(false);
}
emphasizedMoments.clear();
Expand Down Expand Up @@ -206,8 +306,8 @@ private void applyStyleInitialize() {
}

private void applyStyle(int start, int end) {
for (int i = start ; i < end ; i++) {
area.setStyle(i, i+1, getCSS(i));
for (int i = start; i < end; i++) {
area.setStyle(i, i + 1, getCSS(i));
}
}

Expand All @@ -229,7 +329,7 @@ private String getCSS(int i) {
}
css += "-rtfx-underline-color: black; " + "-rtfx-underline-width: " + size + ";";
boolean isRevealed = false;
for (Descripteme descripteme: descriptemes) {
for (Descripteme descripteme : descriptemes) {
if (descripteme.getRevealedProperty().get()) {
isRevealed = true;
break;
Expand Down Expand Up @@ -283,8 +383,7 @@ private void bindDescripteme(Descripteme descripteme, boolean bind) {
descripteme.endIndexProperty().addListener(listenerEndIndex);
descripteme.getRevealedProperty().addListener(listenerRevealed);
descripteme.getTriggerScrollReveal().addListener(listenerScrollToTrigger);
}
else {
} else {
descripteme.getRevealedProperty().removeListener(listenerStartIndex);
descripteme.getRevealedProperty().removeListener(listenerEndIndex);
descripteme.getRevealedProperty().removeListener(listenerRevealed);
Expand Down Expand Up @@ -318,24 +417,22 @@ public void updateContextMenu() {
descriptemes,
annotation)
);
}
else if (annotation != null) {
} else if (annotation != null) {
area.setContextMenu(contextMenuFactory.getContextMenuAnnotation(
area.getSelectedText(),
annotation)
);
}
else if (!descriptemes.isEmpty()) {
} else if (!descriptemes.isEmpty()) {
area.setContextMenu(contextMenuFactory.getContextMenuDescripteme(
area.getSelectedText(),
descriptemes)
);
}
else if (!area.getSelectedText().isEmpty()) {
} else if (!area.getSelectedText().isEmpty()) {
area.setContextMenu(contextMenuFactory.getContextMenuSelection(
area.getSelectedText(),
area.getSelection())
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package components.interviewPanel.search;

public enum ButtonSearchType {
PREVIOUS,
NEXT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package components.interviewPanel.search;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import models.SearchResult;
import org.fxmisc.richtext.InlineCssTextArea;

public class SearchButtonHandler implements EventHandler<ActionEvent> {

private ButtonSearchType type;
private SearchResult searchResult;
private InlineCssTextArea richTextArea;

public SearchButtonHandler(ButtonSearchType type, SearchResult searchResult, InlineCssTextArea richTextArea) {
this.type = type;
this.searchResult = searchResult;
this.richTextArea = richTextArea;
}

@Override
public void handle(ActionEvent event) {
if (this.searchResult.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Find");
alert.setHeaderText("Find");
alert.setContentText("No matches found.");
alert.showAndWait();
event.consume(); // prevent the dialog from closing
return;
}
int currentPosition;

if (this.type.equals(ButtonSearchType.NEXT)) {
currentPosition = this.searchResult.getNextResult();
} else {
currentPosition = this.searchResult.getPreviousResult();
}
richTextArea.requestFocus();
richTextArea.moveTo(currentPosition);
richTextArea.requestFollowCaret();
richTextArea.selectRange(currentPosition, currentPosition + this.searchResult.getCurrentSearchWord().length());
event.consume(); // prevent the dialog from closing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public class ModelisationSpaceController extends ScrollOnDragPane implements Ini
private ScrollPaneCommandFactory paneCmdFactory;
private RootMomentController rmController;

private @FXML AnchorPane pane;
private @FXML ScrollOnDragPane superPane;
private @FXML
AnchorPane pane;
private @FXML
ScrollOnDragPane superPane;

AnchorPane anchorPane = new AnchorPane(); // Container

Expand Down Expand Up @@ -58,54 +60,56 @@ public void initialize(URL location, ResourceBundle resources) {
public void setRootMoment(RootMoment m) {
//Set new moment
clearSpace();
if(m != null) {
if (m != null) {
rmController = new RootMomentController(m, paneCmdFactory, hooksNotifier);
anchorPane.getChildren().clear();
anchorPane.getChildren().add(RootMomentController.createRootMoment(rmController));
superPane.setContent(anchorPane);
double r = AppSettings.zoomLevelProperty.getValue() * 0.01;
anchorPane.getTransforms().setAll(new Scale(r, r,0, 0));
anchorPane.getTransforms().setAll(new Scale(r, r, 0, 0));
AppSettings.zoomLevelProperty.addListener((l) -> {
double ratio = AppSettings.zoomLevelProperty.getValue() * 0.01;
anchorPane.getTransforms().setAll(new Scale(ratio, ratio,0, 0));
anchorPane.getTransforms().setAll(new Scale(ratio, ratio, 0, 0));
});
}
this.setHvalue(this.superPane.getHmax());
}

public void clearSpace() {
if(rmController != null)
if (rmController != null)
rmController.unmount();
superPane.setContent(null);
}

public ModelisationSpaceHook getHooks() { return hooks; }
public ModelisationSpaceHook getHooks() {
return hooks;
}

private void setupDragAndDrop() {
superPane.setOnDragOverHook((e) -> {
if(
!rmController.hasAtLeastOneChildMoment()
&& DragStore.getDraggable().isDraggable()
&& !e.isAccepted()
&& DragStore.getDraggable().getDataFormat() == TemplateMoment.format
if (
!rmController.hasAtLeastOneChildMoment()
&& DragStore.getDraggable().isDraggable()
&& !e.isAccepted()
&& DragStore.getDraggable().getDataFormat() == TemplateMoment.format
) {
e.acceptTransferModes(TransferMode.COPY);
}
});

superPane.setOnDragDroppedHook(e -> {
System.out.println("DataFormat : " + DragStore.getDraggable().getDataFormat().toString());
if(
!rmController.hasAtLeastOneChildMoment()
&& e.isAccepted()
&& DragStore.getDraggable().getDataFormat() == TemplateMoment.format
if (
!rmController.hasAtLeastOneChildMoment()
&& e.isAccepted()
&& DragStore.getDraggable().getDataFormat() == TemplateMoment.format
) {
TemplateMoment t = DragStore.getDraggable();
rmController.addMoment(t.createConcreteMoment());
}
});
}

public ScrollOnDragPane getSuperPane(){
public ScrollOnDragPane getSuperPane() {
return this.superPane;
}
}
Loading

0 comments on commit 5ede3b9

Please sign in to comment.