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

Rework PosteOpenActions to javafx (custom entry type import) #4898

Merged
merged 9 commits into from
Apr 24, 2019
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,57 @@
package org.jabref.gui.importer;

import java.util.List;

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 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;

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);

boxDifferentCustomization.managedProperty().bind(Bindings.isNotEmpty(viewModel.differentCustomizationsProperty()));
unknownEntryTypesCheckList.itemsProperty().bind(viewModel.newTypesProperty());
differentCustomizationCheckList.itemsProperty().bind(viewModel.differentCustomizationsProperty());
}

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

import java.util.List;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.Globals;
import org.jabref.gui.customentrytypes.CustomEntryTypesManager;
import org.jabref.model.EntryTypes;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.CustomEntryType;
import org.jabref.model.entry.EntryType;

public class ImportCustomEntryTypesDialogViewModel {

private final ListProperty<EntryType> newTypesProperty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for an ListProperty here, an observable list is sufficient (the list property is handy if you want to replace the complete list and need listeners for this).

private final ListProperty<EntryType> differentCustomizationsProperty;
private final BibDatabaseMode mode;

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

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

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);
}
}
}

newTypesProperty = new SimpleListProperty<>(newTypes);
differentCustomizationsProperty = new SimpleListProperty<>(differentCustomizationTypes);

}

public ListProperty<EntryType> newTypesProperty() {
return this.newTypesProperty;
}

public ListProperty<EntryType> differentCustomizationsProperty() {
return this.differentCustomizationsProperty;
}

public void importCustomEntryTypes(List<EntryType> checkedUnknownEntryTypes, List<EntryType> checkedDifferentEntryTypes) {
if (!checkedUnknownEntryTypes.isEmpty()) {
checkedUnknownEntryTypes.forEach(type -> EntryTypes.addOrModifyCustomEntryType((CustomEntryType) type, mode));
CustomEntryTypesManager.saveCustomEntryTypes(Globals.prefs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract the Globals.prefs dependency (and even better move the saveCustomEntryTypes method to the preferences class)

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

}
}
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 dlg = new ImportCustomEntryTypesDialog(mode, getListOfUnknownAndUnequalCustomizations(parserResult));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no abbreviations

dlg.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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface GUIPostOpenAction {
*
* @param panel The BasePanel where the database is shown.
* @param pr The result of the BIB parse operation.
* @param dialogService
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed

*/
void performAction(BasePanel panel, ParserResult pr);
}
Loading