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 29 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 @@ -19,6 +19,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- Added a formatter to remove word enclosing braces. [#11222](https://github.com/JabRef/jabref/issues/11222)
- 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 support for using BibTeX Style files (BST) in the Preview. [#11102](https://github.com/JabRef/jabref/issues/11102)
- We added support for automatically update LaTeX citations when a LaTeX file is created, removed, or modified. [#10585](https://github.com/JabRef/jabref/issues/10585)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Label text="%Current Preview" styleClass="titleHeader"/>
<CheckBox fx:id="showAsTabCheckBox" text="%Show preview as a tab in entry editor"/>
<CheckBox fx:id="showPreviewTooltipCheckBox" text="Show preview in entry table tooltip"/>
<Button fx:id="bstFileButton" text="%Add BST file" onAction="#selectBstFile" />
<HBox spacing="4.0">
<VBox spacing="4.0" HBox.hgrow="ALWAYS">
<Label text="%Available" styleClass="sectionHeader"/>
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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 @@ -19,21 +20,23 @@
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;

import org.jabref.gui.Globals;
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.preferences.AbstractPreferenceTabView;
import org.jabref.gui.preferences.PreferencesTab;
import org.jabref.gui.preview.PreviewViewer;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.util.IconValidationDecorator;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.preview.PreviewLayout;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.TestEntry;
import org.jabref.model.database.BibDatabaseContext;

Expand Down Expand Up @@ -63,6 +66,7 @@ public class PreviewTab extends AbstractPreferenceTabView<PreviewTabViewModel> i

@Inject private StateManager stateManager;
@Inject private ThemeManager themeManager;
@Inject private KeyBindingRepository keyBindingRepository;

private final ContextMenu contextMenu = new ContextMenu();

Expand All @@ -89,10 +93,14 @@ public EditAction(StandardActions command) {
public void execute() {
if (editArea != null) {
switch (command) {
case COPY -> editArea.copy();
case CUT -> editArea.cut();
case PASTE -> editArea.paste();
case SELECT_ALL -> editArea.selectAll();
case COPY ->
editArea.copy();
case CUT ->
editArea.cut();
case PASTE ->
editArea.paste();
case SELECT_ALL ->
editArea.selectAll();
}
editArea.requestFocus();
}
Expand All @@ -104,6 +112,19 @@ public String getTabName() {
return Localization.lang("Entry preview");
}

@FXML
private void selectBstFile(ActionEvent event) {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.BST)
.withDefaultExtension(StandardFileType.BST)
.withInitialDirectory(preferencesService.getFilePreferences().getWorkingDirectory())
.build();

dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(bstFile -> {
viewModel.addBstStyle(bstFile);
});
}

public void initialize() {
this.viewModel = new PreviewTabViewModel(dialogService, preferencesService.getPreviewPreferences(), taskExecutor, stateManager);
lastKeyPressTime = System.currentTimeMillis();
Expand All @@ -114,7 +135,7 @@ public void initialize() {
searchBox.setPromptText(Localization.lang("Search") + "...");
searchBox.setLeft(IconTheme.JabRefIcons.SEARCH.getGraphicNode());

ActionFactory factory = new ActionFactory(Globals.getKeyPrefs());
ActionFactory factory = new ActionFactory(keyBindingRepository);
contextMenu.getItems().addAll(
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT)),
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.preferences.preview;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -33,6 +34,7 @@
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.bst.BstPreviewLayout;
import org.jabref.logic.citationstyle.CitationStyle;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.l10n.Localization;
Expand All @@ -52,11 +54,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 All @@ -70,6 +72,8 @@ public class PreviewTabViewModel implements PreferenceTabViewModel {
private final ListProperty<PreviewLayout> chosenListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
private final ObjectProperty<MultipleSelectionModel<PreviewLayout>> chosenSelectionModelProperty = new SimpleObjectProperty<>(new NoSelectionModel<>());

private final ListProperty<Path> bstStylesPaths = new SimpleListProperty<>(FXCollections.observableArrayList());

private final BooleanProperty selectedIsEditableProperty = new SimpleBooleanProperty(false);
private final ObjectProperty<PreviewLayout> selectedLayoutProperty = new SimpleObjectProperty<>();
private final StringProperty sourceTextProperty = new SimpleStringProperty("");
Expand Down Expand Up @@ -103,10 +107,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 @@ -135,6 +139,12 @@ public void setValues() {
dialogService.showErrorDialogAndWait(Localization.lang("Error adding discovered CitationStyles"), ex);
})
.executeWith(taskExecutor);
bstStylesPaths.clear();
bstStylesPaths.addAll(previewPreferences.getBstPreviewLayoutPaths());
bstStylesPaths.forEach(path -> {
BstPreviewLayout layout = new BstPreviewLayout(path);
availableListProperty.add(layout);
});
}

public void setPreviewLayout(PreviewLayout selectedLayout) {
Expand All @@ -153,13 +163,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 Expand Up @@ -194,10 +204,11 @@ public void storeSettings() {
previewPreferences.setShowPreviewAsExtraTab(showAsExtraTabProperty.getValue());
previewPreferences.setShowPreviewEntryTableTooltip(showPreviewInEntryTableTooltip.getValue());
previewPreferences.setCustomPreviewLayout((TextBasedPreviewLayout) customLayout);
previewPreferences.setBstPreviewLayoutPaths(bstStylesPaths);

if (!chosenSelectionModelProperty.getValue().getSelectedItems().isEmpty()) {
previewPreferences.setLayoutCyclePosition(chosenListProperty.getValue().indexOf(
chosenSelectionModelProperty.getValue().getSelectedItems().get(0)));
chosenSelectionModelProperty.getValue().getSelectedItems().getFirst()));
}
}

Expand Down Expand Up @@ -483,4 +494,11 @@ public ObjectProperty<PreviewLayout> selectedLayoutProperty() {
public StringProperty sourceTextProperty() {
return sourceTextProperty;
}

public void addBstStyle(Path bstFile) {
BstPreviewLayout bstPreviewLayout = new BstPreviewLayout(bstFile);
bstStylesPaths.add(bstFile);
availableListProperty().add(bstPreviewLayout);
chosenListProperty().add(bstPreviewLayout);
}
}
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
12 changes: 8 additions & 4 deletions src/main/java/org/jabref/logic/bst/BstFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void bstEquals(BstVMVisitor visitor, ParserRuleContext ctx) {
}

/**
* Pops the top two (integer) literals and pushes their sum.
* Pops the top two (integer) literals and pushes their sum.
*/
private void bstAdd(BstVMVisitor visitor, ParserRuleContext ctx) {
if (stack.size() < 2) {
Expand Down Expand Up @@ -306,7 +306,7 @@ private void bstAddPeriod(BstVMVisitor visitor, ParserRuleContext ctx) {
}

/**
* Executes the function whose name is the entry type of an entry.
* Executes the function whose name is the entry type of entry.
* For example if an entry is of type book, this function executes
* the book function. When given as an argument to the ITERATE
* command, call.type$ actually produces the output for the entries.
Expand All @@ -328,6 +328,10 @@ public void execute(BstVMVisitor visitor, ParserRuleContext ctx, BstEntry bstEnt
} else {
String entryType = bstEntry.entry.getType().getName();
LOGGER.trace("Handling {}", entryType);
if (!functions.containsKey(entryType)) {
LOGGER.error("Function for {} not found ", entryType);
return;
}
functions.get(entryType).execute(visitor, ctx, bstEntry);
}
}
Expand Down Expand Up @@ -443,7 +447,7 @@ private void bstEmpty(BstVMVisitor visitor, ParserRuleContext ctx) {
throw new BstVMException("Operand does not match function empty$ (line %d)".formatted(ctx.start.getLine()));
}

boolean result = "".equals(s.trim());
boolean result = s.trim().isEmpty();
LOGGER.trace("empty$({}) result: {}", s, result);
stack.push(result ? BstVM.TRUE : BstVM.FALSE);
}
Expand Down Expand Up @@ -753,7 +757,7 @@ private void bstSwap(BstVMVisitor visitor, ParserRuleContext ctx) {
* precisely, a "special character", defined in Section 4) counts as
* a single text character, even if it's missing its matching right
* brace, and where braces don't count as text characters.
*
* <p>
* From BibTeXing: For the purposes of counting letters in labels,
* BibTEX considers everything contained inside the braces as a
* single letter.
Expand Down
25 changes: 23 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 @@ -11,22 +12,30 @@
import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter;
import org.jabref.logic.layout.format.RemoveTilde;
import org.jabref.logic.preview.PreviewLayout;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

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 +94,16 @@ public String getDisplayName() {
public String getName() {
return name;
}

@Override
public String getText() {
return source;
}

/**
* Checks if the given style file is a BST file by checking the extension
*/
public static boolean isBstStyleFile(String styleFile) {
return StandardFileType.BST.getExtensions().stream().anyMatch(styleFile::endsWith);
}
}
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 Down
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
Loading
Loading