Skip to content

Commit

Permalink
Merge pull request #983 from oscargus/fixfix
Browse files Browse the repository at this point in the history
Some random cleanups (and probably a bug-fix)
  • Loading branch information
simonharrer committed Mar 17, 2016
2 parents 5a64dd7 + 4fba694 commit 67ae095
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 159 deletions.
5 changes: 2 additions & 3 deletions src/main/java/net/sf/jabref/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.sf.jabref.model.database.BibDatabaseModeDetection;

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

Expand Down Expand Up @@ -46,9 +45,9 @@ public BibDatabaseContext(BibDatabase database, MetaData metaData, File file, De

public BibDatabaseMode getMode() {
List<String> data = metaData.getData(MetaData.DATABASE_TYPE);
if (data == null || data.isEmpty()) {
if ((data == null) || data.isEmpty()) {
BibDatabaseMode inferredMode = BibDatabaseModeDetection.inferMode(database);
if (defaults.mode == BibDatabaseMode.BIBLATEX || inferredMode == BibDatabaseMode.BIBLATEX) {
if ((defaults.mode == BibDatabaseMode.BIBLATEX) || (inferredMode == BibDatabaseMode.BIBLATEX)) {
return BibDatabaseMode.BIBLATEX;
} else {
return BibDatabaseMode.BIBTEX;
Expand Down
52 changes: 24 additions & 28 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,37 +395,33 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
boolean notSavedMsg = false;

// write an output, if something could be resolved
if (newBase != null) {
if (newBase.getEntryCount() > 0) {
String subName = StringUtil.getCorrectFileName(data[1], "bib");
if ((newBase != null) && !newBase.isEmpty()) {
String subName = StringUtil.getCorrectFileName(data[1], "bib");

try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
SaveSession session = databaseWriter.saveDatabase(
new BibDatabaseContext(newBase, defaults), prefs);


// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
System.err.println(Localization.lang("Warning") + ": "
+ Localization.lang(
"The chosen encoding '%0' could not encode the following characters:",
session.getEncoding().displayName())
+ " "
+ session.getWriter().getProblemCharacters());
}
session.commit(new File(subName));
} catch (SaveException ex) {
System.err.println(Localization.lang("Could not save file.") + "\n"
+ ex.getLocalizedMessage());
try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)));
SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults),
prefs);

// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
System.err.println(Localization.lang("Warning") + ": "
+ Localization.lang(
"The chosen encoding '%0' could not encode the following characters:",
session.getEncoding().displayName())
+ " " + session.getWriter().getProblemCharacters());
}

notSavedMsg = true;
session.commit(new File(subName));
} catch (SaveException ex) {
System.err.println(
Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage());
}

notSavedMsg = true;
}

if (!notSavedMsg) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public MetaData(Map<String, String> inData, BibDatabase db) {
* The MetaData object can be constructed with no data in it.
*/
public MetaData() {
// No data
}

public Optional<SaveOrderConfig> getSaveOrderConfig() {
Expand Down Expand Up @@ -399,15 +400,15 @@ public FieldFormatterCleanups getSaveActions() {

public Optional<BibDatabaseMode> getMode() {
List<String> data = getData(MetaData.DATABASE_TYPE);
if (data == null || data.isEmpty()) {
if ((data == null) || data.isEmpty()) {
return Optional.empty();
}
return Optional.of(BibDatabaseMode.valueOf(data.get(0).toUpperCase()));
}

public boolean isProtected() {
List<String> data = getData(Globals.PROTECTED_FLAG_META);
if (data == null || data.isEmpty()) {
if ((data == null) || data.isEmpty()) {
return false;
} else {
return Boolean.parseBoolean(data.get(0));
Expand All @@ -420,7 +421,7 @@ public List<String> getContentSelectors(String fieldName) {

public Optional<String> getDefaultFileDirectory() {
List<String> fileDirectory = getData(FILE_DIRECTORY);
if (fileDirectory == null || fileDirectory.isEmpty()) {
if ((fileDirectory == null) || fileDirectory.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(fileDirectory.get(0).trim());
Expand All @@ -429,7 +430,7 @@ public Optional<String> getDefaultFileDirectory() {

public Optional<String> getUserFileDirectory(String user) {
List<String> fileDirectory = getData(FILE_DIRECTORY + '-' + user);
if (fileDirectory == null || fileDirectory.isEmpty()) {
if ((fileDirectory == null) || fileDirectory.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(fileDirectory.get(0).trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public int compare(BibEntry e1, BibEntry e2) {
if ((f1 != null) && (f2 == null)) {
return -1;
}
if ((f1 == null) && (f2 != null)) {
if (f1 == null) { // f2 != null here automatically
return 1;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@
import net.sf.jabref.bibtex.comparator.CrossRefEntryComparator;
import net.sf.jabref.bibtex.comparator.FieldComparator;
import net.sf.jabref.bibtex.comparator.FieldComparatorStack;
import net.sf.jabref.groups.GroupTreeNode;
import net.sf.jabref.logic.config.SaveOrderConfig;
import net.sf.jabref.logic.id.IdComparator;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.migrations.VersionHandling;
import net.sf.jabref.model.database.BibDatabase;

public class BibDatabaseWriter {
Expand Down Expand Up @@ -247,8 +244,9 @@ private static List<FieldChange> applySaveActions(List<BibEntry> toChange, MetaD
* @param encoding String the name of the encoding, which is part of the file header.
*/
private void writeBibFileHeader(Writer out, Charset encoding) throws IOException {
if(encoding == null)
if(encoding == null) {
return;
}

out.write("% ");
out.write(Globals.encPrefix + encoding);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void saveAs() throws Throwable {
}
}

if (chosenFile != null) {
if (f != null) {
File oldFile = panel.getBibDatabaseContext().getDatabaseFile();
panel.getBibDatabaseContext().getMetaData().setFile(f);
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, f.getParent());
Expand All @@ -375,7 +375,8 @@ public void saveAs() throws Throwable {
}
// Register so we get notifications about outside changes to the file.
try {
panel.setFileMonitorHandle(Globals.fileUpdateMonitor.addUpdateListener(panel, panel.getBibDatabaseContext().getDatabaseFile()));
panel.setFileMonitorHandle(Globals.fileUpdateMonitor.addUpdateListener(panel,
panel.getBibDatabaseContext().getDatabaseFile()));
} catch (IOException ex) {
LOGGER.error("Problem registering file change notifications", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;

public class PushToApplications {
public static final List<PushToApplication> APPLICATIONS;
private static final List<PushToApplication> APPLICATIONS;

/**
* Set up the current available choices:
Expand All @@ -36,4 +36,8 @@ public class PushToApplications {
PushToApplications.APPLICATIONS.add(new PushToVim());
PushToApplications.APPLICATIONS.add(new PushToWinEdt());
}

public static List<PushToApplication> getApplications() {
return APPLICATIONS;
}
}
25 changes: 11 additions & 14 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,6 @@ public class BasePanel extends JPanel implements ClipboardOwner, FileUpdateListe
private ContentAutoCompleters autoCompleters;


// Returns a collection of AutoCompleters, which are populated from the current database
public ContentAutoCompleters getAutoCompleters() {
return autoCompleters;
}


public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Charset encoding) {
Objects.requireNonNull(frame);
Objects.requireNonNull(encoding);
Expand Down Expand Up @@ -225,6 +219,11 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Chars
filterGroupToggle = new StartStopListAction<>(groupFilterList, GroupMatcher.INSTANCE, EverythingMatcher.INSTANCE);
}

// Returns a collection of AutoCompleters, which are populated from the current database
public ContentAutoCompleters getAutoCompleters() {
return autoCompleters;
}

public String getTabTitle() {
StringBuilder title = new StringBuilder();

Expand Down Expand Up @@ -837,7 +836,7 @@ public void update() {

actions.put(Actions.ADD_FILE_LINK, new AttachFileAction(this));

actions.put(Actions.OPEN_EXTERNAL_FILE, (BaseAction) () -> {
actions.put(Actions.OPEN_EXTERNAL_FILE, (BaseAction) () ->
JabRefExecutorService.INSTANCE.execute((Runnable) () -> {
final List<BibEntry> bes = mainTable.getSelectedEntries();
if (bes.size() != 1) {
Expand All @@ -862,8 +861,7 @@ public void update() {
ExternalFileMenuItem item = new ExternalFileMenuItem(frame(), entry, "", flEntry.link,
flEntry.type.getIcon(), bibDatabaseContext.getMetaData(), flEntry.type);
item.openLink();
});
});
}));

actions.put(Actions.OPEN_FOLDER, (BaseAction) () -> {
JabRefExecutorService.INSTANCE.execute(() -> {
Expand All @@ -880,9 +878,8 @@ public void update() {
});
});

actions.put(Actions.OPEN_CONSOLE, (BaseAction) () -> {
JabRefDesktop.openConsole(frame.getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile());
});
actions.put(Actions.OPEN_CONSOLE, (BaseAction) () -> JabRefDesktop
.openConsole(frame.getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile()));

actions.put(Actions.OPEN_URL, (BaseAction) () -> {
String URL_FIELD = "url";
Expand All @@ -904,8 +901,8 @@ public void update() {
tm.setContent(bes.get(0).getField(Globals.FILE_FIELD));
for (int i = 0; i < tm.getRowCount(); i++) {
FileListEntry flEntry = tm.getEntry(i);
if (URL_FIELD.equals(flEntry.type.getName().toLowerCase())
|| PS_FIELD.equals(flEntry.type.getName().toLowerCase())) {
if (URL_FIELD.equalsIgnoreCase(flEntry.type.getName())
|| PS_FIELD.equalsIgnoreCase(flEntry.type.getName())) {
entry = flEntry;
break;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/sf/jabref/gui/CleanupPresetPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.EnumSet;
import java.util.Objects;
import java.util.prefs.Preferences;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
Expand Down
73 changes: 38 additions & 35 deletions src/main/java/net/sf/jabref/gui/ImportInspectionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,25 +401,6 @@ private Optional<BibEntry> internalDuplicate(Collection<BibEntry> entriesDupe, B
return Optional.empty();
}

/**
* Removes all selected entries from the table. Synchronizes on this.entries
* to prevent conflict with addition of new entries.
*/
private void removeSelectedEntries() {
int row = glTable.getSelectedRow();
List<Object> toRemove = new ArrayList<>();
toRemove.addAll(selectionModel.getSelected());
entries.getReadWriteLock().writeLock().lock();
for (Object o : toRemove) {
entries.remove(o);
}
entries.getReadWriteLock().writeLock().unlock();
glTable.clearSelection();
if ((row >= 0) && (!entries.isEmpty())) {
row = Math.min(entries.size() - 1, row);
glTable.addRowSelectionInterval(row, row);
}
}

/* (non-Javadoc)
* @see net.sf.jabref.gui.ImportInspection#entryListComplete()
Expand All @@ -440,22 +421,6 @@ public void entryListComplete() {
}
}

/**
* This method returns a List containing all entries that are selected
* (checkbox checked).
*
* @return a List containing the selected entries.
*/
private List<BibEntry> getSelectedEntries() {
List<BibEntry> selected = new ArrayList<>();
for (BibEntry entry : entries) {
if (entry.isSearchHit()) {
selected.add(entry);
}
}

return selected;
}

/**
* Generate key for the selected entry only.
Expand Down Expand Up @@ -767,6 +732,24 @@ public void actionPerformed(ActionEvent event) {
}
});
}

/**
* This method returns a List containing all entries that are selected
* (checkbox checked).
*
* @return a List containing the selected entries.
*/
private List<BibEntry> getSelectedEntries() {
List<BibEntry> selected = new ArrayList<>();
for (BibEntry entry : entries) {
if (entry.isSearchHit()) {
selected.add(entry);
}
}

return selected;
}

}

private void signalStopFetching() {
Expand Down Expand Up @@ -801,6 +784,26 @@ public DeleteListener() {
public void actionPerformed(ActionEvent event) {
removeSelectedEntries();
}

/**
* Removes all selected entries from the table. Synchronizes on this.entries
* to prevent conflict with addition of new entries.
*/
private void removeSelectedEntries() {
int row = glTable.getSelectedRow();
List<Object> toRemove = new ArrayList<>();
toRemove.addAll(selectionModel.getSelected());
entries.getReadWriteLock().writeLock().lock();
for (Object o : toRemove) {
entries.remove(o);
}
entries.getReadWriteLock().writeLock().unlock();
glTable.clearSelection();
if ((row >= 0) && (!entries.isEmpty())) {
row = Math.min(entries.size() - 1, row);
glTable.addRowSelectionInterval(row, row);
}
}
}

private class SelectionButton implements ActionListener {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import net.sf.jabref.gui.preftabs.PreferencesDialog;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.gui.util.PositionWindow;
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.gui.worker.MarkEntriesAction;
import net.sf.jabref.importer.*;
import net.sf.jabref.importer.fetcher.GeneralFetcher;
Expand Down Expand Up @@ -885,7 +884,7 @@ private void initLayout() {

setProgressBarVisible(false);

pushExternalButton = new PushToApplicationButton(this, PushToApplications.APPLICATIONS);
pushExternalButton = new PushToApplicationButton(this, PushToApplications.getApplications());
fillMenu();
createToolBar();
getContentPane().setLayout(gbl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.TreeMap;

import javax.swing.Action;
import javax.swing.JTabbedPane;

import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.JabRefFrame;
Expand Down
Loading

0 comments on commit 67ae095

Please sign in to comment.