Skip to content

Commit

Permalink
Added LabelPatternPreferences (#1607)
Browse files Browse the repository at this point in the history
* Added LabelPatternPreferences

* Removed static initializer
  • Loading branch information
oscargus authored Jul 28, 2016
1 parent cc22459 commit be54a1f
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 82 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import net.sf.jabref.logic.exporter.ExportFormats;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.net.ProxyAuthenticator;
import net.sf.jabref.logic.net.ProxyPreferences;
import net.sf.jabref.logic.net.ProxyRegisterer;
Expand Down Expand Up @@ -72,6 +74,9 @@ private static void start(String[] args) {
// Read list(s) of journal names and abbreviations
Globals.journalAbbreviationLoader = new JournalAbbreviationLoader();

// Set key pattern based on preferences
LabelPatternUtil.updateDefaultPattern(LabelPatternPreferences.fromPreferences(Globals.prefs));

// Check for running JabRef
RemotePreferences remotePreferences = new RemotePreferences(Globals.prefs);
if (remotePreferences.useRemoteServer()) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/sf/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.sf.jabref.logic.exporter.SavePreferences;
import net.sf.jabref.logic.exporter.SaveSession;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.logging.JabRefLogger;
import net.sf.jabref.logic.search.DatabaseSearcher;
Expand Down Expand Up @@ -435,7 +436,8 @@ private void regenerateBibtexKeys(List<ParserResult> loaded) {
LOGGER.info(Localization.lang("Regenerating BibTeX keys according to metadata"));
for (BibEntry entry : database.getEntries()) {
// try to make a new label
LabelPatternUtil.makeLabel(metaData, database, entry, Globals.prefs);
LabelPatternUtil.makeLabel(metaData, database, entry,
LabelPatternPreferences.fromPreferences(Globals.prefs));
}
} else {
LOGGER.info(Localization.lang("No meta data present in BIB_file. Cannot regenerate BibTeX keys"));
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import net.sf.jabref.logic.exporter.SaveSession;
import net.sf.jabref.logic.l10n.Encodings;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
Expand Down Expand Up @@ -578,7 +579,8 @@ public void run() {
// Finally, set the new keys:
for (BibEntry entry : entries) {
bes = entry;
LabelPatternUtil.makeLabel(bibDatabaseContext.getMetaData(), database, bes, Globals.prefs);
LabelPatternUtil.makeLabel(bibDatabaseContext.getMetaData(), database, bes,
LabelPatternPreferences.fromPreferences(Globals.prefs));
ce.addEdit(new UndoableKeyChange(database, bes, (String) oldvals.get(bes),
bes.getCiteKey()));
}
Expand Down Expand Up @@ -1992,7 +1994,8 @@ public void autoGenerateKeysBeforeSaving() {
for (BibEntry bes : database.getEntries()) {
String oldKey = bes.getCiteKey();
if ((oldKey == null) || oldKey.isEmpty()) {
LabelPatternUtil.makeLabel(bibDatabaseContext.getMetaData(), database, bes, Globals.prefs);
LabelPatternUtil.makeLabel(bibDatabaseContext.getMetaData(), database, bes,
LabelPatternPreferences.fromPreferences(Globals.prefs));
ce.addEdit(new UndoableKeyChange(database, bes, null, bes.getCiteKey()));
any = true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/gui/EntryTypeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.model.EntryTypes;
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 class extends FieldSetComponent to provide some required functionality for the
Expand Down Expand Up @@ -70,7 +72,8 @@ protected void addField(String str) {
return;
}

String testString = LabelPatternUtil.checkLegalKey(s);
String testString = LabelPatternUtil.checkLegalKey(s,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(s) || (s.indexOf('&') >= 0)) {
// Report error and exit.
JOptionPane.showMessageDialog(this, Localization.lang("Entry type names are not allowed to contain white space or the following "
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/gui/FieldSetComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionListener;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* @author alver
Expand Down Expand Up @@ -249,7 +251,8 @@ protected void addField(String str) {
return;
}

String testString = LabelPatternUtil.checkLegalKey(s);
String testString = LabelPatternUtil.checkLegalKey(s,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(s) || (s.indexOf('&') >= 0)) {
// Report error and exit.
JOptionPane.showMessageDialog(this, Localization.lang("Field names are not allowed to contain white space or the following "
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/gui/GenFieldsCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private void okActionPerformed() {
Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
return;
}
String testString = LabelPatternUtil.checkLegalKey(parts[1]);
String testString = LabelPatternUtil.checkLegalKey(parts[1],
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(parts[1]) || (parts[1].indexOf('&') >= 0)) {
// Report error and exit.
JOptionPane.showMessageDialog(this, Localization.lang("Field names are not allowed to contain white space or the following "
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/sf/jabref/gui/ImportInspectionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import net.sf.jabref.logic.groups.GroupTreeNode;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.util.UpdateField;
import net.sf.jabref.model.DuplicateCheck;
Expand Down Expand Up @@ -461,7 +462,8 @@ private void generateKeySelectedEntry() {
database.insertEntry(entry);

// Generate a unique key:
LabelPatternUtil.makeLabel(localMetaData, database, entry, Globals.prefs);
LabelPatternUtil.makeLabel(localMetaData, database, entry,
LabelPatternPreferences.fromPreferences(Globals.prefs));
// Remove the entry from the database again, since we only added it in
// order to
// make sure the key was unique:
Expand Down Expand Up @@ -502,7 +504,8 @@ private void generateKeys() {
entry.setId(IdGenerator.next());
database.insertEntry(entry);

LabelPatternUtil.makeLabel(localMetaData, database, entry, Globals.prefs);
LabelPatternUtil.makeLabel(localMetaData, database, entry,
LabelPatternPreferences.fromPreferences(Globals.prefs));
// Add the generated key to our list:
keys.add(entry.getCiteKey());
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import net.sf.jabref.logic.bibtex.LatexFieldFormatterPreferences;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.search.SearchQueryHighlightListener;
import net.sf.jabref.logic.util.date.TimeStamp;
Expand Down Expand Up @@ -1102,7 +1103,8 @@ public void actionPerformed(ActionEvent event) {
}

// Make sure the key is legal:
String cleaned = LabelPatternUtil.checkLegalKey(newValue);
String cleaned = LabelPatternUtil.checkLegalKey(newValue,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if ((cleaned == null) || cleaned.equals(newValue)) {
textField.setValidBackgroundColor();
} else {
Expand Down Expand Up @@ -1369,7 +1371,7 @@ public void actionPerformed(ActionEvent e) {
}

LabelPatternUtil.makeLabel(panel.getBibDatabaseContext().getMetaData(), panel.getDatabase(), entry,
Globals.prefs);
LabelPatternPreferences.fromPreferences(Globals.prefs));

// Store undo information:
panel.getUndoManager().addEdit(new UndoableKeyChange(panel.getDatabase(), entry, (String) oldValue, entry.getCiteKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.sf.jabref.gui.undo.UndoableKeyChange;
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void update() {
for (BibEntry entry : toGenerateFor) {
String oldKey = entry.getCiteKey();
LabelPatternUtil.makeLabel(panel.getBibDatabaseContext().getMetaData(), panel.getDatabase(), entry,
Globals.prefs);
LabelPatternPreferences.fromPreferences(Globals.prefs));
ce.addEdit(new UndoableKeyChange(panel.getDatabase(), entry, oldKey, entry.getCiteKey()));
}
ce.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jabref.gui.labelpattern.LabelPatternPanel;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.GlobalLabelPattern;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.preferences.JabRefPreferences;

Expand Down Expand Up @@ -90,7 +91,7 @@ public void storeSettings() {
Globals.prefs.putBoolean(JabRefPreferences.KEY_GEN_ALWAYS_ADD_LETTER, false);
}

LabelPatternUtil.updateDefaultPattern();
LabelPatternUtil.updateDefaultPattern(LabelPatternPreferences.fromPreferences(Globals.prefs));

// fetch entries from GUI
GlobalLabelPattern keypatterns = getLabelPatternAsGlobalLabelPattern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import java.util.Map;
import java.util.regex.Pattern;

import net.sf.jabref.Globals;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.model.entry.AuthorList;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* Importer for the Refer/Endnote format.
Expand Down Expand Up @@ -236,7 +238,8 @@ else if ("P".equals(prefix)) {
type = "mastersthesis";
}
} else if ("F".equals(prefix)) {
hm.put(BibEntry.KEY_FIELD, LabelPatternUtil.checkLegalKey(val));
hm.put(BibEntry.KEY_FIELD, LabelPatternUtil.checkLegalKey(val,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternPreferences;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -217,7 +218,8 @@ public ParserResult importEntries(String text) {
// autogenerate label (BibTeX key)
LabelPatternUtil.makeLabel(
JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext().getMetaData(),
JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabase(), e, Globals.prefs);
JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabase(), e,
LabelPatternPreferences.fromPreferences(Globals.prefs));

res.add(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.sf.jabref.logic.labelpattern;

import net.sf.jabref.preferences.JabRefPreferences;

public class LabelPatternPreferences {

private final String defaultLabelPattern;
private final String keyPatternRegex;
private final String keyPatternReplacement;
private final boolean alwaysAddLetter;
private final boolean firstLetterA;
private final boolean enforceLegalKey;

public LabelPatternPreferences(String defaultLabelPattern, String keyPatternRegex, String keyPatternReplacement,
boolean alwaysAddLetter, boolean firstLetterA, boolean enforceLegalKey) {
this.defaultLabelPattern = defaultLabelPattern;
this.keyPatternRegex = keyPatternRegex;
this.keyPatternReplacement = keyPatternReplacement;
this.alwaysAddLetter = alwaysAddLetter;
this.firstLetterA = firstLetterA;
this.enforceLegalKey = enforceLegalKey;
}

public static LabelPatternPreferences fromPreferences(JabRefPreferences jabRefPreferences) {
return new LabelPatternPreferences(jabRefPreferences.get(JabRefPreferences.DEFAULT_LABEL_PATTERN),
jabRefPreferences.get(JabRefPreferences.KEY_PATTERN_REGEX),
jabRefPreferences.get(JabRefPreferences.KEY_PATTERN_REPLACEMENT),
jabRefPreferences.getBoolean(JabRefPreferences.KEY_GEN_ALWAYS_ADD_LETTER),
jabRefPreferences.getBoolean(JabRefPreferences.KEY_GEN_FIRST_LETTER_A),
jabRefPreferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
}

public String getDefaultLabelPattern() {
return defaultLabelPattern;
}

public String getKeyPatternRegex() {
return keyPatternRegex;
}

public String getKeyPatternReplacement() {
return keyPatternReplacement;
}

public boolean isAlwaysAddLetter() {
return alwaysAddLetter;
}

public boolean isFirstLetterA() {
return firstLetterA;
}

public boolean isEnforceLegalKey() {
return enforceLegalKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import net.sf.jabref.model.entry.AuthorList;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -58,16 +57,12 @@ public class LabelPatternUtil {

private static final int CHARS_OF_FIRST = 5;


static {
updateDefaultPattern();
}

private static BibDatabase database;

public static void updateDefaultPattern() {

public static void updateDefaultPattern(LabelPatternPreferences labelPatternPreferences) {
defaultLabelPattern = LabelPatternUtil
.split(JabRefPreferences.getInstance().get(JabRefPreferences.DEFAULT_LABEL_PATTERN));
.split(labelPatternPreferences.getDefaultLabelPattern());
}

/**
Expand Down Expand Up @@ -427,7 +422,8 @@ public static List<String> split(String labelPattern) {
* @param entry a <code>BibEntry</code>
* @return modified BibEntry
*/
public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entry, JabRefPreferences prefs) {
public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entry,
LabelPatternPreferences labelPatternPreferences) {
database = dBase;
String key;
StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -471,12 +467,12 @@ public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entr
}

// Remove all illegal characters from the key.
key = checkLegalKey(stringBuilder.toString());
key = checkLegalKey(stringBuilder.toString(), labelPatternPreferences.isEnforceLegalKey());

// Remove Regular Expressions while generating Keys
String regex = prefs.get(JabRefPreferences.KEY_PATTERN_REGEX);
String regex = labelPatternPreferences.getKeyPatternRegex();
if ((regex != null) && !regex.trim().isEmpty()) {
String replacement = prefs.get(JabRefPreferences.KEY_PATTERN_REPLACEMENT);
String replacement = labelPatternPreferences.getKeyPatternReplacement();
key = key.replaceAll(regex, replacement);
}

Expand All @@ -494,8 +490,8 @@ public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entr
occurrences--; // No change, so we can accept one dupe.
}

boolean alwaysAddLetter = prefs.getBoolean(JabRefPreferences.KEY_GEN_ALWAYS_ADD_LETTER);
boolean firstLetterA = prefs.getBoolean(JabRefPreferences.KEY_GEN_FIRST_LETTER_A);
boolean alwaysAddLetter = labelPatternPreferences.isAlwaysAddLetter();
boolean firstLetterA = labelPatternPreferences.isFirstLetterA();

if (!alwaysAddLetter && (occurrences == 0)) {
// No dupes found, so we can just go ahead.
Expand Down Expand Up @@ -1380,21 +1376,6 @@ private static String[] parseFieldMarker(String arg) {
return parts.toArray(new String[parts.size()]);
}

/**
* This method returns a String similar to the one passed in, except that it is molded into a form that is
* acceptable for bibtex.
* <p>
* Watch-out that the returned string might be of length 0 afterwards.
*
* @param key mayBeNull
*/
public static String checkLegalKey(String key) {
if (key == null) {
return null;
}
return checkLegalKey(key,
JabRefPreferences.getInstance().getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
}

/**
* This method returns a String similar to the one passed in, except that it is molded into a form that is
Expand Down
Loading

0 comments on commit be54a1f

Please sign in to comment.