Skip to content

Commit

Permalink
Removed one Globals call from MetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Aug 23, 2016
1 parent c9921ba commit 120f5f8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import net.sf.jabref.model.bibtexkeypattern.DatabaseBibtexKeyPattern;
import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.FieldName;

Expand Down Expand Up @@ -250,12 +251,12 @@ private static Optional<String> getNextUnit(Reader reader) throws IOException {
/**
* @return the stored label patterns
*/
public AbstractBibtexKeyPattern getBibtexKeyPattern() {
public AbstractBibtexKeyPattern getBibtexKeyPattern(GlobalBibtexKeyPattern globalPattern) {
if (bibtexKeyPattern != null) {
return bibtexKeyPattern;
}

bibtexKeyPattern = new DatabaseBibtexKeyPattern(Globals.prefs);
bibtexKeyPattern = new DatabaseBibtexKeyPattern(globalPattern);

// read the data from the metadata and store it into the bibtexKeyPattern
for (String key : this) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/gui/BibtexKeyPatternDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import net.sf.jabref.Globals;
import net.sf.jabref.MetaData;
import net.sf.jabref.gui.bibtexkeypattern.BibtexKeyPatternPanel;
import net.sf.jabref.gui.keyboard.KeyBinder;
Expand Down Expand Up @@ -58,7 +59,7 @@ public BibtexKeyPatternDialog(JabRefFrame parent, BasePanel panel) {
public void setPanel(BasePanel panel) {
this.panel = panel;
this.metaData = panel.getBibDatabaseContext().getMetaData();
AbstractBibtexKeyPattern keypatterns = metaData.getBibtexKeyPattern();
AbstractBibtexKeyPattern keypatterns = metaData.getBibtexKeyPattern(Globals.prefs.getKeyPattern());
bibtexKeyPatternPanel.setValues(keypatterns);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected GlobalBibtexKeyPattern getKeyPatternAsGlobalBibtexKeyPattern() {
}

public DatabaseBibtexKeyPattern getKeyPatternAsDatabaseBibtexKeyPattern() {
DatabaseBibtexKeyPattern res = new DatabaseBibtexKeyPattern(Globals.prefs);
DatabaseBibtexKeyPattern res = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
fillPatternUsingPanelData(res);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sf.jabref.logic.bibtexkeypattern;

import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
import net.sf.jabref.preferences.JabRefPreferences;

public class BibtexKeyPatternPreferences {
Expand All @@ -10,15 +11,18 @@ public class BibtexKeyPatternPreferences {
private final boolean alwaysAddLetter;
private final boolean firstLetterA;
private final boolean enforceLegalKey;
private final GlobalBibtexKeyPattern keyPattern;

public BibtexKeyPatternPreferences(String defaultBibtexKeyPattern, String keyPatternRegex, String keyPatternReplacement,
boolean alwaysAddLetter, boolean firstLetterA, boolean enforceLegalKey) {
boolean alwaysAddLetter, boolean firstLetterA, boolean enforceLegalKey,
GlobalBibtexKeyPattern keyPattern) {
this.defaultBibtexKeyPattern = defaultBibtexKeyPattern;
this.keyPatternRegex = keyPatternRegex;
this.keyPatternReplacement = keyPatternReplacement;
this.alwaysAddLetter = alwaysAddLetter;
this.firstLetterA = firstLetterA;
this.enforceLegalKey = enforceLegalKey;
this.keyPattern = keyPattern;
}

public static BibtexKeyPatternPreferences fromPreferences(JabRefPreferences jabRefPreferences) {
Expand All @@ -27,7 +31,8 @@ public static BibtexKeyPatternPreferences fromPreferences(JabRefPreferences jabR
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));
jabRefPreferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY),
jabRefPreferences.getKeyPattern());
}

public String getKeyPatternRegex() {
Expand All @@ -51,4 +56,8 @@ public boolean isEnforceLegalKey() {
}

public String getDefaultBibtexKeyPattern() { return defaultBibtexKeyPattern;}

public GlobalBibtexKeyPattern getKeyPattern() {
return keyPattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entr
// get the type of entry
String entryType = entry.getType();
// Get the arrayList corresponding to the type
List<String> typeList = new ArrayList<>(metaData.getBibtexKeyPattern().getValue(entryType));
List<String> typeList = new ArrayList<>(
metaData.getBibtexKeyPattern(bibtexKeyPatternPreferences.getKeyPattern()).getValue(entryType));
if (!typeList.isEmpty()) {
typeList.remove(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@

import java.util.List;

import net.sf.jabref.preferences.JabRefPreferences;

public class DatabaseBibtexKeyPattern extends AbstractBibtexKeyPattern {

private final JabRefPreferences prefs;
private final GlobalBibtexKeyPattern globalBibtexKeyPattern;


public DatabaseBibtexKeyPattern(JabRefPreferences prefs) {
this.prefs = prefs;
public DatabaseBibtexKeyPattern(GlobalBibtexKeyPattern globalBibtexKeyPattern) {
this.globalBibtexKeyPattern = globalBibtexKeyPattern;
}

@Override
public List<String> getLastLevelBibtexKeyPattern(String key) {
return prefs.getKeyPattern().getValue(key);
return globalBibtexKeyPattern.getValue(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void writeEpilogueAndEncoding() throws Exception {

@Test
public void writeMetadata() throws Exception {
DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(Globals.prefs);
DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
bibtexKeyPattern.setDefaultValue("test");
metaData.setBibtexKeyPattern(bibtexKeyPattern);

Expand All @@ -172,7 +172,7 @@ public void writeMetadata() throws Exception {
@Test
public void writeMetadataAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(Globals.prefs);
DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
bibtexKeyPattern.setDefaultValue("test");
metaData.setBibtexKeyPattern(bibtexKeyPattern);

Expand Down Expand Up @@ -429,7 +429,7 @@ public void writeSaveOrderConfig() throws Exception {

@Test
public void writeCustomKeyPattern() throws Exception {
AbstractBibtexKeyPattern pattern = new DatabaseBibtexKeyPattern(Globals.prefs);
AbstractBibtexKeyPattern pattern = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
pattern.setDefaultValue("test");
pattern.addBibtexKeyPattern("article", "articleTest");
metaData.setBibtexKeyPattern(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1512,9 +1512,10 @@ public void integrationTestCustomKeyPattern() throws IOException {
+ "@comment{jabref-meta: keypatterndefault:test;}"),
importFormatPreferences);

AbstractBibtexKeyPattern bibtexKeyPattern = result.getMetaData().getBibtexKeyPattern();
AbstractBibtexKeyPattern bibtexKeyPattern = result.getMetaData()
.getBibtexKeyPattern(Globals.prefs.getKeyPattern());

AbstractBibtexKeyPattern expectedPattern = new DatabaseBibtexKeyPattern(Globals.prefs);
AbstractBibtexKeyPattern expectedPattern = new DatabaseBibtexKeyPattern(Globals.prefs.getKeyPattern());
expectedPattern.setDefaultValue("test");
expectedPattern.addBibtexKeyPattern("article", "articleTest");

Expand Down

0 comments on commit 120f5f8

Please sign in to comment.