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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void openWindow(Stage mainStage) {
for (int i = 0; (i < bibDatabases.size()) && (i < mainFrame.getBasePanelCount()); i++) {
ParserResult pr = bibDatabases.get(i);
BasePanel panel = mainFrame.getBasePanelAt(i);
OpenDatabaseAction.performPostOpenActions(panel, pr);
OpenDatabaseAction.performPostOpenActions(panel, pr, mainFrame.getDialogService());
}

LOGGER.debug("Finished adding panels");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javafx.collections.FXCollections;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.customentrytypes.CustomEntryTypesManager;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
Expand All @@ -24,6 +24,8 @@
import org.jabref.model.entry.CustomEntryType;
import org.jabref.model.entry.EntryType;

import org.controlsfx.control.CheckListView;

/**
* This action checks whether any new custom entry types were loaded from this
* BIB file. If so, an offer to remember these entry types is given.
Expand All @@ -36,10 +38,9 @@ public boolean isActionNecessary(ParserResult parserResult) {
}

@Override
public void performAction(BasePanel panel, ParserResult parserResult) {
public void performAction(BasePanel panel, ParserResult parserResult, DialogService dialogService) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult);

List<EntryType> typesToStore = determineEntryTypesToSave(panel, getListOfUnknownAndUnequalCustomizations(parserResult), mode);
List<EntryType> typesToStore = determineEntryTypesToSave(panel, getListOfUnknownAndUnequalCustomizations(parserResult), mode, dialogService);

if (!typesToStore.isEmpty()) {
typesToStore.forEach(type -> EntryTypes.addOrModifyCustomEntryType((CustomEntryType) type, mode));
Expand All @@ -51,13 +52,12 @@ private List<EntryType> getListOfUnknownAndUnequalCustomizations(ParserResult pa
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());
.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) {
private List<EntryType> determineEntryTypesToSave(BasePanel panel, List<EntryType> allCustomizedEntryTypes, BibDatabaseMode databaseMode, DialogService dialogService) {
List<EntryType> newTypes = new ArrayList<>();
List<EntryType> differentCustomizations = new ArrayList<>();

Expand All @@ -72,63 +72,60 @@ private List<EntryType> determineEntryTypesToSave(BasePanel panel, List<EntryTyp
}
}

Map<EntryType, JCheckBox> typeCheckBoxMap = new HashMap<>();
DialogPane pane = new DialogPane();
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 all this dialog-related code to a new class with the usual fxml / view / viewmodel


CheckListView<EntryType> unknownEntryTypesCheckList = new CheckListView<>(FXCollections.observableArrayList(newTypes));

VBox vbox = new VBox();
vbox.getChildren().add(new Label(Localization.lang("Select all customized types to be stored in local preferences") + ":"));
vbox.getChildren().add(new Label(Localization.lang("Currently unknown")));
vbox.getChildren().add(unknownEntryTypesCheckList);

JPanel checkboxPanel = createCheckBoxPanel(newTypes, differentCustomizations, typeCheckBoxMap);
Optional<CheckListView<EntryType>> differentCustomizationCheckList = Optional.empty();
if (!differentCustomizations.isEmpty()) {

int answer = JOptionPane.showConfirmDialog(null,
checkboxPanel,
Localization.lang("Custom entry types"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
differentCustomizationCheckList = Optional.of(new CheckListView<>(FXCollections.observableArrayList(differentCustomizations)));

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();
vbox.getChildren().add(new Label(Localization.lang("Different customization, current settings will be overwritten") + ":"));
vbox.getChildren().add(differentCustomizationCheckList.get());
}

}
pane.setContent(vbox);

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);
}
Optional<ButtonType> buttonPressed = dialogService.showCustomDialogAndWait(Localization.lang("Custom entry types"), pane, ButtonType.OK, ButtonType.CANCEL);
if (buttonPressed.isPresent() && (buttonPressed.get() == ButtonType.OK)) {

List<EntryType> differentCustomizationSelected = new ArrayList<>();
differentCustomizationCheckList.map(view -> view.getCheckModel().getCheckedItems()).ifPresent(differentCustomizationSelected::addAll);

List<EntryType> selectedUnknown = unknownEntryTypesCheckList.getCheckModel().getCheckedItems();

return Stream.concat(selectedUnknown.stream(), differentCustomizationSelected.stream()).collect(Collectors.toList());
}
return Collections.emptyList();
}

// 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);
}
/*
Copy link
Member

Choose a reason for hiding this comment

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

this can be deleted now, right?

// 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);
}
return checkboxPanel;
}

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

private BibDatabaseMode getBibDatabaseModeFromParserResult(ParserResult parserResult) {
return parserResult.getMetaData().getMode().orElse(Globals.prefs.getDefaultBibDatabaseMode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.importer.actions;

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.logic.importer.ParserResult;

/**
Expand Down Expand Up @@ -32,6 +33,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);
void performAction(BasePanel panel, ParserResult pr, DialogService dialogService);
Copy link
Member

Choose a reason for hiding this comment

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

Dependencies as the DialogService should be passed to the constructor and not as a method parameter. This needs a bit more refactoring, but I don't like the static list of post open actions anyway...so this would go into the right direction.

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.logic.importer.ParserResult;
import org.jabref.migrations.MergeReviewIntoCommentMigration;
import org.jabref.model.entry.BibEntry;
Expand All @@ -15,12 +16,12 @@ public boolean isActionNecessary(ParserResult parserResult) {
}

@Override
public void performAction(BasePanel basePanel, ParserResult parserResult) {
public void performAction(BasePanel basePanel, ParserResult parserResult, DialogService dialogService) {
MergeReviewIntoCommentMigration migration = new MergeReviewIntoCommentMigration();

migration.performMigration(parserResult);
List<BibEntry> conflicts = MergeReviewIntoCommentMigration.collectConflicts(parserResult);
if (!conflicts.isEmpty() && new MergeReviewIntoCommentConfirmationDialog(basePanel.frame().getDialogService()).askUserForMerge(conflicts)) {
if (!conflicts.isEmpty() && new MergeReviewIntoCommentConfirmationDialog(dialogService).askUserForMerge(conflicts)) {
migration.performConflictingMigration(parserResult);
}
}
Expand Down
Loading