Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 11102 by allowing users to add local bst files to preview layout list #11234

Merged
merged 31 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
070e687
Update PreviewTab.fxml
sahilsekr42 Apr 21, 2024
dbfc138
Update PreviewTab.java
sahilsekr42 Apr 21, 2024
293b777
Update PreviewTabViewModel.java
sahilsekr42 Apr 21, 2024
a46bee8
fix undeclared variable
sahilsekr42 Apr 21, 2024
0126f39
Update CHANGELOG.md for committed changes for issue 11102
sahilsekr42 Apr 21, 2024
5a5be03
Update CHANGELOG.md for fixing markdown spacing error
sahilsekr42 Apr 21, 2024
589eb1c
build.gradle openrewrite
sahilsekr42 Apr 21, 2024
cdabc1f
fix gradle errors
sahilsekr42 Apr 21, 2024
a54ecc2
Update PreviewTabViewModel.java for bstlayout getsource() case handling
sahilsekr42 Apr 22, 2024
d782e04
fix import errors
sahilsekr42 Apr 22, 2024
212f7ef
better architecture
Siedlerchr Apr 22, 2024
2a4db3b
test ieeetran with test entry
Siedlerchr Apr 22, 2024
1412d89
fix
Siedlerchr Apr 22, 2024
41d357d
Fix path to IEEEtran.bst
koppor Apr 25, 2024
730fbc7
Streamline conversion to BstEntry
koppor Apr 25, 2024
7a131e7
refactor file chooser
Siedlerchr Apr 25, 2024
8b04f6f
checkstyle
Siedlerchr Apr 25, 2024
04969c3
Somre more traces
koppor Apr 25, 2024
e82de74
Merge branch 'fix-11102' of https://github.com/sahilsekr42/jabref int…
koppor Apr 25, 2024
995856d
Add ''
koppor Apr 25, 2024
d1499b0
Merge branch 'main' into fix-11102
koppor May 19, 2024
efb9e19
checkstyle
Siedlerchr May 19, 2024
773faa4
checkstyle
Siedlerchr May 19, 2024
b1ece3b
Merge remote-tracking branch 'upstream/main' into fix-11102
Siedlerchr May 20, 2024
1a0a23a
move button up
Siedlerchr May 20, 2024
83b4cbb
Fix storing of style in Preferences
Siedlerchr May 20, 2024
380aa6b
wording of changelog
Siedlerchr May 20, 2024
a3aaf3f
cehckstyle
Siedlerchr May 20, 2024
574c87d
checkstyle
Siedlerchr May 20, 2024
57aa305
fix line break
Siedlerchr May 20, 2024
225dab0
damn style
Siedlerchr May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added tooltip on main table cells that shows cell content or cell content and entry preview if set in preferences. [10925](https://github.com/JabRef/jabref/issues/10925)
- We added the ability to add a keyword/crossref when typing the separator character (e.g., comma) in the keywords/crossref fields. [#11178](https://github.com/JabRef/jabref/issues/11178)
- We added an exporter and improved the importer for Endnote XML format. [#11137](https://github.com/JabRef/jabref/issues/11137)
- We added a button in gui to give users an easy option to select and add local bst files to available styles in preview layout list. [#11102](https://github.com/JabRef/jabref/issues/11102)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<CheckBox fx:id="showAsTabCheckBox" text="%Show preview as a tab in entry editor"/>
<CheckBox fx:id="showPreviewTooltipCheckBox" text="Show preview in entry table tooltip"/>
<HBox spacing="4.0">
<Button fx:id="bstFileButton" text="Select BST File" onAction="#selectBstFile" />
<VBox spacing="4.0" HBox.hgrow="ALWAYS">
<Label text="%Available" styleClass="sectionHeader"/>
<CustomTextField fx:id="searchBox" promptText="%Filter" VBox.vgrow="NEVER" prefHeight="20.0"/>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.jabref.gui.preferences.preview;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import javafx.application.Platform;
import javafx.beans.property.ListProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
Expand All @@ -18,6 +21,7 @@
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.stage.FileChooser;

import org.jabref.gui.Globals;
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
import org.jabref.gui.StateManager;
Expand All @@ -32,6 +36,7 @@
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.IconValidationDecorator;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.bst.BstPreviewLayout;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.preview.PreviewLayout;
import org.jabref.logic.util.TestEntry;
Expand Down Expand Up @@ -104,6 +109,20 @@ public String getTabName() {
return Localization.lang("Entry preview");
}

@FXML
private void selectBstFile(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select BST File");
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("BST Files", "*.bst"));

File selectedFile = fileChooser.showOpenDialog(null);
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
if (selectedFile != null) {
String filePath = selectedFile.getAbsolutePath();
BstPreviewLayout bstPreviewLayout = new BstPreviewLayout(Path.of(filePath));
viewModel.availableListProperty().add(bstPreviewLayout);
}
}

public void initialize() {
this.viewModel = new PreviewTabViewModel(dialogService, preferencesService.getPreviewPreferences(), taskExecutor, stateManager);
lastKeyPressTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
/**
* This class is Preferences -> Entry Preview tab model
* <p>
* {@link PreviewTab} is the controller of Entry Preview tab
* {@link PreviewTab} is the controller of Entry Preview tab
* </p>
*
* @see PreviewTab
* */
*/
public class PreviewTabViewModel implements PreferenceTabViewModel {

private static final Logger LOGGER = LoggerFactory.getLogger(PreviewTabViewModel.class);
Expand Down Expand Up @@ -103,10 +103,10 @@ public PreviewTabViewModel(DialogService dialogService,
chosenListProperty,
input -> !chosenListProperty.getValue().isEmpty(),
ValidationMessage.error("%s > %s %n %n %s".formatted(
Localization.lang("Entry preview"),
Localization.lang("Selected"),
Localization.lang("Selected Layouts can not be empty")
)
Localization.lang("Entry preview"),
Localization.lang("Selected"),
Localization.lang("Selected Layouts can not be empty")
)
)
);
}
Expand Down Expand Up @@ -153,13 +153,13 @@ public void setPreviewLayout(PreviewLayout selectedLayout) {
Localization.lang("Parsing error") + ": " + Localization.lang("illegal backslash expression"), exception);
}

if (selectedLayout instanceof TextBasedPreviewLayout layout) {
sourceTextProperty.setValue(layout.getText().replace("__NEWLINE__", "\n"));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
selectedIsEditableProperty.setValue(true);
} else {
sourceTextProperty.setValue(((CitationStylePreviewLayout) selectedLayout).getSource());
selectedIsEditableProperty.setValue(false);
}
boolean isEditingAllowed = selectedLayout instanceof TextBasedPreviewLayout;
setContentForPreview(selectedLayout.getText(), isEditingAllowed);
}

private void setContentForPreview(String text, boolean editable) {
sourceTextProperty.setValue(text);
selectedIsEditableProperty.setValue(editable);
}

public void refreshPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private List<String> generateCitations() throws IOException {
PreviewLayout previewLayout = preferencesService.getPreviewPreferences().getSelectedPreviewLayout();

if (previewLayout instanceof CitationStylePreviewLayout citationStyleLayout) {
styleSource = citationStyleLayout.getSource();
styleSource = citationStyleLayout.getText();
}

if (styleSource != null) {
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/jabref/logic/bst/BstPreviewLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.bst;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -17,16 +18,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BstPreviewLayout implements PreviewLayout {
public final class BstPreviewLayout implements PreviewLayout {

private static final Logger LOGGER = LoggerFactory.getLogger(BstPreviewLayout.class);

private final String name;

private String source;
private BstVM bstVM;
private String error;

public BstPreviewLayout(Path path) {
try {
this.source = String.join("\n", Files.readAllLines(path));
} catch (IOException e) {
LOGGER.error("Error reading file", e);
this.source = "";
}

name = path.getFileName().toString();
if (!Files.exists(path)) {
LOGGER.error("File {} not found", path.toAbsolutePath());
Expand Down Expand Up @@ -85,4 +93,9 @@ public String getDisplayName() {
public String getName() {
return name;
}

@Override
public String getText() {
return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;

public class CitationStylePreviewLayout implements PreviewLayout {
public final class CitationStylePreviewLayout implements PreviewLayout {
private final CitationStyle citationStyle;
private final BibEntryTypesManager bibEntryTypesManager;

Expand All @@ -24,7 +24,8 @@ public String getDisplayName() {
return citationStyle.getTitle();
}

public String getSource() {
@Override
public String getText() {
return citationStyle.getSource();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Implements the preview based JabRef's <a href="https://docs.jabref.org/import-export/export/customexports">Custom export filters</a>.
*/
public class TextBasedPreviewLayout implements PreviewLayout {
public final class TextBasedPreviewLayout implements PreviewLayout {
public static final String NAME = "PREVIEW";

private static final Logger LOGGER = LoggerFactory.getLogger(TextBasedPreviewLayout.class);
Expand Down Expand Up @@ -54,8 +54,9 @@ public String generatePreview(BibEntry entry, BibDatabaseContext databaseContext
}
}

@Override
public String getText() {
return text;
return text.replace("__NEWLINE__", "\n");
}

@Override
Expand All @@ -67,4 +68,5 @@ public String getName() {
public String getDisplayName() {
return Localization.lang("Customized preview style");
}

}
7 changes: 6 additions & 1 deletion src/main/java/org/jabref/logic/preview/PreviewLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import java.util.Locale;

import org.jabref.logic.bst.BstPreviewLayout;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.layout.TextBasedPreviewLayout;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

/**
* Used for displaying a rendered entry in the UI. Due to historical reasons, "rendering" is called "layout".
*/
public interface PreviewLayout {
public sealed interface PreviewLayout permits BstPreviewLayout, CitationStylePreviewLayout, TextBasedPreviewLayout {

String generatePreview(BibEntry entry, BibDatabaseContext databaseContext);

String getDisplayName();

String getText();

String getName();

default boolean containsCaseIndependent(String searchTerm) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/jabref/logic/bst/BstVMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import org.jabref.logic.util.TestEntry;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
Expand Down Expand Up @@ -42,6 +43,19 @@ public void abbrv() throws RecognitionException, IOException {
result.replaceAll("\\s", ""));
}

@Test
public void ieeetran() throws RecognitionException, IOException {
BstVM vm = new BstVM(Path.of("src/test/resources/org/jabref/logic/bst/IEEEtran.bst"));
List<BibEntry> testEntries = List.of(TestEntry.getTestEntry());

String expected = "\\begin{thebibliography}{1}\\bibitem{canh05}K.~Crowston, H.~Annabi, J.~Howison, and C.~Masango.\\newblock Effective work practices for floss development: A model and propositions.\\newblock In {\\em Hawaii International Conference On System Sciences (HICSS)}, 2005.\\end{thebibliography}";
String result = vm.render(testEntries);

assertEquals(
expected.replaceAll("\\s", ""),
result.replaceAll("\\s", ""));
}

@Test
public void simple() throws RecognitionException {
BstVM vm = new BstVM("""
Expand Down
Loading