Skip to content

Commit

Permalink
Show preview also for available styles (#8110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr authored Oct 6, 2021
1 parent ec283c1 commit 74b5852
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a preference to Opt-In to JabRef's online metadata extraction service (Grobid) usage. [#8002](https://github.com/JabRef/jabref/pull/8002)
- We readded the possibility to display the search results of all databases ("Global Search"). It is shown in a separate window. [#4096](https://github.com/JabRef/jabref/issues/4096)
- We readded the possibility to keep the search string when switching tabs. It is implemented by a toggle button. [#4096](https://github.com/JabRef/jabref/issues/4096#issuecomment-575986882)
- We allowed the user to also preview the available citation styles in the preferences besides the selected ones [#8108](https://github.com/JabRef/jabref/issues/8108)
- We added an option to search the available citation styles by name in the preferences [#8108](https://github.com/JabRef/jabref/issues/8108)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
Expand All @@ -13,11 +14,18 @@
<?import org.jabref.gui.icon.JabRefIconView?>
<?import org.fxmisc.flowless.VirtualizedScrollPane?>
<?import org.fxmisc.richtext.CodeArea?>
<?import org.controlsfx.control.textfield.CustomTextField?>

<fx:root spacing="10.0" type="VBox"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.jabref.gui.preferences.preview.PreviewTab">
<Label text="%Current Preview" styleClass="titleHeader"/>
<CheckBox fx:id="showAsTabCheckBox" text="%Show preview as a tab in entry editor"/>
<CustomTextField fx:id="searchBox" promptText="%Filter" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0"/>
</VBox.margin>
</CustomTextField>
<HBox spacing="4.0">
<VBox spacing="4.0" HBox.hgrow="ALWAYS">
<Label text="%Available" styleClass="sectionHeader"/>
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/org/jabref/gui/preferences/preview/PreviewTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.preferences.AbstractPreferenceTabView;
import org.jabref.gui.preferences.PreferencesTab;
import org.jabref.gui.preview.PreviewViewer;
Expand All @@ -39,6 +40,7 @@
import com.airhacks.afterburner.views.ViewLoader;
import com.tobiasdiez.easybind.EasyBind;
import de.saxsys.mvvmfx.utils.validation.visualization.ControlsFxVisualizer;
import org.controlsfx.control.textfield.CustomTextField;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;

Expand All @@ -55,6 +57,7 @@ public class PreviewTab extends AbstractPreferenceTabView<PreviewTabViewModel> i
@FXML private Button resetDefaultButton;
@FXML private Tab previewTab;
@FXML private CodeArea editArea;
@FXML private CustomTextField searchBox;

@Inject private StateManager stateManager;

Expand Down Expand Up @@ -99,6 +102,9 @@ public String getTabName() {
}

public void initialize() {
searchBox.setPromptText(Localization.lang("Search") + "...");
searchBox.setLeft(IconTheme.JabRefIcons.SEARCH.getGraphicNode());

this.viewModel = new PreviewTabViewModel(dialogService, preferencesService, taskExecutor, stateManager);

lastKeyPressTime = System.currentTimeMillis();
Expand All @@ -115,7 +121,7 @@ public void initialize() {

showAsTabCheckBox.selectedProperty().bindBidirectional(viewModel.showAsExtraTabProperty());

availableListView.itemsProperty().bindBidirectional(viewModel.availableListProperty());
availableListView.setItems(viewModel.getFilteredPreviews());
viewModel.availableSelectionModelProperty().setValue(availableListView.getSelectionModel());
new ViewModelListCellFactory<PreviewLayout>()
.withText(PreviewLayout::getDisplayName)
Expand All @@ -125,6 +131,7 @@ public void initialize() {
availableListView.setOnDragDropped(event -> dragDropped(viewModel.availableListProperty(), event));
availableListView.setOnKeyTyped(event -> jumpToSearchKey(availableListView, event));
availableListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
availableListView.selectionModelProperty().getValue().selectedItemProperty().addListener((observable, oldValue, newValue) -> viewModel.setPreviewLayout(newValue));

chosenListView.itemsProperty().bindBidirectional(viewModel.chosenListProperty());
viewModel.chosenSelectionModelProperty().setValue(chosenListView.getSelectionModel());
Expand All @@ -147,15 +154,20 @@ public void initialize() {

previewTab.setContent(new PreviewViewer(new BibDatabaseContext(), dialogService, stateManager));
((PreviewViewer) previewTab.getContent()).setEntry(TestEntry.getTestEntry());

EasyBind.subscribe(viewModel.layoutProperty(), value -> ((PreviewViewer) previewTab.getContent()).setLayout(value));
previewTab.getContent().visibleProperty().bind(viewModel.chosenSelectionModelProperty().getValue().selectedItemProperty().isNotNull());
previewTab.getContent().visibleProperty().bind(viewModel.chosenSelectionModelProperty().getValue().selectedItemProperty().isNotNull()
.or(viewModel.availableSelectionModelProperty().getValue().selectedItemProperty().isNotNull()));
((PreviewViewer) previewTab.getContent()).setTheme(preferencesService.getTheme());

editArea.clear();
editArea.setParagraphGraphicFactory(LineNumberFactory.get(editArea));
editArea.setContextMenu(contextMenu);
editArea.visibleProperty().bind(viewModel.chosenSelectionModelProperty().getValue().selectedItemProperty().isNotNull());
viewModel.sourceTextProperty().addListener((observable, oldValue, newValue) -> editArea.replaceText(newValue));
editArea.visibleProperty().bind(viewModel.chosenSelectionModelProperty().getValue().selectedItemProperty().isNotNull()
.or(viewModel.availableSelectionModelProperty().getValue().selectedItemProperty().isNotNull()));
viewModel.sourceTextProperty().addListener((observable, oldValue, newValue) -> {
editArea.replaceText(newValue);
});
editArea.textProperty().addListener((observable, oldValue, newValue) -> {
viewModel.sourceTextProperty().setValue(newValue);
editArea.setStyleSpans(0, viewModel.computeHighlighting(newValue));
Expand All @@ -166,6 +178,10 @@ public void initialize() {
}
});

searchBox.textProperty().addListener((observable, previousText, searchTerm) -> {
viewModel.setFilterPredicate(searchTerm);
});

readOnlyLabel.visibleProperty().bind(viewModel.selectedIsEditableProperty().not());
resetDefaultButton.disableProperty().bind(viewModel.selectedIsEditableProperty().not());
contextMenu.getItems().get(0).disableProperty().bind(viewModel.selectedIsEditableProperty().not());
Expand All @@ -189,7 +205,7 @@ private void jumpToSearchKey(ListView<PreviewLayout> list, KeyEvent keypressed)
return;
}

if (System.currentTimeMillis() - lastKeyPressTime < 1000) {
if ((System.currentTimeMillis() - lastKeyPressTime) < 1000) {
listSearchTerm += keypressed.getCharacter().toLowerCase();
} else {
listSearchTerm = keypressed.getCharacter().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.transformation.FilteredList;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
Expand Down Expand Up @@ -83,7 +84,9 @@ public class PreviewTabViewModel implements PreferenceTabViewModel {
private final CustomLocalDragboard localDragboard;
private ListProperty<PreviewLayout> dragSourceList = null;
private ObjectProperty<MultipleSelectionModel<PreviewLayout>> dragSourceSelectionModel = null;
private List<String> restartWarning = new ArrayList<>();
private final List<String> restartWarning = new ArrayList<>();

private final FilteredList<PreviewLayout> filteredPreviews = new FilteredList<>(this.availableListProperty());

public PreviewTabViewModel(DialogService dialogService, PreferencesService preferences, TaskExecutor taskExecutor, StateManager stateManager) {
this.dialogService = dialogService;
Expand All @@ -92,13 +95,6 @@ public PreviewTabViewModel(DialogService dialogService, PreferencesService prefe
this.localDragboard = stateManager.getLocalDragboard();
initialPreviewPreferences = preferences.getPreviewPreferences();

sourceTextProperty.addListener((observable, oldValue, newValue) -> {
var currentLayout = getCurrentLayout();
if (currentLayout instanceof TextBasedPreviewLayout) {
((TextBasedPreviewLayout) currentLayout).setText(sourceTextProperty.getValue().replace("\n", "__NEWLINE__"));
}
});

chosenListValidator = new FunctionBasedValidator<>(
chosenListProperty,
input -> !chosenListProperty.getValue().isEmpty(),
Expand All @@ -109,6 +105,7 @@ public PreviewTabViewModel(DialogService dialogService, PreferencesService prefe
)
)
);

}

public BooleanProperty showAsExtraTabProperty() {
Expand Down Expand Up @@ -175,24 +172,6 @@ private PreviewLayout findLayoutByName(String name) {
.orElse(null));
}

private PreviewLayout getCurrentLayout() {
if (!chosenSelectionModelProperty.getValue().getSelectedItems().isEmpty()) {
return chosenSelectionModelProperty.getValue().getSelectedItems().get(0);

}

if (!chosenListProperty.getValue().isEmpty()) {
return chosenListProperty.getValue().get(0);
}

PreviewLayout layout = findLayoutByName(TextBasedPreviewLayout.NAME);
if (layout == null) {
layout = initialPreviewPreferences.getTextBasedPreviewLayout();
}

return layout;
}

/**
* Getter method for warning list.
* @return list of string with warning information.
Expand Down Expand Up @@ -483,6 +462,10 @@ public boolean dragDroppedInChosenCell(PreviewLayout targetLayout, Dragboard dra
return success;
}

public FilteredList<PreviewLayout> getFilteredPreviews() {
return this.filteredPreviews;
}

public ListProperty<PreviewLayout> availableListProperty() {
return availableListProperty;
}
Expand Down Expand Up @@ -510,4 +493,8 @@ public ObjectProperty<PreviewLayout> layoutProperty() {
public StringProperty sourceTextProperty() {
return sourceTextProperty;
}

public void setFilterPredicate(String searchTerm) {
this.filteredPreviews.setPredicate(preview -> searchTerm.isEmpty() || preview.containsCaseIndependent(searchTerm));
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/logic/preview/PreviewLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.logic.preview;

import java.util.Locale;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;

Expand All @@ -13,4 +15,8 @@ public interface PreviewLayout {
String getDisplayName();

String getName();

default boolean containsCaseIndependent(String searchTerm) {
return this.getDisplayName().toLowerCase(Locale.ROOT).contains(searchTerm.toLowerCase(Locale.ROOT));
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/jabref/preferences/PreviewPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ public Builder getBuilder() {
}

public PreviewLayout getCurrentPreviewStyle() {
return getPreviewCycle().get(getPreviewCyclePosition());
if (previewCycle.size() > 0) {
return previewCycle.get(previewCyclePosition);
}
return getTextBasedPreviewLayout();
}

public LayoutFormatterPreferences getLayoutFormatterPreferences() {
return Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);
}

public PreviewLayout getTextBasedPreviewLayout() {
return new TextBasedPreviewLayout(getPreviewStyle(), getLayoutFormatterPreferences());
return new TextBasedPreviewLayout(previewStyle, getLayoutFormatterPreferences());
}

public boolean showPreviewAsExtraTab() {
Expand Down

0 comments on commit 74b5852

Please sign in to comment.