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

Improve entrytype customization #2331

Merged
merged 27 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
60db50a
entry type customization based on BibdatabaseMode
matthiasgeiger Dec 2, 2016
cbbdd67
Remove unnecessary help method in BibDatabaseMode
matthiasgeiger Dec 2, 2016
4548190
use database mode of current DB
matthiasgeiger Dec 4, 2016
ab10ab1
add new storage of custom entry types
matthiasgeiger Dec 5, 2016
830f59c
load and store databasemode specific lists of custom types
matthiasgeiger Dec 5, 2016
0d45001
rework to use database mode specific custom types
matthiasgeiger Dec 5, 2016
d144954
reset of preferences now also clears bibtexKeyPatterns and customized…
matthiasgeiger Dec 5, 2016
9d275bc
reenable ignored test
matthiasgeiger Dec 5, 2016
379a398
rewrite and add additional tests to EntryTypesTestBibtex
matthiasgeiger Dec 5, 2016
7dafccf
cleanup after tests
matthiasgeiger Dec 5, 2016
e189fd7
add convenience method to get IeeeTranEntryType by name
matthiasgeiger Dec 11, 2016
cd13456
Filter customizations of standard types in NewEntry/ChangeEntryType d…
matthiasgeiger Dec 11, 2016
441964d
Improve customEntryType import dialog
matthiasgeiger Dec 12, 2016
1cc3493
update localization
matthiasgeiger Dec 12, 2016
6d7afc1
minor cleanup and rename
matthiasgeiger Dec 12, 2016
4c6aa25
do not cycle through all entries but only through changed ones
matthiasgeiger Dec 12, 2016
5aae2f9
improve naming
matthiasgeiger Dec 12, 2016
92c5712
different order of fields should indicate different types - using sta…
matthiasgeiger Dec 12, 2016
4ba2c1f
incorporate feedback
matthiasgeiger Dec 12, 2016
283401f
fix codacy
matthiasgeiger Dec 13, 2016
9678cba
migrate customEntryType preferences
matthiasgeiger Dec 13, 2016
fbe70d7
Merge branch 'master' into improve-entrytype-customization
matthiasgeiger Dec 13, 2016
999e493
fix localization duplicates
matthiasgeiger Dec 13, 2016
696bcfc
test entrytypes parametrized
matthiasgeiger Dec 13, 2016
8584a4d
add CHANGELOG for custom type changes/fixes
matthiasgeiger Dec 13, 2016
e247a5d
increase coverage
matthiasgeiger Dec 13, 2016
5e256f5
refactor to adhere to architectural constraints
matthiasgeiger Dec 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/main/java/net/sf/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.metadata.MetaData;
import net.sf.jabref.model.strings.StringUtil;
import net.sf.jabref.preferences.JabRefPreferences;
import net.sf.jabref.preferences.SearchPreferences;
import net.sf.jabref.shared.prefs.SharedDatabasePreferences;

Expand Down Expand Up @@ -299,8 +297,7 @@ private boolean generateAux(List<ParserResult> loaded, String[] data) {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults), prefs);

// Show just a warning message if encoding did not work for all characters:
Expand Down Expand Up @@ -338,8 +335,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
try {
System.out.println(Localization.lang("Saving") + ": " + data[0]);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(
FileSaveSession::new);
SaveSession session = databaseWriter.saveDatabase(
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/net/sf/jabref/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
import net.sf.jabref.model.DuplicateCheck;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.database.EntrySorter;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexString;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.groups.GroupTreeNode;
import net.sf.jabref.model.metadata.MetaData;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -152,8 +150,7 @@ private void storeTempDatabase() {
.withEncoding(panel.getBibDatabaseContext().getMetaData().getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()));

Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
SaveSession ss = databaseWriter.saveDatabase(new BibDatabaseContext(databaseInTemp, metadataInTemp, defaults), prefs);
ss.commit(Globals.getFileUpdateMonitor().getTempFile(panel.fileMonitorHandle()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import net.sf.jabref.Globals;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.database.BibDatabaseMode;
Expand Down Expand Up @@ -289,7 +290,7 @@ private void applyChanges() {
new CustomEntryType(StringUtil.capitalizeFirst(stringListEntry.getKey()), reqStr, optStr, opt2Str) :
new CustomEntryType(StringUtil.capitalizeFirst(stringListEntry.getKey()), reqStr, optStr);

EntryTypes.addOrModifyCustomEntryType(typ);
EntryTypes.addOrModifyCustomEntryType(typ, bibDatabaseMode);
updateTypesForEntries();
}
}
Expand All @@ -309,6 +310,7 @@ private void applyChanges() {
}

updateTables();
CustomEntryTypesManager.saveCustomEntryTypes(Globals.prefs);
}

private void typeDeletion(String name) {
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/net/sf/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

Expand All @@ -28,13 +29,13 @@
import net.sf.jabref.Globals;
import net.sf.jabref.gui.importer.fetcher.EntryFetchers;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibLatexEntryTypes;
import net.sf.jabref.model.entry.BibtexEntryTypes;
import net.sf.jabref.model.entry.EntryType;
import net.sf.jabref.model.entry.IEEETranEntryTypes;
Expand All @@ -59,10 +60,8 @@ public class EntryTypeDialog extends JDialog implements ActionListener {
private JComboBox<String> comboBox;
private final JabRefFrame frame;
private static final int COLUMN = 3;
private final boolean biblatexMode;

private final CancelAction cancelAction = new CancelAction();
private final BibDatabaseContext bibDatabaseContext;

static class TypeButton extends JButton implements Comparable<TypeButton> {

Expand Down Expand Up @@ -90,10 +89,6 @@ public EntryTypeDialog(JabRefFrame frame) {

this.frame = frame;

bibDatabaseContext = frame.getCurrentBasePanel().getBibDatabaseContext();
biblatexMode = bibDatabaseContext.isBiblatexMode();


setTitle(Localization.lang("Select entry type"));

addWindowListener(new WindowAdapter() {
Expand All @@ -115,14 +110,21 @@ private JPanel createEntryGroupsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new VerticalLayout());

if (biblatexMode) {
panel.add(createEntryGroupPanel("BibLateX", EntryTypes.getAllValues(bibDatabaseContext.getMode())));
if (frame.getCurrentBasePanel().getBibDatabaseContext().isBiblatexMode()) {
panel.add(createEntryGroupPanel("BibLateX", BibLatexEntryTypes.ALL));

List<EntryType> customTypes = EntryTypes.getAllCustomTypes(BibDatabaseMode.BIBLATEX);
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to extract the following code in a private method? It is duplicated below and the only difference is the mode.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same as above - helper method would be overkill, imho

if (!customTypes.isEmpty()) {
panel.add(createEntryGroupPanel(Localization.lang("Custom"), customTypes));
}

} else {
panel.add(createEntryGroupPanel("BibTeX", BibtexEntryTypes.ALL));
panel.add(createEntryGroupPanel("IEEETran", IEEETranEntryTypes.ALL));

if (!CustomEntryTypesManager.ALL.isEmpty()) {
panel.add(createEntryGroupPanel(Localization.lang("Custom"), CustomEntryTypesManager.ALL));
List<EntryType> customTypes = EntryTypes.getAllCustomTypes(BibDatabaseMode.BIBTEX);
if (!customTypes.isEmpty()) {
panel.add(createEntryGroupPanel(Localization.lang("Custom"), customTypes));
}
}
panel.add(createIdFetcherPanel());
Expand All @@ -146,7 +148,7 @@ private JPanel createCancelButtonBarPanel() {
return buttons;
}

private JPanel createEntryGroupPanel(String groupTitle, Collection<EntryType> entries) {
private JPanel createEntryGroupPanel(String groupTitle, Collection<? extends EntryType> entries) {
JPanel panel = new JPanel();
GridBagLayout bagLayout = new GridBagLayout();
panel.setLayout(bagLayout);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
import net.sf.jabref.gui.specialfields.SpecialFieldValueViewModel;
import net.sf.jabref.gui.util.WindowLocation;
import net.sf.jabref.gui.worker.MarkEntriesAction;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.autosaveandbackup.AutosaveManager;
import net.sf.jabref.logic.autosaveandbackup.BackupManager;
import net.sf.jabref.logic.help.HelpFile;
Expand Down Expand Up @@ -788,7 +787,6 @@ private void tearDownJabRef(List<String> filenames) {
fileHistory.storeHistory();
prefs.customExports.store(Globals.prefs);
prefs.customImports.store();
CustomEntryTypesManager.saveCustomEntryTypes(prefs);

prefs.flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* The action concerned with generate a new (sub-)database from latex AUX file.
Expand All @@ -39,8 +37,7 @@ public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);

if (dialog.generatePressed()) {
Defaults defaults = new Defaults(
BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BasePanel bp = new BasePanel(jabRefFrame, new BibDatabaseContext(dialog.getGenerateDB(), defaults));
jabRefFrame.addTab(bp, true);
jabRefFrame.output(Localization.lang("New database created."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
import net.sf.jabref.model.DuplicateCheck;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.AuthorList;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
Expand Down Expand Up @@ -705,8 +704,7 @@ private void removeEntriesToDelete(NamedCompound ce) {
private void addSelectedEntries(NamedCompound ce, final List<BibEntry> selected) {
if (newDatabase) {
// Create a new BasePanel for the entries:
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
panel = new BasePanel(frame, new BibDatabaseContext(defaults));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package net.sf.jabref.gui.importer.actions;

import java.util.Arrays;
import java.util.Iterator;
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 net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.CustomEntryType;
import net.sf.jabref.model.entry.EntryType;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* This action checks whether any new custom entry types were loaded from this
Expand All @@ -24,42 +31,105 @@
public class CheckForNewEntryTypesAction implements PostOpenAction {

@Override
public boolean isActionNecessary(ParserResult pr) {
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
BibDatabaseMode mode = new BibDatabaseContext(pr.getDatabase(), pr.getMetaData(), defaults).getMode();
// See if any custom entry types were imported, but disregard those we already know:
for (Iterator<String> i = pr.getEntryTypes().keySet().iterator(); i.hasNext();) {
String typeName = i.next().toLowerCase();
if (EntryTypes.getType(typeName, mode).isPresent()) {
i.remove();
}
}
return !pr.getEntryTypes().isEmpty();
public boolean isActionNecessary(ParserResult parserResult) {
return !getListOfUnknownAndUnequalCustomizations(parserResult).isEmpty();
}

@Override
public void performAction(BasePanel panel, ParserResult pr) {

StringBuilder sb = new StringBuilder();
sb.append(Localization.lang("Custom entry types found in file")).append(": ");
Object[] types = pr.getEntryTypes().keySet().toArray();
Arrays.sort(types);
for (Object type : types) {
sb.append(type).append(", ");
public void performAction(BasePanel panel, ParserResult parserResult) {

BibDatabaseMode mode = parserResult.getMetaData().getMode().orElse(Globals.prefs.getDefaultBibDatabaseMode());
Copy link
Member

Choose a reason for hiding this comment

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

To avoid writing that method chain multiple times in this class, maybe extract a helper method?


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

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

private List<EntryType> getListOfUnknownAndUnequalCustomizations(ParserResult parserResult) {
BibDatabaseMode mode = parserResult.getMetaData().getMode().orElse(Globals.prefs.getDefaultBibDatabaseMode());

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) {
Copy link
Member

Choose a reason for hiding this comment

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

please adhere to the formatting guidelines: for ( and below if (

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);
}
}
}
String s = sb.toString();

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

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

int answer = JOptionPane.showConfirmDialog(panel.frame(),
s.substring(0, s.length() - 2) + ".\n"
+ Localization.lang("Remember these entry types?"),
checkboxPanel,
Localization.lang("Custom entry types"),
JOptionPane.YES_NO_OPTION,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);

if (answer == JOptionPane.YES_OPTION) {
// Import
for (EntryType typ : pr.getEntryTypes().values()) {
EntryTypes.addOrModifyCustomEntryType((CustomEntryType) typ);
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")+".");
Copy link
Member

Choose a reason for hiding this comment

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

Also formatting problems here. I should be sufficient to auto-format the 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;
}
}
Loading