Skip to content

Commit

Permalink
Rework PosteOpenActions to javafx (custom entry type import) (#4898)
Browse files Browse the repository at this point in the history
* Rework PosteOpenActions to javafx (custom entry type import)

Convert dialog for importing custom entry types to CheckListView
Rework threading when opening a database

* Rework PosteOpenActions to javafx (custom entry type import)

Convert dialog for importing custom entry types to CheckListView
Rework threading when opening a database

* rework threading stuff
simplify code

* rework dialog, create fxml etc
fix l10n
Remove obsolete code

* remove dialog service argument

* remove dialogService parameter

* Refactorings, move functionality of CustomEntryTypesManager to Preferences
Use ObservableList instead of LIstProperty
  • Loading branch information
Siedlerchr authored Apr 24, 2019
2 parents aab77a1 + 08b583d commit ca95b6d
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 244 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import org.controlsfx.control.CheckListView?>

<DialogPane prefHeight="550.0" prefWidth="566.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.importer.ImportCustomEntryTypesDialog">
<content>
<VBox minHeight="-Infinity" prefHeight="113.0" prefWidth="571.0" spacing="1.0">
<children>
<Label text="%Custom entry types found in file" />
<Label text="%Select all customized types to be stored in local preferences:" />
<Label text="%Currently unknown:" />
<CheckListView fx:id="unknownEntryTypesCheckList" />
<VBox fx:id="boxDifferentCustomization">
<children>
<Label text="%Different customization, current settings will be overwritten" />
<CheckListView fx:id="differentCustomizationCheckList" />
</children>
</VBox>
</children>
</VBox>
</content>
<buttonTypes>
<ButtonType fx:constant="CANCEL" />
<ButtonType fx:constant="OK" />
</buttonTypes>
</DialogPane>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.jabref.gui.importer;

import java.util.List;

import javax.inject.Inject;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;

import org.jabref.gui.util.BaseDialog;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.EntryType;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;
import org.controlsfx.control.CheckListView;

public class ImportCustomEntryTypesDialog extends BaseDialog<Void> {

@FXML private CheckListView<EntryType> unknownEntryTypesCheckList;
@FXML private VBox boxDifferentCustomization;
@FXML private CheckListView<EntryType> differentCustomizationCheckList;
@Inject private PreferencesService preferencesService;

private ImportCustomEntryTypesDialogViewModel viewModel;

private final BibDatabaseMode mode;
private final List<EntryType> customEntryTypes;

public ImportCustomEntryTypesDialog(BibDatabaseMode mode, List<EntryType> customEntryTypes) {
this.mode = mode;
this.customEntryTypes = customEntryTypes;

ViewLoader.view(this)
.load()
.setAsDialogPane(this);

setResultConverter(btn -> {
if (btn == ButtonType.OK) {
viewModel.importCustomEntryTypes(unknownEntryTypesCheckList.getCheckModel().getCheckedItems(), differentCustomizationCheckList.getCheckModel().getCheckedItems());
}
return null;
});

setTitle(Localization.lang("Custom entry types"));

}

@FXML
public void initialize() {
viewModel = new ImportCustomEntryTypesDialogViewModel(mode, customEntryTypes, preferencesService);

boxDifferentCustomization.managedProperty().bind(Bindings.isNotEmpty(viewModel.differentCustomizations()));
unknownEntryTypesCheckList.setItems(viewModel.newTypes());
differentCustomizationCheckList.setItems(viewModel.differentCustomizations());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jabref.gui.importer;

import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.model.EntryTypes;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.CustomEntryType;
import org.jabref.model.entry.EntryType;
import org.jabref.preferences.PreferencesService;

public class ImportCustomEntryTypesDialogViewModel {

private final BibDatabaseMode mode;
private final PreferencesService preferencesService;

private final ObservableList<EntryType> newTypes = FXCollections.observableArrayList();
private final ObservableList<EntryType> differentCustomizationTypes = FXCollections.observableArrayList();

public ImportCustomEntryTypesDialogViewModel(BibDatabaseMode mode, List<EntryType> customEntryTypes, PreferencesService preferencesService) {
this.mode = mode;
this.preferencesService = preferencesService;

for (EntryType customType : customEntryTypes) {
if (!EntryTypes.getType(customType.getName(), mode).isPresent()) {
newTypes.add(customType);
} else {
EntryType currentlyStoredType = EntryTypes.getType(customType.getName(), mode).get();
if (!EntryTypes.isEqualNameAndFieldBased(customType, currentlyStoredType)) {
differentCustomizationTypes.add(customType);
}
}
}

}

public ObservableList<EntryType> newTypes() {
return this.newTypes;
}

public ObservableList<EntryType> differentCustomizations() {
return this.differentCustomizationTypes;
}

public void importCustomEntryTypes(List<EntryType> checkedUnknownEntryTypes, List<EntryType> checkedDifferentEntryTypes) {
if (!checkedUnknownEntryTypes.isEmpty()) {
checkedUnknownEntryTypes.forEach(type -> EntryTypes.addOrModifyCustomEntryType((CustomEntryType) type, mode));
preferencesService.saveCustomEntryTypes();
}
if (!checkedDifferentEntryTypes.isEmpty()) {
checkedUnknownEntryTypes.forEach(type -> EntryTypes.addOrModifyCustomEntryType((CustomEntryType) type, mode));
preferencesService.saveCustomEntryTypes();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
package org.jabref.gui.importer.actions;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.customentrytypes.CustomEntryTypesManager;
import org.jabref.gui.importer.ImportCustomEntryTypesDialog;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.EntryTypes;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.CustomEntryType;
import org.jabref.model.entry.EntryType;

/**
Expand All @@ -39,94 +26,18 @@ public boolean isActionNecessary(ParserResult parserResult) {
public void performAction(BasePanel panel, ParserResult parserResult) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult);

List<EntryType> typesToStore = determineEntryTypesToSave(panel, getListOfUnknownAndUnequalCustomizations(parserResult), mode);
ImportCustomEntryTypesDialog importCustomEntryTypesDialog = new ImportCustomEntryTypesDialog(mode, getListOfUnknownAndUnequalCustomizations(parserResult));
importCustomEntryTypesDialog.showAndWait();

if (!typesToStore.isEmpty()) {
typesToStore.forEach(type -> EntryTypes.addOrModifyCustomEntryType((CustomEntryType) type, mode));
CustomEntryTypesManager.saveCustomEntryTypes(Globals.prefs);
}
}

private List<EntryType> getListOfUnknownAndUnequalCustomizations(ParserResult parserResult) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult);

return parserResult.getEntryTypes().values().stream()
.filter(type ->
(!EntryTypes.getType(type.getName(), mode).isPresent())
|| !EntryTypes.isEqualNameAndFieldBased(type, EntryTypes.getType(type.getName(), mode).get()))
.collect(Collectors.toList());
}

private List<EntryType> determineEntryTypesToSave(BasePanel panel, List<EntryType> allCustomizedEntryTypes, BibDatabaseMode databaseMode) {
List<EntryType> newTypes = new ArrayList<>();
List<EntryType> differentCustomizations = new ArrayList<>();

for (EntryType customType : allCustomizedEntryTypes) {
if (!EntryTypes.getType(customType.getName(), databaseMode).isPresent()) {
newTypes.add(customType);
} else {
EntryType currentlyStoredType = EntryTypes.getType(customType.getName(), databaseMode).get();
if (!EntryTypes.isEqualNameAndFieldBased(customType, currentlyStoredType)) {
differentCustomizations.add(customType);
}
}
}

Map<EntryType, JCheckBox> typeCheckBoxMap = new HashMap<>();

JPanel checkboxPanel = createCheckBoxPanel(newTypes, differentCustomizations, typeCheckBoxMap);

int answer = JOptionPane.showConfirmDialog(null,
checkboxPanel,
Localization.lang("Custom entry types"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);

if (answer == JOptionPane.YES_OPTION) {
return typeCheckBoxMap.entrySet().stream().filter(entry -> entry.getValue().isSelected())
.map(Map.Entry::getKey).collect(Collectors.toList());
} else {
return Collections.emptyList();
}

}

private JPanel createCheckBoxPanel(List<EntryType> newTypes, List<EntryType> differentCustomizations,
Map<EntryType, JCheckBox> typeCheckBoxMap) {
JPanel checkboxPanel = new JPanel();
checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.PAGE_AXIS));

JLabel customFoundLabel = new JLabel(Localization.lang("Custom entry types found in file") + ".");
Font boldStandardFont = new Font(customFoundLabel.getFont().getFontName(), Font.BOLD, customFoundLabel.getFont().getSize());
customFoundLabel.setFont(boldStandardFont);
checkboxPanel.add(customFoundLabel);

JLabel selectLabel = new JLabel(Localization.lang("Select all customized types to be stored in local preferences") + ":");
selectLabel.setFont(boldStandardFont);
checkboxPanel.add(selectLabel);

checkboxPanel.add(new JLabel(" "));

// add all unknown types:
if (!newTypes.isEmpty()) {
checkboxPanel.add(new JLabel(Localization.lang("Currently unknown") + ":"));
for (EntryType type : newTypes) {
JCheckBox box = new JCheckBox(type.getName(), true);
checkboxPanel.add(box);
typeCheckBoxMap.put(type, box);
}
}

// add all different customizations
if (!differentCustomizations.isEmpty()) {
checkboxPanel.add(new JLabel(Localization.lang("Different customization, current settings will be overwritten") + ":"));
for (EntryType type : differentCustomizations) {
JCheckBox box = new JCheckBox(type.getName(), true);
checkboxPanel.add(box);
typeCheckBoxMap.put(type, box);
}
}
return checkboxPanel;
.filter(type -> (!EntryTypes.getType(type.getName(), mode).isPresent())
|| !EntryTypes.isEqualNameAndFieldBased(type, EntryTypes.getType(type.getName(), mode).get()))
.collect(Collectors.toList());
}

private BibDatabaseMode getBibDatabaseModeFromParserResult(ParserResult parserResult) {
Expand Down
Loading

0 comments on commit ca95b6d

Please sign in to comment.