-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
7701375
9b7b764
113d5d7
9358343
af992a3
2d0f4dd
8c302f5
c0327a6
08b583d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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. | ||
|
@@ -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)); | ||
|
@@ -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<>(); | ||
|
||
|
@@ -72,63 +72,60 @@ private List<EntryType> determineEntryTypesToSave(BasePanel panel, List<EntryTyp | |
} | ||
} | ||
|
||
Map<EntryType, JCheckBox> typeCheckBoxMap = new HashMap<>(); | ||
DialogPane pane = new DialogPane(); | ||
|
||
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); | ||
} | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
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; | ||
|
||
/** | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
There was a problem hiding this comment.
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