Skip to content

Commit

Permalink
Tests green, feature works.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Jan 29, 2016
1 parent aa1c4e5 commit b51f3ad
Show file tree
Hide file tree
Showing 122 changed files with 821 additions and 554 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Implemented [#668](https://github.com/JabRef/jabref/issues/668): Replace clear with icon to reduce search bar width
- Improved layout for OSX: Toolbar buttons and search field
- Migrated JabRef help to markdown at https://github.com/JabRef/help.jabref.org
- BibTeX and BibLaTeX mode is now file based and can be switched at runtime. The information is stored in the .bib file, and if it is not there detected by the entry types.

### Fixed
- Make BibTex parser more robust against missing newlines
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/net/sf/jabref/BibDatabaseContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package net.sf.jabref;

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

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Represents everything related to a .bib file.
* <p>
* The entries are stored in BibDatabase, the other data in MetaData and the options relevant for this file in Defaults.
*/
public class BibDatabaseContext {

private final BibDatabase database;
private final MetaData metaData;
private final Defaults defaults;

public BibDatabaseContext(Defaults defaults) {
this(new BibDatabase(), defaults);
}

public BibDatabaseContext(BibDatabase database, Defaults defaults) {
this(database, new MetaData(), defaults);
}

public BibDatabaseContext(BibDatabase database, MetaData metaData, Defaults defaults) {
this.defaults = Objects.requireNonNull(defaults);
this.database = Objects.requireNonNull(database);
this.metaData = Objects.requireNonNull(metaData);

this.setMode(getMode());
}

public BibDatabaseContext(BibDatabase database, MetaData metaData, File file, Defaults defaults) {
this(database, metaData, defaults);

this.metaData.setFile(file);
}

public BibDatabaseMode getMode() {
List<String> data = metaData.getData(MetaData.DATABASE_TYPE);
if (data == null) {
BibDatabaseMode inferredMode = BibDatabaseModeDetection.inferMode(database);
if (defaults.mode == BibDatabaseMode.BIBLATEX || inferredMode == BibDatabaseMode.BIBLATEX) {
return BibDatabaseMode.BIBLATEX;
} else {
return BibDatabaseMode.BIBTEX;
}
}
return BibDatabaseMode.valueOf(data.get(0).toUpperCase());
}

public void setMode(BibDatabaseMode bibDatabaseMode) {
metaData.putData(MetaData.DATABASE_TYPE, Collections.singletonList(bibDatabaseMode.getFormattedName()));
}

/**
* Get the file where this database was last saved to or loaded from, if any.
*
* @return The relevant File, or null if none is defined.
*/
public File getDatabaseFile() {
return metaData.getFile();
}

public BibDatabase getDatabase() {
return database;
}

public MetaData getMetaData() {
return metaData;
}

public boolean isBiblatexMode() {
return getMode() == BibDatabaseMode.BIBLATEX;
}
}
7 changes: 5 additions & 2 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.migrations.PreferencesMigrations;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.util.Util;
import net.sf.jabref.wizard.auximport.AuxCommandLine;
Expand Down Expand Up @@ -308,8 +309,9 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
if (!pr.isInvalid()) {
try {
System.out.println(Localization.lang("Saving") + ": " + data[0]);
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession session = FileActions.saveDatabase(
new LoadedDatabase(pr.getDatabase(), pr.getMetaData()),
new BibDatabaseContext(pr.getDatabase(), pr.getMetaData(), defaults),
new File(data[0]), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
// Show just a warning message if encoding didn't work for all characters:
Expand Down Expand Up @@ -397,7 +399,8 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in

try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SaveSession session = FileActions.saveDatabase(new LoadedDatabase(newBase, new MetaData()),
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession session = FileActions.saveDatabase(new BibDatabaseContext(newBase, defaults),
new File(subName), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
// Show just a warning message if encoding didn't work for all characters:
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public final class JabRefPreferences {
public static final String ABBR_AUTHOR_NAMES = "abbrAuthorNames";
public static final String NAMES_NATBIB = "namesNatbib";
public static final String NAMES_FIRST_LAST = "namesFf";
public static final String BIBLATEX_MODE = "biblatexMode";
public static final String NAMES_AS_IS = "namesAsIs";
public static final String TABLE_COLOR_CODES_ON = "tableColorCodesOn";
public static final String ENTRY_EDITOR_HEIGHT = "entryEditorHeight";
Expand Down Expand Up @@ -430,6 +431,8 @@ private JabRefPreferences() {
defaults.put(LATEX_EDITOR_PATH, OS.guessProgramPath("LEd", "LEd"));
defaults.put(TEXSTUDIO_PATH, OS.guessProgramPath("texstudio", "TeXstudio"));

defaults.put(BIBLATEX_MODE, false);

if (OS.OS_X) {
//defaults.put(JabRefPreferences.PDFVIEWER, "/Applications/Preview.app");
//defaults.put(JabRefPreferences.PSVIEWER, "/Applications/Preview.app");
Expand Down
71 changes: 0 additions & 71 deletions src/main/java/net/sf/jabref/LoadedDatabase.java

This file was deleted.

6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;

import net.sf.jabref.model.database.BibDatabaseMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -77,7 +78,7 @@ public ChangeScanner(JabRefFrame frame, BasePanel bp, File file) { //, BibDataba
this.panel = bp;
this.frame = frame;
this.inMem = bp.database();
this.mdInMem = bp.getLoadedDatabase().getMetaData();
this.mdInMem = bp.getBibDatabaseContext().getMetaData();
this.f = file;
}

Expand Down Expand Up @@ -158,7 +159,8 @@ private void storeTempDatabase() {
@Override
public void run() {
try {
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(inTemp, mdInTemp),
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession ss = FileActions.saveDatabase(new BibDatabaseContext(inTemp, mdInTemp, defaults),
Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle()), Globals.prefs,
false, false, panel.getEncoding(), true);
ss.commit();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/collab/GroupChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public GroupChange(GroupTreeNode changedGroups, GroupTreeNode tmpGroupRoot) {

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
final GroupTreeNode root = panel.getLoadedDatabase().getMetaData().getGroups();
final GroupTreeNode root = panel.getBibDatabaseContext().getMetaData().getGroups();
final UndoableModifySubtree undo = new UndoableModifySubtree(
panel.getGroupSelector(), panel.getLoadedDatabase().getMetaData().getGroups(),
panel.getGroupSelector(), panel.getBibDatabaseContext().getMetaData().getGroups(),
root, Localization.lang("Modified groups"));
root.removeAllChildren();
if (changedGroups == null) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/net/sf/jabref/exporter/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package net.sf.jabref.exporter;

import net.sf.jabref.LoadedDatabase;
import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Defaults;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.model.database.BibDatabaseMode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void run() {
}

for (BasePanel panel : panels) {
if (panel.isModified() && (panel.getLoadedDatabase().getDatabaseFile() != null)) {
if (panel.isModified() && (panel.getBibDatabaseContext().getDatabaseFile() != null)) {
AutoSaveManager.autoSave(panel);
}
}
Expand All @@ -95,10 +94,11 @@ public static File getAutoSaveFile(File f) {
* @return true if successful, false otherwise.
*/
private static boolean autoSave(BasePanel panel) {
File databaseFile = panel.getLoadedDatabase().getDatabaseFile();
File databaseFile = panel.getBibDatabaseContext().getDatabaseFile();
File backupFile = AutoSaveManager.getAutoSaveFile(databaseFile);
try {
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(panel.database(), panel.getLoadedDatabase().getMetaData()),
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession ss = FileActions.saveDatabase(new BibDatabaseContext(panel.database(), panel.getBibDatabaseContext().getMetaData(), defaults),

This comment has been minimized.

Copy link
@tobiasdiez

tobiasdiez Jan 29, 2016

Member

Why create a new BibDatabaseContext here instead of reusing panel.gitBibDatabaseContext ?

This comment has been minimized.

Copy link
@simonharrer

simonharrer Jan 30, 2016

Author Contributor

Good point. Fixed.

backupFile, Globals.prefs, false, false, panel.getEncoding(), true);
ss.commit();
} catch (SaveException e) {
Expand All @@ -117,10 +117,10 @@ private static boolean autoSave(BasePanel panel) {
* @return true if there was no autosave or if the autosave was successfully deleted, false otherwise.
*/
public static boolean deleteAutoSaveFile(BasePanel panel) {
if (panel.getLoadedDatabase().getDatabaseFile() == null) {
if (panel.getBibDatabaseContext().getDatabaseFile() == null) {
return true;
}
File backupFile = AutoSaveManager.getAutoSaveFile(panel.getLoadedDatabase().getDatabaseFile());
File backupFile = AutoSaveManager.getAutoSaveFile(panel.getBibDatabaseContext().getDatabaseFile());
if (backupFile.exists()) {
return backupFile.delete();
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sf/jabref/exporter/ExportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ public void actionPerformed(ActionEvent e) {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getLoadedDatabase().getMetaData()
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData()
.getFileDirectory(Globals.FILE_FIELD).toArray(new String[0]);
// Also store the database's file in a global variable:
Globals.prefs.databaseFile = frame.getCurrentBasePanel().getLoadedDatabase().getDatabaseFile();
Globals.prefs.databaseFile = frame.getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile();

// Make sure we remember which filter was used, to set
// the default for next time:
Expand All @@ -228,7 +228,7 @@ public void actionPerformed(ActionEvent e) {
public void run() {
try {
format.performExport(frame.getCurrentBasePanel().database(),
frame.getCurrentBasePanel().getLoadedDatabase().getMetaData(),
frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData(),
finFile.getPath(), frame
.getCurrentBasePanel().getEncoding(), finEntryIDs);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public void run() {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getLoadedDatabase().getMetaData()
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData()
.getFileDirectory(Globals.FILE_FIELD).toArray(new String[0]);
// Also store the database's file in a global variable:
Globals.prefs.databaseFile = frame.getCurrentBasePanel().getLoadedDatabase().getDatabaseFile();
Globals.prefs.databaseFile = frame.getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile();

File tmp = null;
try {
Expand All @@ -118,7 +118,7 @@ public void run() {
}

// Write to file:
format.performExport(database, panel.getLoadedDatabase().getMetaData(),
format.performExport(database, panel.getBibDatabaseContext().getMetaData(),
tmp.getPath(), panel.getEncoding(), entries);
// Read the file and put the contents on the clipboard:
StringBuilder sb = new StringBuilder();
Expand Down
Loading

0 comments on commit b51f3ad

Please sign in to comment.