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

Add latest changes to latexintegration branch #5170

Merged
merged 1 commit into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
114 changes: 59 additions & 55 deletions src/main/java/org/jabref/gui/entryeditor/LatexCitationsTab.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package org.jabref.gui.entryeditor;

import java.util.Optional;
import java.util.stream.Collectors;

import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

import org.jabref.gui.DialogService;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.texparser.CitationsDisplay;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
Expand All @@ -24,14 +28,16 @@
public class LatexCitationsTab extends EntryEditorTab {

private final LatexCitationsTabViewModel viewModel;
private final StackPane searchPane;
private final GridPane searchPane;
private final ProgressIndicator progressIndicator;
private final CitationsDisplay citationsDisplay;

public LatexCitationsTab(BibDatabaseContext databaseContext, PreferencesService preferencesService,
TaskExecutor taskExecutor, DialogService dialogService) {
this.viewModel = new LatexCitationsTabViewModel(databaseContext, preferencesService, taskExecutor, dialogService);
this.searchPane = new StackPane();
this.searchPane = new GridPane();
this.progressIndicator = new ProgressIndicator();
this.citationsDisplay = new CitationsDisplay();

setText(Localization.lang("LaTeX Citations"));
setTooltip(new Tooltip(Localization.lang("Search citations for this entry in LaTeX files")));
Expand All @@ -41,81 +47,79 @@ public LatexCitationsTab(BibDatabaseContext databaseContext, PreferencesService

private void setSearchPane() {
progressIndicator.setMaxSize(100, 100);
searchPane.getStyleClass().add("related-articles-tab");
citationsDisplay.basePathProperty().bindBidirectional(viewModel.directoryProperty());
citationsDisplay.setItems(viewModel.getCitationList());

RowConstraints mainRow = new RowConstraints();
mainRow.setVgrow(Priority.ALWAYS);

RowConstraints bottomRow = new RowConstraints(40);
bottomRow.setVgrow(Priority.NEVER);

ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(100);
column.setHalignment(HPos.CENTER);

searchPane.getColumnConstraints().setAll(column);
searchPane.getRowConstraints().setAll(mainRow, bottomRow);
setContent(searchPane);

EasyBind.subscribe(viewModel.statusProperty(), status -> {
searchPane.getChildren().clear();
switch (status) {
case IN_PROGRESS:
searchPane.getChildren().setAll(progressIndicator);
searchPane.add(progressIndicator, 0, 0);
break;
case CITATIONS_FOUND:
searchPane.getChildren().setAll(getCitationsPane());
searchPane.add(getCitationsPane(), 0, 0);
break;
case NO_RESULTS:
searchPane.getChildren().setAll(getNotFoundPane());
searchPane.add(getNotFoundPane(), 0, 0);
break;
case ERROR:
searchPane.getChildren().setAll(getErrorPane());
searchPane.add(getErrorPane(), 0, 0);
break;
}
searchPane.add(getLatexDirectoryBox(), 0, 1);
});
}

private VBox getLatexDirectoryBox() {
Text latexDirectoryText = new Text(String.format("%n%n%s: %s", Localization.lang("Current search directory"),
viewModel.directoryProperty().get()));
latexDirectoryText.setStyle("-fx-font-weight: bold;");
private HBox getLatexDirectoryBox() {
Text latexDirectoryText = new Text(Localization.lang("Current search directory:"));
latexDirectoryText.setStyle("-fx-font-size: 0.9em;-fx-padding: 0;");
Text latexDirectoryPath = new Text(viewModel.directoryProperty().get().toString());
latexDirectoryPath.setStyle("-fx-font-family: 'Courier New', Courier, monospace;-fx-font-size: 0.9em;-fx-font-weight: bold;");
Button latexDirectoryButton = new Button(Localization.lang("Set LaTeX file directory"));
latexDirectoryButton.setStyle("-fx-border-width: 1;-fx-font-size: 0.85em;-fx-padding: 0.2em;");
latexDirectoryButton.setGraphic(IconTheme.JabRefIcons.LATEX_FILE_DIRECTORY.getGraphicNode());
latexDirectoryButton.setOnAction(event -> viewModel.setLatexDirectory());

return new VBox(15, latexDirectoryText, latexDirectoryButton);
HBox latexDirectoryBox = new HBox(10, latexDirectoryText, latexDirectoryPath, latexDirectoryButton);
latexDirectoryBox.setAlignment(Pos.CENTER);
return latexDirectoryBox;
}

private ScrollPane getCitationsPane() {
Text titleText = new Text(Localization.lang("Citations found"));
titleText.getStyleClass().add("recommendation-heading");

VBox citationsBox = new VBox(20, titleText);
citationsBox.getChildren().addAll(viewModel.getCitationList().stream().map(
citation -> citation.getDisplayGraphic(viewModel.directoryProperty().get(), Optional.empty())).collect(Collectors.toList()));

citationsBox.getChildren().add(getLatexDirectoryBox());

ScrollPane citationsPane = new ScrollPane();
citationsPane.setContent(citationsBox);

return citationsPane;
private VBox getCitationsPane() {
VBox citationsBox = new VBox(30, citationsDisplay);
citationsBox.setStyle("-fx-padding: 0;");
return citationsBox;
}

private ScrollPane getNotFoundPane() {
Text notFoundTitleText = new Text(Localization.lang("No citations found"));
notFoundTitleText.getStyleClass().add("recommendation-heading");

private VBox getNotFoundPane() {
Label titleLabel = new Label(Localization.lang("No citations found"));
titleLabel.setStyle("-fx-font-size: 1.5em;-fx-font-weight: bold;-fx-text-fill: -jr-theme-text;");
Text notFoundText = new Text(Localization.lang("No LaTeX files containing this entry were found."));
notFoundText.setStyle("-fx-font-size: 110%;");

VBox notFoundBox = new VBox(20, notFoundTitleText, notFoundText, getLatexDirectoryBox());
ScrollPane notFoundPane = new ScrollPane();
notFoundPane.setContent(notFoundBox);

return notFoundPane;
VBox notFoundBox = new VBox(30, titleLabel, notFoundText);
notFoundBox.setStyle("-fx-padding: 30 0 0 30;");
return notFoundBox;
}

private ScrollPane getErrorPane() {
Text errorTitleText = new Text(Localization.lang("Error"));
errorTitleText.setStyle("-fx-fill: -fx-accent;");
errorTitleText.getStyleClass().add("recommendation-heading");

private VBox getErrorPane() {
Label titleLabel = new Label(Localization.lang("Error"));
titleLabel.setStyle("-fx-font-size: 1.5em;-fx-font-weight: bold;-fx-text-fill: -fx-accent;");
Text errorMessageText = new Text(viewModel.searchErrorProperty().get());
errorMessageText.setStyle("-fx-font-family: monospace;-fx-font-size: 120%;");

VBox errorBox = new VBox(20, errorTitleText, errorMessageText, getLatexDirectoryBox());
ScrollPane errorPane = new ScrollPane();
errorPane.setContent(errorBox);

return errorPane;
VBox errorMessageBox = new VBox(30, titleLabel, errorMessageText);
errorMessageBox.setStyle("-fx-padding: 30 0 0 30;");
return errorMessageBox;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -21,14 +22,14 @@

import org.jabref.gui.AbstractViewModel;
import org.jabref.gui.DialogService;
import org.jabref.gui.texparser.CitationViewModel;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.texparser.DefaultTexParser;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.texparser.Citation;
import org.jabref.model.texparser.TexParserResult;
import org.jabref.preferences.PreferencesService;

Expand All @@ -51,19 +52,21 @@ enum Status {
private final TaskExecutor taskExecutor;
private final DialogService dialogService;
private final ObjectProperty<Path> directory;
private final ObservableList<CitationViewModel> citationList;
private final ObservableList<Citation> citationList;
private final ObjectProperty<Status> status;
private final StringProperty searchError;
private Future<?> searchTask;
private TexParserResult texParserResult;
private BibEntry currentEntry;

public LatexCitationsTabViewModel(BibDatabaseContext databaseContext, PreferencesService preferencesService,
TaskExecutor taskExecutor, DialogService dialogService) {
this.databaseContext = databaseContext;
this.preferencesService = preferencesService;
this.taskExecutor = taskExecutor;
this.dialogService = dialogService;
this.directory = new SimpleObjectProperty<>(null);
this.directory = new SimpleObjectProperty<>(databaseContext.getMetaData().getLaTexFileDirectory(preferencesService.getUser())
.orElseGet(preferencesService::getWorkingDir));
this.citationList = FXCollections.observableArrayList();
this.status = new SimpleObjectProperty<>(Status.IN_PROGRESS);
this.searchError = new SimpleStringProperty("");
Expand All @@ -72,6 +75,7 @@ public LatexCitationsTabViewModel(BibDatabaseContext databaseContext, Preference
public void init(BibEntry entry) {
cancelSearch();

currentEntry = entry;
Optional<String> citeKey = entry.getCiteKeyOptional();
if (citeKey.isPresent()) {
startSearch(citeKey.get());
Expand All @@ -85,7 +89,7 @@ public ObjectProperty<Path> directoryProperty() {
return directory;
}

public ObservableList<CitationViewModel> getCitationList() {
public ObservableList<Citation> getCitationList() {
return new ReadOnlyListWrapper<>(citationList);
}

Expand All @@ -100,7 +104,10 @@ public StringProperty searchErrorProperty() {
private void startSearch(String citeKey) {
searchTask = BackgroundTask.wrap(() -> searchAndParse(citeKey))
.onRunning(() -> status.set(Status.IN_PROGRESS))
.onSuccess(status::set)
.onSuccess(result -> {
citationList.setAll(result);
status.set(citationList.isEmpty() ? Status.NO_RESULTS : Status.CITATIONS_FOUND);
})
.onFailure(error -> {
searchError.set(error.getMessage());
status.set(Status.ERROR);
Expand All @@ -117,32 +124,30 @@ private void cancelSearch() {
searchTask.cancel(true);
}

private Status searchAndParse(String citeKey) throws IOException {
private Collection<Citation> searchAndParse(String citeKey) throws IOException {
Path newDirectory = databaseContext.getMetaData().getLaTexFileDirectory(preferencesService.getUser())
.orElseGet(preferencesService::getWorkingDir);

if (texParserResult == null || !newDirectory.equals(directory.get())) {
directory.set(newDirectory);

if (Files.notExists(newDirectory)) {
if (!newDirectory.toFile().exists()) {
throw new IOException(String.format("Current search directory does not exist: %s", newDirectory));
}

List<Path> texFiles = searchDirectory(newDirectory, new ArrayList<>());
texParserResult = new DefaultTexParser().parse(texFiles);
}

citationList.setAll(texParserResult.getCitationsByKey(citeKey).stream().map(CitationViewModel::new).collect(Collectors.toList()));

return citationList.isEmpty() ? Status.NO_RESULTS : Status.CITATIONS_FOUND;
return texParserResult.getCitationsByKey(citeKey);
}

private List<Path> searchDirectory(Path directory, List<Path> texFiles) {
Map<Boolean, List<Path>> fileListPartition;
try (Stream<Path> filesStream = Files.list(directory)) {
fileListPartition = filesStream.collect(Collectors.partitioningBy(path -> path.toFile().isDirectory()));
} catch (IOException e) {
LOGGER.error(String.format("Error searching files: %s", e.getMessage()));
LOGGER.error(String.format("%s while searching files: %s", e.getClass().getName(), e.getMessage()));
return texFiles;
}

Expand All @@ -163,6 +168,8 @@ public void setLatexDirectory() {

dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(selectedDirectory ->
databaseContext.getMetaData().setLaTexFileDirectory(preferencesService.getUser(), selectedDirectory.toAbsolutePath()));

init(currentEntry);
}

public boolean shouldShow() {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/icon/IconTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ public enum JabRefIcons implements JabRefIcon {
CLOSE_JABREF(MaterialDesignIcon.GLASSDOOR),
ARTICLE(MaterialDesignIcon.FILE_DOCUMENT),
BOOK(MaterialDesignIcon.BOOK_OPEN_PAGE_VARIANT),
LATEX_CITATIONS(JabRefMaterialDesignIcon.TEX_STUDIO);
LATEX_CITATIONS(JabRefMaterialDesignIcon.TEX_STUDIO),
LATEX_FILE_DIRECTORY(MaterialDesignIcon.FOLDER_OUTLINE),
LATEX_FILE(MaterialDesignIcon.FILE_OUTLINE),
LATEX_COMMENT(MaterialDesignIcon.COMMENT_TEXT_OUTLINE),
LATEX_LINE(MaterialDesignIcon.FORMAT_LINE_SPACING);

private final JabRefIcon icon;

Expand Down
59 changes: 0 additions & 59 deletions src/main/java/org/jabref/gui/texparser/CitationViewModel.java

This file was deleted.

Loading