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

Relativizes paths to aux files #4792

Merged
merged 8 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
24 changes: 22 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,6 +47,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.Keyword;
Expand All @@ -58,6 +61,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;

Expand Down Expand Up @@ -90,6 +94,7 @@ class GroupDialog extends BaseDialog<AbstractGroup> {
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();
Expand Down Expand Up @@ -144,6 +149,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));
Expand Down Expand Up @@ -330,7 +336,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());
Expand Down Expand Up @@ -610,7 +616,21 @@ 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<Path> fileDirectories = getFileDirectoriesAsPaths();
return FileUtil.relativize(path, fileDirectories);
}

private List<Path> getFileDirectoriesAsPaths() {
List<Path> fileDirs = new ArrayList<>();
MetaData metaData = basePanel.getBibDatabaseContext().getMetaData();
metaData.getLaTexFileDirectory(prefs.getFilePreferences().getUser())
.ifPresent(laTexFileDirectory -> fileDirs.add(laTexFileDirectory));

return fileDirs;
}

private String fromTextFlowToHTMLString(TextFlow textFlow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="-Infinity" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="-Infinity" prefHeight="50.0" vgrow="ALWAYS" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
Expand All @@ -40,10 +41,13 @@
<Label text="%User-specific file directory" GridPane.rowIndex="3" />
<TextField fx:id="userSpecificFileDirectory" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button fx:id="browseUserSpefiicFileDir" mnemonicParsing="false" onAction="#browseUserSpecificFileDirectory" text="%Browse" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Label text="%LaTex file directory" GridPane.rowIndex="4" />
<TextField fx:id="laTexFileDirectory" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Button fx:id="browseLatexFileDir" mnemonicParsing="false" onAction="#browseLaTexFileDirectory" text="%Browse" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<Label alignment="TOP_LEFT" styleClass="sectionHeader" text="%Override default file directories" GridPane.columnSpan="3" GridPane.rowIndex="1" />
<Label alignment="TOP_LEFT" styleClass="sectionHeader" text="%Library protection" GridPane.columnSpan="3" GridPane.rowIndex="4" />
<CheckBox fx:id="protect" mnemonicParsing="false" text="%Refuse to save the library before external changes have been reviewed." GridPane.columnSpan="3" GridPane.rowIndex="5" />
<Label styleClass="sectionHeader" text="%Export sort order" GridPane.rowIndex="6" />
<Label alignment="TOP_LEFT" styleClass="sectionHeader" text="%Library protection" GridPane.columnSpan="3" GridPane.rowIndex="5" />
<CheckBox fx:id="protect" mnemonicParsing="false" text="%Refuse to save the library before external changes have been reviewed." GridPane.columnSpan="3" GridPane.rowIndex="6" />
<Label styleClass="sectionHeader" text="%Export sort order" GridPane.rowIndex="7" />
</children>
</GridPane>
</children>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.libraryproperties;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Optional;

import javax.inject.Inject;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class LibraryPropertiesDialogView extends BaseDialog<Void> {
@FXML private Button browseGeneralFileDir;
@FXML private TextField userSpecificFileDirectory;
@FXML private Button browseUserSpefiicFileDir;
@FXML private TextField laTexFileDirectory;
@FXML private CheckBox protect;
@Inject private PreferencesService preferencesService;

Expand Down Expand Up @@ -70,6 +72,7 @@ private void initialize() {

generalFileDirectory.textProperty().bindBidirectional(viewModel.generalFileDirectoryPropertyProperty());
userSpecificFileDirectory.textProperty().bindBidirectional(viewModel.userSpecificFileDirectoryProperty());
laTexFileDirectory.textProperty().bindBidirectional(viewModel.LaTexFileDirectoryProperty());

encoding.itemsProperty().bind(viewModel.encodingsProperty());
encoding.valueProperty().bindBidirectional(viewModel.selectedEncodingProperty());
Expand Down Expand Up @@ -112,6 +115,11 @@ public void browseUserSpecificFileDirectory(ActionEvent event) {
viewModel.browseUserDir();
}

@FXML
void browseLaTexFileDirectory(ActionEvent event) {
viewModel.browseLaTexDir();
}

private void storeSettings() {
//FIXME: Move to viewModel until fieldFormatterCleanupsPanel is property implemented
MetaData metaData = panel.getBibDatabaseContext().getMetaData();
Expand All @@ -134,6 +142,13 @@ private void storeSettings() {
metaData.setUserFileDirectory(preferencesService.getUser(), text);
}

text = viewModel.LaTexFileDirectoryProperty().getValue();
if (text.isEmpty()) {
metaData.clearLaTexFileDirectory(preferencesService.getUser());
} else {
metaData.setLaTexFileDirectory(preferencesService.getUser(), Paths.get(text));
}

if (viewModel.libraryProtectedProperty().getValue()) {
metaData.markAsProtected();
} else {
Expand Down Expand Up @@ -165,7 +180,7 @@ private void storeSettings() {

boolean changed = saveOrderConfigChanged || encodingChanged
|| viewModel.generalFileDirChanged() || viewModel.userFileDirChanged()
|| viewModel.protectedValueChanged() || saveActionsChanged;
|| viewModel.protectedValueChanged() || saveActionsChanged || viewModel.LaTexFileDirChanged();
// ... if so, mark base changed. Prevent the Undo button from removing
// change marking:
if (changed) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.libraryproperties;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Optional;

import javafx.beans.property.BooleanProperty;
Expand All @@ -25,6 +26,7 @@ public class LibraryPropertiesDialogViewModel {

private final StringProperty generalFileDirectoryProperty = new SimpleStringProperty("");
private final StringProperty userSpecificFileDirectoryProperty = new SimpleStringProperty("");
private final StringProperty laTexFileDirectoryProperty = new SimpleStringProperty("");
private final ListProperty<Charset> encodingsProperty = new SimpleListProperty<>(FXCollections.observableArrayList(Encodings.getCharsets()));
private final ObjectProperty<Charset> selectedEncodingPropety = new SimpleObjectProperty<>(Encodings.getCharsets().get(0));
private final BooleanProperty libraryProtectedProperty = new SimpleBooleanProperty();
Expand All @@ -36,6 +38,7 @@ public class LibraryPropertiesDialogViewModel {

private final String oldUserSpecificFileDir;
private final String oldGeneralFileDir;
private final String oldLaTexFileDir;
private final boolean oldLibraryProtected;

public LibraryPropertiesDialogViewModel(BasePanel panel, DialogService dialogService, PreferencesService preferencesService) {
Expand All @@ -59,8 +62,15 @@ public LibraryPropertiesDialogViewModel(BasePanel panel, DialogService dialogSer
Optional<String> fileDI = metaData.getUserFileDirectory(preferencesService.getUser());
fileDI.ifPresent(userSpecificFileDirectoryProperty::setValue);

metaData.getLaTexFileDirectory(preferencesService.getUser()).map(Path::toString).ifPresent(laTexFileDirectoryProperty::setValue);

Optional<Path> fileDL = metaData.getLaTexFileDirectory(preferencesService.getUser());
samiyac marked this conversation as resolved.
Show resolved Hide resolved
Optional<String> laTexFile = Optional.ofNullable(fileDL.toString());
samiyac marked this conversation as resolved.
Show resolved Hide resolved
laTexFile.ifPresent(laTexFileDirectoryProperty::setValue);

oldUserSpecificFileDir = generalFileDirectoryProperty.getValue();
oldGeneralFileDir = userSpecificFileDirectoryProperty.getValue();
oldLaTexFileDir = laTexFileDirectoryProperty.getValue();

libraryProtectedProperty.setValue(metaData.isProtected());
oldLibraryProtected = libraryProtectedProperty.getValue();
Expand All @@ -74,6 +84,10 @@ public StringProperty userSpecificFileDirectoryProperty() {
return this.userSpecificFileDirectoryProperty;
}

public StringProperty LaTexFileDirectoryProperty() {
return this.laTexFileDirectoryProperty;
}

public ListProperty<Charset> encodingsProperty() {
return this.encodingsProperty;
}
Expand All @@ -90,6 +104,10 @@ public void browseUserDir() {
dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(dir -> userSpecificFileDirectoryProperty.setValue(dir.toAbsolutePath().toString()));
}

public void browseLaTexDir() {
dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(dir -> laTexFileDirectoryProperty.setValue(dir.toAbsolutePath().toString()));
}

public BooleanProperty libraryProtectedProperty() {
return this.libraryProtectedProperty;
}
Expand All @@ -102,6 +120,10 @@ public boolean userFileDirChanged() {
return !oldUserSpecificFileDir.equals(userSpecificFileDirectoryProperty.getValue());
}

public boolean LaTexFileDirChanged() {
return !oldLaTexFileDir.equals(laTexFileDirectoryProperty.getValue());
}

public boolean protectedValueChanged() {
return !oldLibraryProtected == libraryProtectedProperty.getValue();
}
Expand All @@ -113,4 +135,5 @@ public BooleanProperty encodingDisableProperty() {
public BooleanProperty protectDisableProperty() {
return protectDisableProperty;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static Map<String, String> getSerializedStringMap(MetaData metaData,
path -> stringyMetaData.put(MetaData.FILE_DIRECTORY, Collections.singletonList(path.trim())));
metaData.getUserFileDirectories().forEach((user, path) -> stringyMetaData
.put(MetaData.FILE_DIRECTORY + '-' + user, Collections.singletonList(path.trim())));
metaData.getLaTexFileDirectories().forEach((user, path) -> stringyMetaData
.put(MetaData.FILE_DIRECTORY + "Latex-" + user, Collections.singletonList(path.toString().trim())));

for (ContentSelector selector: metaData.getContentSelectorList()) {
stringyMetaData.put(MetaData.SELECTOR_META_PREFIX + selector.getFieldName(), selector.getValues());
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/jabref/logic/importer/util/GroupsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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.model.util.FileUpdateMonitor;

Expand All @@ -35,7 +36,7 @@ public class GroupsParser {
private GroupsParser() {
}

public static GroupTreeNode importGroups(List<String> orderedData, Character keywordSeparator, FileUpdateMonitor fileMonitor)
public static GroupTreeNode importGroups(List<String> orderedData, Character keywordSeparator, FileUpdateMonitor fileMonitor, MetaData metaData)
throws ParseException {
try {
GroupTreeNode cursor = null;
Expand All @@ -52,7 +53,7 @@ public static GroupTreeNode importGroups(List<String> orderedData, Character key
throw new ParseException("Expected \"" + string + "\" to contain whitespace");
}
int level = Integer.parseInt(string.substring(0, spaceIndex));
AbstractGroup group = GroupsParser.fromString(string.substring(spaceIndex + 1), keywordSeparator, fileMonitor);
AbstractGroup group = GroupsParser.fromString(string.substring(spaceIndex + 1), keywordSeparator, fileMonitor, metaData);
GroupTreeNode newNode = GroupTreeNode.fromGroup(group);
if (cursor == null) {
// create new root
Expand Down Expand Up @@ -84,7 +85,7 @@ public static GroupTreeNode importGroups(List<String> orderedData, Character key
* @throws ParseException If an error occurred and a group could not be created,
* e.g. due to a malformed regular expression.
*/
public static AbstractGroup fromString(String s, Character keywordSeparator, FileUpdateMonitor fileMonitor)
public static AbstractGroup fromString(String s, Character keywordSeparator, FileUpdateMonitor fileMonitor, MetaData metaData)
throws ParseException {
if (s.startsWith(MetadataSerializationConfiguration.KEYWORD_GROUP_ID)) {
return keywordGroupFromString(s, keywordSeparator);
Expand All @@ -108,21 +109,21 @@ public static AbstractGroup fromString(String s, Character keywordSeparator, Fil
return automaticKeywordGroupFromString(s);
}
if (s.startsWith(MetadataSerializationConfiguration.TEX_GROUP_ID)) {
return texGroupFromString(s, fileMonitor);
return texGroupFromString(s, fileMonitor, metaData);
}

throw new ParseException("Unknown group: " + s);
}

private static AbstractGroup texGroupFromString(String string, FileUpdateMonitor fileMonitor) throws ParseException {
private static AbstractGroup texGroupFromString(String string, FileUpdateMonitor fileMonitor, MetaData metaData) throws ParseException {
QuotedStringTokenizer tok = new QuotedStringTokenizer(string.substring(MetadataSerializationConfiguration.TEX_GROUP_ID
.length()), MetadataSerializationConfiguration.GROUP_UNIT_SEPARATOR, MetadataSerializationConfiguration.GROUP_QUOTE_CHAR);

String name = StringUtil.unquote(tok.nextToken(), MetadataSerializationConfiguration.GROUP_QUOTE_CHAR);
GroupHierarchyType context = GroupHierarchyType.getByNumberOrDefault(Integer.parseInt(tok.nextToken()));
try {
Path path = Paths.get(tok.nextToken());
TexGroup newGroup = new TexGroup(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor);
TexGroup newGroup = new TexGroup(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
addGroupDetails(tok, newGroup);
return newGroup;
} catch (InvalidPathException | IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -60,12 +62,18 @@ public MetaData parse(MetaData metaData, Map<String, String> data, Character key
} else if (entry.getKey().startsWith(MetaData.SELECTOR_META_PREFIX)) {
metaData.addContentSelector(ContentSelectors.parse(entry.getKey().substring(MetaData.SELECTOR_META_PREFIX.length()), StringUtil.unquote(entry.getValue(), MetaData.ESCAPE_CHARACTER)));
continue;
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY + "Latex-")) {
// The user name comes directly after "FILE_DIRECTORYLatex-"
String user = entry.getKey().substring(MetaData.FILE_DIRECTORY.length() + 6);
Path path = Paths.get(getSingleItem(value)).normalize();
metaData.setLaTexFileDirectory(user, path);
continue;
}

switch (entry.getKey()) {
case MetaData.GROUPSTREE:
case MetaData.GROUPSTREE_LEGACY:
metaData.setGroups(GroupsParser.importGroups(value, keywordSeparator, fileMonitor));
metaData.setGroups(GroupsParser.importGroups(value, keywordSeparator, fileMonitor, metaData));
break;
case MetaData.SAVE_ACTIONS:
metaData.setSaveActions(Cleanups.parse(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public Optional<Path> getFirstExistingFileDir(FilePreferences preferences) {
* metadata can specify a general directory and/or a user-specific directory or the preferences can specify one. <p>
* The settings are prioritized in the following order and the first defined setting is used:
* <ol>
* <li>metdata</li>
* <li>metadata</li>
* <li>user-specific directory</li>
* <li>preferences directory</li>
* <li>BIB file directory</li>
Expand Down
Loading