Skip to content

Commit

Permalink
BibDatabaseType --> *Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Jan 26, 2016
1 parent 3236f9e commit 2c82e52
Show file tree
Hide file tree
Showing 47 changed files with 191 additions and 203 deletions.
24 changes: 11 additions & 13 deletions src/main/java/net/sf/jabref/LoadedDatabase.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package net.sf.jabref;

import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseType;
import net.sf.jabref.model.database.BibDatabaseTypeDetection;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.database.BibDatabaseModeDetection;

import java.io.File;
import java.util.Objects;
import java.util.Vector;

public class LoadedDatabase {

private static final String DATABASE_TYPE = "DATABASE_TYPE";

private final BibDatabase database;
private final MetaData metaData;

Expand All @@ -27,7 +25,7 @@ public LoadedDatabase(BibDatabase database, MetaData metaData) {
this.database = Objects.requireNonNull(database);
this.metaData = Objects.requireNonNull(metaData);

this.setType(getType());
this.setMode(getMode());
}

public LoadedDatabase(BibDatabase database, MetaData metaData, File file) {
Expand All @@ -36,18 +34,18 @@ public LoadedDatabase(BibDatabase database, MetaData metaData, File file) {
this.metaData.setFile(file);
}

public BibDatabaseType getType() {
Vector<String> data = metaData.getData(DATABASE_TYPE);
public BibDatabaseMode getMode() {
Vector<String> data = metaData.getData(MetaData.DATABASE_TYPE);
if(data == null) {
return BibDatabaseTypeDetection.inferType(database);
return BibDatabaseModeDetection.inferMode(database);
}
return BibDatabaseType.valueOf(data.get(0));
return BibDatabaseMode.valueOf(data.get(0));
}

public void setType(BibDatabaseType type) {
public void setMode(BibDatabaseMode bibDatabaseMode) {
Vector<String> list = new Vector<>();
list.add(type.name());
metaData.putData(DATABASE_TYPE, list);
list.add(bibDatabaseMode.name());
metaData.putData(MetaData.DATABASE_TYPE, list);
}

/**
Expand All @@ -68,6 +66,6 @@ public MetaData getMetaData() {
}

public boolean isBiblatexMode() {
return getType() == BibDatabaseType.BIBLATEX;
return getMode() == BibDatabaseMode.BIBLATEX;
}
}
1 change: 1 addition & 0 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class MetaData implements Iterable<String> {
public static final String META_FLAG = "jabref-meta: ";
private static final String PREFIX_KEYPATTERN = "keypattern_";
private static final String KEYPATTERNDEFAULT = "keypatterndefault";
static final String DATABASE_TYPE = "DATABASE_TYPE";

private final HashMap<String, Vector<String>> metaData = new HashMap<>();
private GroupTreeNode groupsRoot;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/net/sf/jabref/bibtex/BibEntryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.sf.jabref.Globals;
import net.sf.jabref.exporter.LatexFieldFormatter;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.database.BibDatabaseType;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.EntryType;
import java.io.IOException;
Expand All @@ -26,25 +26,25 @@ public BibEntryWriter(LatexFieldFormatter fieldFormatter, boolean write) {
this.write = write;
}

public void write(BibEntry entry, Writer out, BibDatabaseType bibDatabaseType) throws IOException {
public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
// if the entry has not been modified, write it as it was
if (!entry.hasChanged()) {
out.write(entry.getParsedSerialization());
return;
}
out.write(Globals.NEWLINE + Globals.NEWLINE);

writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseType);
writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseMode);
}

public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabaseType bibDatabaseType) throws IOException {
public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
// if the entry has not been modified, write it as it was
if (!entry.hasChanged()) {
out.write(entry.getParsedSerialization().trim());
return;
}

writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseType);
writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseMode);
}

/**
Expand All @@ -54,9 +54,9 @@ public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabas
* @param out
* @throws IOException
*/
private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Writer out, BibDatabaseType bibDatabaseType) throws IOException {
private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
// Write header with type and bibtex-key.
TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.empty(), bibDatabaseType);
TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.empty(), bibDatabaseMode);
out.write('@' + typedEntry.getTypeForDisplay() + '{');

writeKeyField(entry, out);
Expand All @@ -66,7 +66,7 @@ private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Write
boolean hasWritten = false;
int indentation = getLengthOfLongestFieldName(entry);

EntryType type = EntryTypes.getType(entry.getType(), bibDatabaseType);
EntryType type = EntryTypes.getType(entry.getType(), bibDatabaseMode);

// Write required fields first.
List<String> fields = type.getRequiredFieldsFlat();
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/net/sf/jabref/bibtex/EntryTypes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.sf.jabref.bibtex;

import net.sf.jabref.model.database.BibDatabaseType;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.*;

import java.util.*;
Expand Down Expand Up @@ -100,25 +100,25 @@ public void removeType(String name) {
* This method returns the BibtexEntryType for the name of a type,
* or null if it does not exist.
*/
public static EntryType getType(String name, BibDatabaseType type) {
return type == BibDatabaseType.BIBLATEX ? BIBLATEX.getType(name) : BIBTEX.getType(name);
public static EntryType getType(String name, BibDatabaseMode type) {

This comment has been minimized.

Copy link
@tobiasdiez

tobiasdiez Jan 26, 2016

Member

The name of the parameter should be "mode" instead of "type" :) Probably happens in quite a few places.

return type == BibDatabaseMode.BIBLATEX ? BIBLATEX.getType(name) : BIBTEX.getType(name);
}

/**
* This method returns the EntryType for the name of a type,
* or the default type (*.MISC) if it does not exist.
*/
// Get an entry type defined in BibtexEntryType
public static EntryType getTypeOrDefault(String name, BibDatabaseType type) {
return type == BibDatabaseType.BIBLATEX ? BIBLATEX.getTypeOrDefault(name) : BIBTEX.getTypeOrDefault(name);
public static EntryType getTypeOrDefault(String name, BibDatabaseMode type) {
return type == BibDatabaseMode.BIBLATEX ? BIBLATEX.getTypeOrDefault(name) : BIBTEX.getTypeOrDefault(name);
}

/**
* This method returns the standard BibtexEntryType for the
* name of a type, or null if it does not exist.
*/
public static EntryType getStandardType(String name, BibDatabaseType type) {
return type == BibDatabaseType.BIBLATEX ? BIBLATEX.getStandardType(name) : BIBTEX.getStandardType(name);
public static EntryType getStandardType(String name, BibDatabaseMode type) {
return type == BibDatabaseMode.BIBLATEX ? BIBLATEX.getStandardType(name) : BIBTEX.getStandardType(name);
}

public static void addOrModifyCustomEntryType(CustomEntryType customEntryType) {
Expand All @@ -130,12 +130,12 @@ private static void addOrModifyEntryType(EntryType name) {
BIBTEX.addOrModifyEntryType(name);
}

public static Set<String> getAllTypes(BibDatabaseType type) {
return type == BibDatabaseType.BIBLATEX ? BIBLATEX.getAllTypes() : BIBTEX.getAllTypes();
public static Set<String> getAllTypes(BibDatabaseMode type) {
return type == BibDatabaseMode.BIBLATEX ? BIBLATEX.getAllTypes() : BIBTEX.getAllTypes();
}

public static Collection<EntryType> getAllValues(BibDatabaseType type) {
return type == BibDatabaseType.BIBLATEX ? BIBLATEX.getAllValues() : BIBTEX.getAllValues();
public static Collection<EntryType> getAllValues(BibDatabaseMode type) {
return type == BibDatabaseMode.BIBLATEX ? BIBLATEX.getAllValues() : BIBTEX.getAllValues();
}

/**
Expand All @@ -144,8 +144,8 @@ public static Collection<EntryType> getAllValues(BibDatabaseType type) {
*
* @param name The customized entry type to remove.
*/
public static void removeType(String name, BibDatabaseType type) {
if (type == BibDatabaseType.BIBLATEX) {
public static void removeType(String name, BibDatabaseMode type) {
if (type == BibDatabaseMode.BIBLATEX) {
BIBLATEX.removeType(name);
} else {
BIBTEX.removeType(name);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jabref/exporter/FileActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public static SaveSession saveDatabase(LoadedDatabase loadedDatabase, File targe
// Check if we must write the type definition for this
// entry, as well. Our criterion is that all non-standard
// types (*not* customized standard types) must be written.
EntryType entryType = EntryTypes.getType(entry.getType(), loadedDatabase.getType());
EntryType entryType = EntryTypes.getType(entry.getType(), loadedDatabase.getMode());

if (EntryTypes.getStandardType(entryType.getName(), loadedDatabase.getType()) == null) {
if (EntryTypes.getStandardType(entryType.getName(), loadedDatabase.getMode()) == null) {
types.put(entryType.getName(), entryType);
}

Expand All @@ -242,7 +242,7 @@ public static SaveSession saveDatabase(LoadedDatabase loadedDatabase, File targe
}

if (write) {
bibtexEntryWriter.write(entry, writer, loadedDatabase.getType());
bibtexEntryWriter.write(entry, writer, loadedDatabase.getMode());
}
}

Expand Down Expand Up @@ -418,12 +418,12 @@ public static SaveSession savePartOfDatabase(LoadedDatabase loadedDatabase, File
// Check if we must write the type definition for this
// entry, as well. Our criterion is that all non-standard
// types (*not* customized standard types) must be written.
EntryType tp = EntryTypes.getType(be.getType(), loadedDatabase.getType());
if (EntryTypes.getStandardType(tp.getName(), loadedDatabase.getType()) == null) {
EntryType tp = EntryTypes.getType(be.getType(), loadedDatabase.getMode());
if (EntryTypes.getStandardType(tp.getName(), loadedDatabase.getMode()) == null) {
types.put(tp.getName(), tp);
}

bibtexEntryWriter.write(be, fw, loadedDatabase.getType());
bibtexEntryWriter.write(be, fw, loadedDatabase.getMode());
//only append newline if the entry has changed
if (!be.hasChanged()) {
fw.write(Globals.NEWLINE);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/net/sf/jabref/gui/DuplicateResolverDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package net.sf.jabref.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

Expand Down Expand Up @@ -81,28 +79,28 @@ private void init(BibEntry one, BibEntry two, int type) {
first = new JButton(Localization.lang("Keep left"));
second = new JButton(Localization.lang("Keep right"));
both = new JButton(Localization.lang("Keep both"));
me = new MergeEntries(one, two, frame.getCurrentBasePanel().getLoadedDatabase().getType());
me = new MergeEntries(one, two, frame.getCurrentBasePanel().getLoadedDatabase().getMode());
break;
case INSPECTION:
first = new JButton(Localization.lang("Remove old entry"));
second = new JButton(Localization.lang("Remove entry from import"));
both = new JButton(Localization.lang("Keep both"));
me = new MergeEntries(one, two, Localization.lang("Old entry"),
Localization.lang("From import"), frame.getCurrentBasePanel().getLoadedDatabase().getType());
Localization.lang("From import"), frame.getCurrentBasePanel().getLoadedDatabase().getMode());
break;
case DUPLICATE_SEARCH_WITH_EXACT:
first = new JButton(Localization.lang("Keep left"));
second = new JButton(Localization.lang("Keep right"));
both = new JButton(Localization.lang("Keep both"));
removeExact = new JButton(Localization.lang("Automatically remove exact duplicates"));
me = new MergeEntries(one, two, frame.getCurrentBasePanel().getLoadedDatabase().getType());
me = new MergeEntries(one, two, frame.getCurrentBasePanel().getLoadedDatabase().getMode());
break;
default:
first = new JButton(Localization.lang("Import and remove old entry"));
second = new JButton(Localization.lang("Do not import entry"));
both = new JButton(Localization.lang("Import and keep old entry"));
me = new MergeEntries(one, two, Localization.lang("Old entry"),
Localization.lang("From import"), frame.getCurrentBasePanel().getLoadedDatabase().getType());
Localization.lang("From import"), frame.getCurrentBasePanel().getLoadedDatabase().getMode());
}

if (removeExact != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/DuplicateSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class SearcherRunnable implements Runnable {
public void run() {
for (int i = 0; (i < (bes.length - 1)) && !finished; i++) {
for (int j = i + 1; (j < bes.length) && !finished; j++) {
boolean eq = DuplicateCheck.isDuplicate(bes[i], bes[j], panel.getLoadedDatabase().getType());
boolean eq = DuplicateCheck.isDuplicate(bes[i], bes[j], panel.getLoadedDatabase().getMode());

// If (suspected) duplicates, add them to the duplicates vector.
if (eq) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/net/sf/jabref/gui/EntryCustomizationDialog2.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ private void initGui() {
right.setLayout(new GridLayout(biblatexMode ? 2 : 1, 2));

List<String> entryTypes = new ArrayList<>();
for (String s : EntryTypes.getAllTypes(loadedDatabase.getType())) {
for (String s : EntryTypes.getAllTypes(loadedDatabase.getMode())) {
entryTypes.add(s);
}

typeComp = new EntryTypeList(entryTypes, loadedDatabase.getType());
typeComp = new EntryTypeList(entryTypes, loadedDatabase.getMode());
typeComp.addListSelectionListener(this);
typeComp.addAdditionActionListener(this);
typeComp.addDefaultActionListener(new DefaultListener());
Expand Down Expand Up @@ -180,7 +180,7 @@ public void valueChanged(ListSelectionEvent e) {
}
List<String> rl = reqLists.get(s);
if (rl == null) {
EntryType type = EntryTypes.getType(s,loadedDatabase.getType());
EntryType type = EntryTypes.getType(s,loadedDatabase.getMode());
if (type == null) {
// New entry
reqComp.setFields(new ArrayList<>());
Expand Down Expand Up @@ -248,13 +248,13 @@ private void applyChanges() {
if (defaulted.contains(stringListEntry.getKey())) {
// This type should be reverted to its default setup.
String nm = EntryUtil.capitalizeFirst(stringListEntry.getKey());
EntryTypes.removeType(nm, loadedDatabase.getType());
EntryTypes.removeType(nm, loadedDatabase.getMode());

updateTypesForEntries(nm);
continue;
}

EntryType oldType = EntryTypes.getType(stringListEntry.getKey(), loadedDatabase.getType());
EntryType oldType = EntryTypes.getType(stringListEntry.getKey(), loadedDatabase.getMode());
if (oldType != null) {
List<String> oldReq = oldType.getRequiredFieldsFlat();
List<String> oldOpt = oldType.getOptionalFields();
Expand All @@ -281,7 +281,7 @@ private void applyChanges() {
}

Set<Object> toRemove = new HashSet<>();
for (String o : EntryTypes.getAllTypes(loadedDatabase.getType())) {
for (String o : EntryTypes.getAllTypes(loadedDatabase.getMode())) {
if (!types.contains(o)) {
toRemove.add(o);
}
Expand All @@ -298,10 +298,10 @@ private void applyChanges() {
}

private void typeDeletion(String name) {
EntryType type = EntryTypes.getType(name, loadedDatabase.getType());
EntryType type = EntryTypes.getType(name, loadedDatabase.getMode());

if (type instanceof CustomEntryType) {
if (EntryTypes.getStandardType(name, loadedDatabase.getType()) == null) {
if (EntryTypes.getStandardType(name, loadedDatabase.getMode()) == null) {
int reply = JOptionPane.showConfirmDialog
(frame, Localization.lang("All entries of this "
+ "type will be declared "
Expand All @@ -313,7 +313,7 @@ private void typeDeletion(String name) {
return;
}
}
EntryTypes.removeType(name, loadedDatabase.getType());
EntryTypes.removeType(name, loadedDatabase.getMode());
updateTypesForEntries(EntryUtil.capitalizeFirst(name));
changed.remove(name);
reqLists.remove(name);
Expand Down Expand Up @@ -373,7 +373,7 @@ private void updateTypesForEntries(String typeName) {
bp.entryEditors.remove(typeName);

for (BibEntry entry : bp.database().getEntries()) {
EntryType newType = EntryTypes.getType(entry.getType(), loadedDatabase.getType());
EntryType newType = EntryTypes.getType(entry.getType(), loadedDatabase.getMode());
if (newType != null) {
entry.setType(newType);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ public void actionPerformed(ActionEvent e) {
}
defaulted.add(lastSelected);

EntryType type = EntryTypes.getStandardType(lastSelected, loadedDatabase.getType());
EntryType type = EntryTypes.getStandardType(lastSelected, loadedDatabase.getMode());
if (type != null) {
List<String> of = type.getOptionalFields();
List<String> req = type.getRequiredFields();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.jgoodies.forms.builder.ButtonBarBuilder;
import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.LoadedDatabase;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.CustomEntryTypesManager;
Expand Down Expand Up @@ -92,7 +91,7 @@ private JPanel createEntryGroupsPanel() {
panel.setLayout(new VerticalLayout());

if(biblatexMode) {
panel.add(createEntryGroupPanel("BibLateX", EntryTypes.getAllValues(loadedDatabase.getType())));
panel.add(createEntryGroupPanel("BibLateX", EntryTypes.getAllValues(loadedDatabase.getMode())));
} else {
panel.add(createEntryGroupPanel("BibTeX", BibtexEntryTypes.ALL));
panel.add(createEntryGroupPanel("IEEETran", IEEETranEntryTypes.ALL));
Expand Down
Loading

0 comments on commit 2c82e52

Please sign in to comment.