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 26 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 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)
- 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
35 changes: 29 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,24 @@
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.bst.BstPreviewLayout;

Check failure on line 37 in src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Unused import - org.jabref.logic.bst.BstPreviewLayout. Raw Output: /github/workspace/./src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java:37:8: error: Unused import - org.jabref.logic.bst.BstPreviewLayout. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
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 +67,7 @@

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

private final ContextMenu contextMenu = new ContextMenu();

Expand All @@ -89,10 +94,14 @@
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 +113,20 @@
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 -> {

Check failure on line 125 in src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 There is more than 1 empty line after this line. Raw Output: /github/workspace/./src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java:125:66: error: There is more than 1 empty line after this line. (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)
viewModel.addBstStyle(bstFile);
});
}

public void initialize() {
this.viewModel = new PreviewTabViewModel(dialogService, preferencesService.getPreviewPreferences(), taskExecutor, stateManager);
lastKeyPressTime = System.currentTimeMillis();
Expand All @@ -114,7 +137,7 @@
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 All @@ -11,6 +12,7 @@
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;

Check failure on line 15 in src/main/java/org/jabref/gui/preferences/preview/PreviewTabViewModel.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Unused import - javafx.beans.property.Property. Raw Output: /github/workspace/./src/main/java/org/jabref/gui/preferences/preview/PreviewTabViewModel.java:15:8: error: Unused import - javafx.beans.property.Property. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand All @@ -33,6 +35,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 +55,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 +73,8 @@
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 +108,10 @@
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 All @@ -123,6 +128,7 @@
availableListProperty.getValue().add(previewPreferences.getCustomPreviewLayout());
}


BackgroundTask.wrap(CitationStyle::discoverCitationStyles)
.onSuccess(styles -> styles.stream()
.map(style -> new CitationStylePreviewLayout(style, Globals.entryTypesManager))
Expand All @@ -135,6 +141,12 @@
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 +165,13 @@
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 +206,11 @@
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 +496,11 @@
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
Loading
Loading