Skip to content

Commit

Permalink
Enabling zero-ranking with one click on star
Browse files Browse the repository at this point in the history
Uses the position of the clicked star in the HBox to determine the clicked rank and compares it to the current value. If both are equal, the rank is set to zero.
  • Loading branch information
SebieF committed Nov 2, 2022
1 parent 0d7f855 commit 05a8b4f
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.swing.undo.UndoManager;

import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tooltip;
Expand All @@ -30,6 +31,7 @@

import com.tobiasdiez.easybind.EasyBind;
import org.controlsfx.control.Rating;
import org.kordamp.ikonli.javafx.FontIcon;

/**
* A column that displays a SpecialField
Expand Down Expand Up @@ -100,9 +102,26 @@ private Rating createSpecialRating(BibEntryTableViewModel entry, Optional<Specia
}

ranking.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {

if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
ranking.setRating(0);
event.consume();
} else if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 1) {
if(entry.getEntry().getField(SpecialField.RANKING).isPresent()) {
// Get the position of the clicked star (1-5)
var clickedStar = ((FontIcon) event.getTarget());
var parent = (HBox) clickedStar.getParent();
var positionOfStarInParent = parent.getChildren().indexOf(clickedStar) + 1;

// Compare clicked with previous rating
var clickedRating = SpecialFieldValue.getRating(positionOfStarInParent);
var previousRating = SpecialFieldValue.getRating(
Integer.parseInt(entry.getEntry().getField(SpecialField.RANKING).get().split("rank")[1]));
if (clickedRating.equals(previousRating)) {
ranking.setRating(0);
event.consume();
}
}
} else if (event.getButton().equals(MouseButton.SECONDARY)) {
event.consume();
}
Expand Down

0 comments on commit 05a8b4f

Please sign in to comment.