Skip to content

Commit

Permalink
fix loading and storing of XMP Field Exlusions
Browse files Browse the repository at this point in the history
Fixes #4072
  • Loading branch information
Siedlerchr committed Aug 23, 2018
1 parent fe06310 commit 95df58c
Showing 1 changed file with 69 additions and 73 deletions.
142 changes: 69 additions & 73 deletions src/main/java/org/jabref/gui/preferences/XmpPrefsTab.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package org.jabref.gui.preferences;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;

import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.JabRefPreferences;

/**
Expand All @@ -32,32 +34,34 @@
* Allows the user to enable and configure the XMP privacy filter.
*/
class XmpPrefsTab extends Pane implements PrefsTab {

private final JabRefPreferences prefs;
private boolean tableChanged;
private final GridPane builder = new GridPane();
private final ObservableList<TableRow> data = FXCollections.observableArrayList();
private final ListProperty<XMPPrivacyFilter> fields = new SimpleListProperty<>(FXCollections.observableArrayList());
private final CheckBox privacyFilterCheckBox = new CheckBox(
Localization.lang("Do not write the following fields to XMP Metadata:"));
private final List<TableRow> tableRows = new ArrayList<>(10);
Localization.lang("Do not write the following fields to XMP Metadata:"));
private final TableView<XMPPrivacyFilter> tableView = new TableView<>();

/**
* Customization of external program paths.
*/
public XmpPrefsTab(JabRefPreferences prefs) {
this.prefs = Objects.requireNonNull(prefs);
TableView tableView = new TableView();
TableColumn<TableRow,String> column = new TableColumn<>(Localization.lang("Field to filter"));
column.setCellValueFactory(new PropertyValueFactory<>("name"));
column.setCellFactory(TextFieldTableCell.forTableColumn());
column.setOnEditCommit(
(TableColumn.CellEditEvent<TableRow, String> t) -> {
t.getTableView().getItems().get(
t.getTablePosition().getRow()).setName(t.getNewValue());
});

tableView.itemsProperty().bindBidirectional(fields);
TableColumn<XMPPrivacyFilter, String> column = new TableColumn<>();

column.setCellValueFactory(cellData -> cellData.getValue().filterName());
new ValueTableCellFactory<XMPPrivacyFilter, String>().withText(item -> item).install(column);

column.setOnEditCommit((CellEditEvent<XMPPrivacyFilter, String> cell) -> {
cell.getRowValue().setField(cell.getNewValue());
});

column.setPrefWidth(350);
tableView.setItems(data);
tableView.getColumns().add(column);
final TextField addName = new TextField();

TextField addName = new TextField();
addName.setPromptText("name");
addName.setPrefSize(200, 30);
BorderPane tablePanel = new BorderPane();
Expand All @@ -69,33 +73,24 @@ public XmpPrefsTab(JabRefPreferences prefs) {

Button add = new Button("Add");
add.setPrefSize(80, 20);
add.setOnAction(e-> {
add.setOnAction(e -> {
if (!addName.getText().isEmpty()) {
TableRow tableRow = new TableRow(addName.getText());
XMPPrivacyFilter tableRow = new XMPPrivacyFilter(addName.getText());
addName.clear();
data.add(tableRow);
tableRows.clear();
tableRows.addAll(data);
tableView.setItems(data);
tableChanged = true;
tableView.refresh();
fields.add(tableRow);
}
});
Button delete = new Button("Delete");
delete.setPrefSize(80, 20);
delete.setOnAction(e-> {
if (tableView.getFocusModel() != null && tableView.getFocusModel().getFocusedIndex() != -1) {
tableChanged = true;
delete.setOnAction(e -> {
if ((tableView.getFocusModel() != null) && (tableView.getFocusModel().getFocusedIndex() != -1)) {
int row = tableView.getFocusModel().getFocusedIndex();
TableRow tableRow = data.get(row);
data.remove(tableRow);
tableRows.clear();
tableRows.addAll(data);
tableView.setItems(data);
tableView.refresh();
XMPPrivacyFilter tableRow = fields.get(row);
fields.remove(tableRow);

}
});
HBox toolbar = new HBox(addName,add,delete);
HBox toolbar = new HBox(addName, add, delete);
tablePanel.setBottom(toolbar);

// Build Prefs Tabs
Expand All @@ -107,35 +102,20 @@ public XmpPrefsTab(JabRefPreferences prefs) {

}

@Override
public Node getBuilder() {
return builder;
}

public static class TableRow {
private SimpleStringProperty name;

TableRow(String name) {
this.name = new SimpleStringProperty(name);
}

public void setName(String name) {
this.name.set(name);
}

public String getName() {
return name.get();
}
}

/**
* Load settings from the preferences and initialize the table.
*/
@Override
public void setValues() {
tableRows.clear();
tableRows.addAll(data);
privacyFilterCheckBox.setSelected(JabRefPreferences.getInstance().getBoolean(
JabRefPreferences.USE_XMP_PRIVACY_FILTER));
List<XMPPrivacyFilter> xmpExclusions = prefs.getStringList(JabRefPreferences.XMP_PRIVACY_FILTERS).stream().map(XMPPrivacyFilter::new).collect(Collectors.toList());
fields.clear();
fields.addAll(xmpExclusions);
privacyFilterCheckBox.setSelected(JabRefPreferences.getInstance().getBoolean(JabRefPreferences.USE_XMP_PRIVACY_FILTER));
}

/**
Expand All @@ -145,24 +125,14 @@ public void setValues() {
*/
@Override
public void storeSettings() {
// Now we need to make sense of the contents the user has made to the
// table setup table. This needs to be done either if changes were made, or
// if the checkbox is checked and no field values have been stored previously:
if (tableChanged ||
(privacyFilterCheckBox.isSelected() && !prefs.hasKey(JabRefPreferences.XMP_PRIVACY_FILTERS))) {

// First we remove all rows with empty names.
for (int i = tableRows.size() - 1; i >= 0; i--) {
if ((tableRows.get(i) == null) || tableRows.get(i).toString().isEmpty()) {
tableRows.remove(i);
}
}
// Finally, we store the new preferences.
JabRefPreferences.getInstance().putStringList(JabRefPreferences.XMP_PRIVACY_FILTERS,
tableRows.stream().map(Object::toString).collect(Collectors.toList()));
if (privacyFilterCheckBox.isSelected()) {

fields.stream().filter(s -> StringUtil.isNullOrEmpty(s.getField())).forEach(fields::remove);
prefs.putStringList(JabRefPreferences.XMP_PRIVACY_FILTERS,
fields.stream().map(XMPPrivacyFilter::getField).collect(Collectors.toList()));
}

JabRefPreferences.getInstance().putBoolean(JabRefPreferences.USE_XMP_PRIVACY_FILTER, privacyFilterCheckBox.isSelected());
prefs.putBoolean(JabRefPreferences.USE_XMP_PRIVACY_FILTER, privacyFilterCheckBox.isSelected());
}

@Override
Expand All @@ -174,4 +144,30 @@ public boolean validateSettings() {
public String getTabName() {
return Localization.lang("XMP-metadata");
}

private class XMPPrivacyFilter {

private final SimpleStringProperty field;

XMPPrivacyFilter(String field) {
this.field = new SimpleStringProperty(field);
}

public void setField(String field) {
this.field.set(field);
}

public String getField() {
return field.get();
}

public StringProperty filterName() {
return field;
}

@Override
public String toString() {
return field.getValue();
}
}
}

0 comments on commit 95df58c

Please sign in to comment.