From 95ed92c501456b2ad8476fb50eff1334f0d76e96 Mon Sep 17 00:00:00 2001 From: samiyac Date: Thu, 21 Mar 2019 01:08:53 +0530 Subject: [PATCH 1/8] Relativizes paths to aux files --- .../org/jabref/gui/groups/GroupDialog.java | 32 +++++++++- .../LibraryPropertiesDialog.fxml | 10 +++- .../LibraryPropertiesDialogView.java | 17 +++++- .../LibraryPropertiesDialogViewModel.java | 19 ++++++ .../logic/exporter/MetaDataSerializer.java | 6 +- .../logic/importer/util/GroupsParser.java | 13 +++-- .../logic/importer/util/MetaDataParser.java | 7 ++- .../model/database/BibDatabaseContext.java | 2 +- .../org/jabref/model/groups/TexGroup.java | 58 ++++++++++++++++--- .../org/jabref/model/metadata/MetaData.java | 20 +++++++ src/main/resources/l10n/JabRef_en.properties | 1 + .../exporter/BibtexDatabaseWriterTest.java | 3 +- .../logic/exporter/GroupSerializerTest.java | 3 +- .../importer/fileformat/BibtexParserTest.java | 4 +- .../logic/importer/util/GroupsParserTest.java | 25 ++++---- .../org/jabref/model/groups/TexGroupTest.java | 24 +++++++- 16 files changed, 205 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialog.java b/src/main/java/org/jabref/gui/groups/GroupDialog.java index 94c9f519fa7..d4f863c7a68 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialog.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialog.java @@ -1,8 +1,10 @@ package org.jabref.gui.groups; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -58,6 +60,7 @@ import org.jabref.model.groups.SearchGroup; import org.jabref.model.groups.TexGroup; import org.jabref.model.groups.WordKeywordGroup; +import org.jabref.model.metadata.MetaData; import org.jabref.model.strings.StringUtil; import org.jabref.preferences.JabRefPreferences; @@ -90,6 +93,7 @@ class GroupDialog extends BaseDialog { private final RadioButton unionButton = new RadioButton(Localization.lang("Include subgroups: When selected, view entries contained in this group or its subgroups")); private final DialogService dialogService; private final JabRefPreferences prefs; + private final BasePanel basePanel; // for KeywordGroup private final TextField keywordGroupSearchTerm = new TextField(); @@ -144,6 +148,7 @@ public GroupDialog(DialogService dialogService, BasePanel basePanel, JabRefPrefe this.dialogService = dialogService; this.prefs = prefs; + this.basePanel = basePanel; // set default values (overwritten if editedGroup != null) keywordGroupSearchField.setText(prefs.get(JabRefPreferences.GROUPS_DEFAULT_FIELD)); @@ -330,7 +335,7 @@ groupName, getContext(), } } else if (texRadioButton.isSelected()) { resultingGroup = new TexGroup(groupName, getContext(), - Paths.get(texGroupFilePath.getText().trim()), new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor()); + Paths.get(texGroupFilePath.getText().trim()), new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor(), basePanel.getBibDatabaseContext().getMetaData()); } resultingGroup.setColor(colorField.getValue()); @@ -610,7 +615,30 @@ private void openBrowseDialog() { .addExtensionFilter(StandardFileType.AUX) .withDefaultExtension(StandardFileType.AUX) .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build(); - dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> texGroupFilePath.setText(file.toAbsolutePath().toString())); + dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> texGroupFilePath.setText(relativize(file.toAbsolutePath()).toString())); + } + + private Path relativize(Path path) { + List fileDirectories = getFileDirectoriesAsPaths(); + if (!path.isAbsolute()) { + return path; + } + + for (Path directory : fileDirectories) { + if (path.startsWith(directory)) { + return directory.relativize(path); + } + } + return path; + } + + private List getFileDirectoriesAsPaths() { + List fileDirs = new ArrayList<>(); + MetaData metaData = basePanel.getBibDatabaseContext().getMetaData(); + metaData.getLaTexFileDirectory(prefs.getFilePreferences().getUser()) + .ifPresent(LaTexFileDirectory -> fileDirs.add(Paths.get(LaTexFileDirectory).toAbsolutePath().normalize())); + + return fileDirs; } private String fromTextFlowToHTMLString(TextFlow textFlow) { diff --git a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml index 126d9e959b6..9352cba7ac4 100644 --- a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml +++ b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml @@ -26,6 +26,7 @@ + @@ -40,10 +41,13 @@