Skip to content

Commit

Permalink
Fix logic - and adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Dec 4, 2023
1 parent 6e4a677 commit 98e6f5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/main/java/org/jabref/gui/entryeditor/CommentsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,26 @@ public CommentsTab(PreferencesService preferences,
@Override
protected SequencedSet<Field> determineFieldsToShow(BibEntry entry) {
SequencedSet<Field> comments = new LinkedHashSet<>();
if (entryEditorPreferences.shouldShowUserCommentsFields()) {

// First comes the standard comment field
comments.add(StandardField.COMMENT);

// Also show comment field of the current user (if enabled in the preferences)
if (entry.hasField(userSpecificCommentField) || entryEditorPreferences.shouldShowUserCommentsFields()) {
comments.add(userSpecificCommentField);
}
comments.add(StandardField.COMMENT);

// Show all non-empty comment fields (otherwise, they are completely hidden)
comments.addAll(entry.getFields().stream()
.filter(field -> (field instanceof UserSpecificCommentField && entryEditorPreferences.shouldShowUserCommentsFields()) ||
field.getName().toLowerCase().contains("comment"))
.sorted(Comparator.comparing(Field::getName))
.collect(Collectors.toCollection(LinkedHashSet::new)));
.filter(field -> (field instanceof UserSpecificCommentField && !field.equals(userSpecificCommentField))
|| field.getName().toLowerCase().contains("comment"))
.sorted(Comparator.comparing(Field::getName))
.collect(Collectors.toCollection(LinkedHashSet::new)));
return comments;
}

/**
* Comment editors: thre times size of button
* Comment editors: three times size of button
*/
private void setCompressedRowLayout() {
int numberOfComments = gridPane.getRowCount() - 1;
Expand Down Expand Up @@ -118,21 +124,20 @@ private void setCompressedRowLayout() {
protected void setupPanel(BibEntry entry, boolean compressed) {
super.setupPanel(entry, compressed);

boolean hasDefaultOwnerField = false;

Optional<FieldEditorFX> fieldEditorForUserDefinedComment = editors.entrySet().stream().filter(f -> f.getKey().getName().contains(defaultOwner)).map(Map.Entry::getValue).findFirst();
for (Map.Entry<Field, FieldEditorFX> fieldEditorEntry : editors.entrySet()) {
Field field = fieldEditorEntry.getKey();
FieldEditorFX editor = fieldEditorEntry.getValue();

boolean isStandardBibtexComment = field == StandardField.COMMENT;
boolean isDefaultOwnerComment = field.getName().contains(defaultOwner);
hasDefaultOwnerField = hasDefaultOwnerField || isDefaultOwnerComment;
boolean isDefaultOwnerComment = field.equals(userSpecificCommentField);
boolean shouldBeEnabled = isStandardBibtexComment || isDefaultOwnerComment;
editor.getNode().setDisable(!shouldBeEnabled);
}

if (hasDefaultOwnerField) {
// Show "Hide" button only if user-specific comment field is empty. Otherwise, it is a strange UI, because the
// button would just disappear and no change **in the current** editor would be made
if (entryEditorPreferences.shouldShowUserCommentsFields() && !entry.hasField(userSpecificCommentField)) {
Button hideDefaultOwnerCommentButton = new Button(Localization.lang("Hide user comments"));
hideDefaultOwnerCommentButton.setOnAction(e -> {
var labelForField = gridPane.getChildren().stream().filter(s -> s instanceof FieldNameLabel).filter(x -> ((FieldNameLabel) x).getText().equals(userSpecificCommentField.getDisplayName())).findFirst();
Expand Down
32 changes: 31 additions & 1 deletion src/test/java/org/jabref/gui/entryeditor/CommentsTabTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testfx.framework.junit5.ApplicationExtension;
Expand Down Expand Up @@ -82,6 +84,7 @@ void setUp() {
when(preferences.getOwnerPreferences()).thenReturn(ownerPreferences);
when(ownerPreferences.getDefaultOwner()).thenReturn(ownerName);
when(preferences.getEntryEditorPreferences()).thenReturn(entryEditorPreferences);
when(entryEditorPreferences.shouldShowUserCommentsFields()).thenReturn(true);
when(databaseContext.getMode()).thenReturn(BibDatabaseMode.BIBLATEX);
BibEntryType entryTypeMock = mock(BibEntryType.class);
when(entryTypesManager.enrich(any(), any())).thenReturn(Optional.of(entryTypeMock));
Expand All @@ -101,8 +104,35 @@ void setUp() {
}

@Test
void testDetermineFieldsToShowWorksForASingleUser() {
void emptyCommentShownIfGloballyEnabled() {
final UserSpecificCommentField ownerComment = new UserSpecificCommentField(ownerName);
when(entryEditorPreferences.shouldShowUserCommentsFields()).thenReturn(true);

BibEntry entry = new BibEntry(StandardEntryType.Book)
.withField(StandardField.COMMENT, "Standard comment text");

SequencedSet<Field> fields = commentsTab.determineFieldsToShow(entry);

assertEquals(Set.of(StandardField.COMMENT, ownerComment), fields);
}

@Test
void emptyCommentFieldNotShownIfGloballyDisabled() {
when(entryEditorPreferences.shouldShowUserCommentsFields()).thenReturn(false);

BibEntry entry = new BibEntry(StandardEntryType.Book)
.withField(StandardField.COMMENT, "Standard comment text");

SequencedSet<Field> fields = commentsTab.determineFieldsToShow(entry);

assertEquals(Set.of(StandardField.COMMENT), fields);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void commentFieldShownIfContainsText(boolean shouldShowUserCommentsFields) {
final UserSpecificCommentField ownerComment = new UserSpecificCommentField(ownerName);
when(entryEditorPreferences.shouldShowUserCommentsFields()).thenReturn(shouldShowUserCommentsFields);

BibEntry entry = new BibEntry(StandardEntryType.Book)
.withField(StandardField.COMMENT, "Standard comment text")
Expand Down

0 comments on commit 98e6f5f

Please sign in to comment.