Skip to content

Commit

Permalink
Conversion of prefs/bibtexkeygen and appearance to mvvm (#5360)
Browse files Browse the repository at this point in the history
* Initial

* Fix special key, refactor visual appearance, l10n

* Fixed Indent and wrong value set

* Refurbished css

* Added BibtexKeyPatternTable, fixed bugs

* Fixed bugs in reset, added reset all

* Refactored by PR-suggestions, added jumpToPressedKey, highlite default
column, set defaultItem to top of list,

* l10n

* Changelog

* Fixed pattern saving, converted default to property

* Changed comments

* Added help button

* Removed fontTweaksLinux according to PR #5330

* l10n

* Changed font size limits
  • Loading branch information
calixtus authored and tobiasdiez committed Oct 15, 2019
1 parent 3bad538 commit 2a5507f
Show file tree
Hide file tree
Showing 23 changed files with 820 additions and 315 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed a problem where the "editor" information has been duplicated during saving a .bib-Database. [#5359](https://github.com/JabRef/jabref/issues/5359)
- We re-introduced the feature to switch between different preview styles. [#5221](https://github.com/JabRef/jabref/issues/5221)
- We fixed various issues (including [#5263](https://github.com/JabRef/jabref/issues/5263)) related to copying entries to the clipboard
- We fixed some display errors in the preferences dialog and replaced some of the controls [#5033](https://github.com/JabRef/jabref/pull/5033) [#5047](https://github.com/JabRef/jabref/pull/5047) [#5062](https://github.com/JabRef/jabref/pull/5062) [#5141](https://github.com/JabRef/jabref/pull/5141) [#5185](https://github.com/JabRef/jabref/pull/5185) [#5265](https://github.com/JabRef/jabref/pull/5265) [#5315](https://github.com/JabRef/jabref/pull/5315) [#5360](https://github.com/JabRef/jabref/pull/5360)
- We fixed an exception which occurred when trying to import entries without an open library. [#5447](https://github.com/JabRef/jabref/issues/5447)



### Removed


Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/jabref/gui/Base.css
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,3 @@ We want to have a look that matches our icons in the tool-bar */
.dialog-pane {
-fx-background-color: -fx-control-inner-background;
}

.preference-sidepane {
-fx-background-color: -jr-sidepane-background;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ private void init() {
return null;
});

// Keep this for later conversion of the library-properties
/* void storeSettings() {
DataBaseKeyPattern newKeyPattern = new DatabaseBibtexKeyPattern(preferences.getKeyPattern());
bibtexKeyPatternTableView.patternListProperty.forEach(item -> {
String patternString = item.getPattern();
if (!item.getEntryType().getName().equals("default")) {
if (!patternString.trim().isEmpty()) {
newKeyPattern.addBibtexKeyPattern(item.getEntryType(), patternString);
}
}
});
if (!defaultItem.getPattern().trim().isEmpty()) {
// we do not trim the value at the assignment to enable users to have spaces at the beginning and
// at the end of the pattern
newKeyPattern.setDefaultValue(defaultItemProperty.getPattern());
}
} */

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import org.jabref.model.bibtexkeypattern.DatabaseBibtexKeyPattern;
import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.types.EntryType;
Expand Down Expand Up @@ -118,12 +117,6 @@ private void buildGUI() {
gridPane.add(btnDefaultAll1, 2, rowIndex);
}

protected GlobalBibtexKeyPattern getKeyPatternAsGlobalBibtexKeyPattern() {
GlobalBibtexKeyPattern res = GlobalBibtexKeyPattern.fromPattern(Globals.prefs.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN));
fillPatternUsingPanelData(res);
return res;
}

public DatabaseBibtexKeyPattern getKeyPatternAsDatabaseBibtexKeyPattern() {
DatabaseBibtexKeyPattern res = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
fillPatternUsingPanelData(res);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>

<fx:root editable="true" minWidth="280.0" type="TableView"
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.jabref.gui.bibtexkeypattern.BibtexKeyPatternTableView">
<columns>
<TableColumn fx:id="entryTypeColumn" editable="false" minWidth="100.0" text="Name"/>
<TableColumn fx:id="patternColumn" minWidth="100.0" text="Key pattern"/>
<TableColumn fx:id="actionsColumn" editable="false" maxWidth="30.0" minWidth="30.0" prefWidth="30.0"
reorderable="false" resizable="false" sortable="false"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</fx:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jabref.gui.bibtexkeypattern;

import java.util.Objects;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.model.entry.types.EntryType;

public class BibtexKeyPatternTableItemModel {
private final ObjectProperty<EntryType> entryType = new SimpleObjectProperty<>();
private final StringProperty pattern = new SimpleStringProperty("");

public BibtexKeyPatternTableItemModel(EntryType entryType, String pattern) {
Objects.requireNonNull(entryType);
Objects.requireNonNull(pattern);
this.entryType.setValue(entryType);
this.pattern.setValue(pattern);
}

public EntryType getEntryType() { return entryType.getValue(); }

public ObjectProperty<EntryType> entryType() { return entryType; }

public void setPattern(String pattern) {
this.pattern.setValue(pattern);
}

public String getPattern() {
return pattern.getValue();
}

public StringProperty pattern() { return pattern; }

@Override
public String toString() { return "[" + entryType.getValue().getName() + "," + pattern.getValue() + "]"; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.jabref.gui.bibtexkeypattern;

import java.util.Collection;

import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyEvent;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.types.EntryType;
import org.jabref.preferences.JabRefPreferences;

import com.airhacks.afterburner.views.ViewLoader;

public class BibtexKeyPatternTableView extends TableView<BibtexKeyPatternTableItemModel> {

@FXML public TableColumn<BibtexKeyPatternTableItemModel, EntryType> entryTypeColumn;
@FXML public TableColumn<BibtexKeyPatternTableItemModel, String> patternColumn;
@FXML public TableColumn<BibtexKeyPatternTableItemModel, EntryType> actionsColumn;

private BibtexKeyPatternTableViewModel viewModel;

private long lastKeyPressTime;
private String tableSearchTerm;

public BibtexKeyPatternTableView(JabRefPreferences preferences, Collection<BibEntryType> entryTypeList, AbstractBibtexKeyPattern keyPattern) {
super();

viewModel = new BibtexKeyPatternTableViewModel(preferences, entryTypeList, keyPattern);

ViewLoader.view(this)
.root(this)
.load();
}

@FXML
private void initialize() {
this.setEditable(true);

entryTypeColumn.setSortable(true);
entryTypeColumn.setReorderable(false);
entryTypeColumn.setCellValueFactory(cellData -> cellData.getValue().entryType());
new ValueTableCellFactory<BibtexKeyPatternTableItemModel, EntryType>()
.withText(EntryType::getDisplayName)
.install(entryTypeColumn);
this.setOnSort(event ->
viewModel.patternListProperty().sort(BibtexKeyPatternTableViewModel.defaultOnTopComparator));

patternColumn.setSortable(true);
patternColumn.setReorderable(false);
patternColumn.setCellValueFactory(cellData -> cellData.getValue().pattern());
patternColumn.setCellFactory(TextFieldTableCell.forTableColumn());
patternColumn.setEditable(true);
patternColumn.setOnEditCommit(
(TableColumn.CellEditEvent<BibtexKeyPatternTableItemModel, String> event) ->
event.getRowValue().setPattern(event.getNewValue()));

actionsColumn.setSortable(false);
actionsColumn.setReorderable(false);
actionsColumn.setCellValueFactory(cellData -> cellData.getValue().entryType());
new ValueTableCellFactory<BibtexKeyPatternTableItemModel, EntryType>()
.withGraphic(entryType -> IconTheme.JabRefIcons.REFRESH.getGraphicNode())
.withTooltip(entryType ->
String.format(Localization.lang("Reset %s to default value"), entryType.getDisplayName()))
.withOnMouseClickedEvent(item -> evt ->
viewModel.setItemToDefaultPattern(this.getFocusModel().getFocusedItem()))
.install(actionsColumn);

this.setRowFactory(item -> new HighlightTableRow());
this.setOnKeyTyped(this::jumpToSearchKey);
this.itemsProperty().bindBidirectional(viewModel.patternListProperty());
}

public void setValues() { viewModel.setValues(); }

public void resetAll() { viewModel.resetAll(); }

public ListProperty<BibtexKeyPatternTableItemModel> patternListProperty() { return viewModel.patternListProperty(); }

public ObjectProperty<BibtexKeyPatternTableItemModel> defaultKeyPatternProperty() { return viewModel.defaultKeyPatternProperty(); }

private void jumpToSearchKey(KeyEvent keypressed) {
if (keypressed.getCharacter() == null) {
return;
}

if (System.currentTimeMillis() - lastKeyPressTime < 1000) {
tableSearchTerm += keypressed.getCharacter().toLowerCase();
} else {
tableSearchTerm = keypressed.getCharacter().toLowerCase();
}

lastKeyPressTime = System.currentTimeMillis();

this.getItems().stream().filter(item -> item.getEntryType().getName().toLowerCase().startsWith(tableSearchTerm))
.findFirst().ifPresent(this::scrollTo);
}

private static class HighlightTableRow extends TableRow<BibtexKeyPatternTableItemModel> {
@Override
public void updateItem(BibtexKeyPatternTableItemModel item, boolean empty) {
super.updateItem(item, empty);
if (item == null || item.getEntryType() == null) {
setStyle("");
} else if (isSelected()) {
setStyle("-fx-background-color: -fx-selection-bar");
} else if (item.getEntryType().getName().equals(BibtexKeyPatternTableViewModel.ENTRY_TYPE_DEFAULT_NAME)) {
setStyle("-fx-background-color: -fx-default-button");
} else {
setStyle("");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jabref.gui.bibtexkeypattern;

import java.util.Collection;
import java.util.Comparator;

import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.types.EntryType;
import org.jabref.preferences.JabRefPreferences;

public class BibtexKeyPatternTableViewModel {

public static final String ENTRY_TYPE_DEFAULT_NAME = "default";

public static Comparator<BibtexKeyPatternTableItemModel> defaultOnTopComparator = (o1, o2) -> {
String itemOneName = o1.getEntryType().getName();
String itemTwoName = o2.getEntryType().getName();

if (itemOneName.equals(itemTwoName)) {
return 0;
} else if (itemOneName.equals(ENTRY_TYPE_DEFAULT_NAME)) {
return -1;
} else if (itemTwoName.equals(ENTRY_TYPE_DEFAULT_NAME)) {
return 1;
}

return 0;
};

private final ListProperty<BibtexKeyPatternTableItemModel> patternListProperty = new SimpleListProperty<>();
private final ObjectProperty<BibtexKeyPatternTableItemModel> defaultItemProperty = new SimpleObjectProperty<>();
private final AbstractBibtexKeyPattern initialKeyPattern;
private final Collection<BibEntryType> bibEntryTypeList;
private final JabRefPreferences preferences;

public BibtexKeyPatternTableViewModel(JabRefPreferences preferences, Collection<BibEntryType> entryTypeList, AbstractBibtexKeyPattern initialKeyPattern) {
this.preferences = preferences;
this.bibEntryTypeList = entryTypeList;
this.initialKeyPattern = initialKeyPattern;
}

public void setValues() {
String defaultPattern;
if ((initialKeyPattern.getDefaultValue() == null) || initialKeyPattern.getDefaultValue().isEmpty()) {
defaultPattern = "";
} else {
defaultPattern = initialKeyPattern.getDefaultValue().get(0);
}

defaultItemProperty.setValue(new BibtexKeyPatternTableItemModel(new DefaultEntryType(), defaultPattern));
patternListProperty.setValue(FXCollections.observableArrayList());
patternListProperty.add(defaultItemProperty.getValue());

bibEntryTypeList.stream()
.map(BibEntryType::getType)
.forEach(entryType -> {
String pattern;
if (initialKeyPattern.isDefaultValue(entryType)) {
pattern = "";
} else {
pattern = initialKeyPattern.getPatterns().get(entryType).get(0);
}
patternListProperty.add(new BibtexKeyPatternTableItemModel(entryType, pattern));
});
}

public void setItemToDefaultPattern(BibtexKeyPatternTableItemModel item) {
item.setPattern((String) preferences.defaults.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN));
}

public void resetAll() {
patternListProperty.forEach(item -> item.setPattern(""));
defaultItemProperty.getValue().setPattern((String) preferences.defaults.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN));
}

public ListProperty<BibtexKeyPatternTableItemModel> patternListProperty() { return patternListProperty; }

public ObjectProperty<BibtexKeyPatternTableItemModel> defaultKeyPatternProperty() { return defaultItemProperty; }

public static class DefaultEntryType implements EntryType {
@Override
public String getName() { return ENTRY_TYPE_DEFAULT_NAME; }

@Override
public String getDisplayName() { return Localization.lang("Default pattern"); }
}
}
Loading

0 comments on commit 2a5507f

Please sign in to comment.