Skip to content

Commit

Permalink
Merge pull request #1056 from Siedlerchr/npeDBexport
Browse files Browse the repository at this point in the history
Fixes NPE in Export to External DB
  • Loading branch information
tobiasdiez committed Mar 28, 2016
2 parents ed989b0 + de698af commit f9f6cec
Showing 1 changed file with 79 additions and 68 deletions.
147 changes: 79 additions & 68 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
import java.util.*;

public class BasePanel extends JPanel implements ClipboardOwner, FileUpdateListener {
private static final Log LOGGER = LogFactory.getLog(BasePanel.class);

private static final Log LOGGER = LogFactory.getLog(BasePanel.class);

private final BibDatabase database;
private final BibDatabaseContext bibDatabaseContext;
Expand Down Expand Up @@ -210,8 +210,10 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Chars
}
}

filterSearchToggle = new StartStopListAction<>(searchFilterList, SearchMatcher.INSTANCE, EverythingMatcher.INSTANCE);
filterGroupToggle = new StartStopListAction<>(groupFilterList, GroupMatcher.INSTANCE, EverythingMatcher.INSTANCE);
filterSearchToggle = new StartStopListAction<>(searchFilterList, SearchMatcher.INSTANCE,
EverythingMatcher.INSTANCE);
filterGroupToggle = new StartStopListAction<>(groupFilterList, GroupMatcher.INSTANCE,
EverythingMatcher.INSTANCE);
}

// Returns a collection of AutoCompleters, which are populated from the current database
Expand Down Expand Up @@ -291,7 +293,8 @@ private void setupActions() {

actions.put(Actions.SAVE_SELECTED_AS, new SaveSelectedAction(SavePreferences.DatabaseSaveType.ALL));

actions.put(Actions.SAVE_SELECTED_AS_PLAIN, new SaveSelectedAction(SavePreferences.DatabaseSaveType.PLAIN_BIBTEX));
actions.put(Actions.SAVE_SELECTED_AS_PLAIN,
new SaveSelectedAction(SavePreferences.DatabaseSaveType.PLAIN_BIBTEX));

// The action for copying selected entries.
actions.put(Actions.COPY, (BaseAction) () -> {
Expand Down Expand Up @@ -409,9 +412,7 @@ private void setupActions() {
if ((bes != null) && (!bes.isEmpty())) {

NamedCompound ce = new NamedCompound(
(bes.size() > 1 ?
Localization.lang("paste entries") :
Localization.lang("paste entry")));
(bes.size() > 1 ? Localization.lang("paste entries") : Localization.lang("paste entry")));

// Store the first inserted bibtexentry.
// bes[0] does not work as bes[0] is first clonded,
Expand Down Expand Up @@ -498,6 +499,7 @@ private void setupActions() {
String errorMessage = "";
boolean connectedToDB;


// run first, in EDT:
@Override
public void init() {
Expand Down Expand Up @@ -541,7 +543,9 @@ public void run() {
frame.output(Localization.lang("Attempting SQL export..."));
final DBExporterAndImporterFactory factory = new DBExporterAndImporterFactory();
final DBExporter exporter = factory.getExporter(dbs.getServerType());
exporter.exportDatabaseToDBMS(database, bibDatabaseContext.getMetaData(), null, dbs, frame);
exporter.exportDatabaseToDBMS(database, bibDatabaseContext.getMetaData(),
getDatabase().getEntries(),
dbs, frame);
dbs.isConfigValid(true);
} catch (Exception ex) {
final String preamble = Localization
Expand All @@ -564,7 +568,8 @@ public void update() {
// if no error, report success
if (errorMessage.isEmpty()) {
if (connectedToDB) {
frame.output(Localization.lang("%0 export successful"));
final DBStrings dbs = bibDatabaseContext.getMetaData().getDBStrings();
frame.output(Localization.lang("%0 export successful", dbs.getServerType()));
}
} else { // show an error dialog if an error occurred
final String preamble = Localization
Expand Down Expand Up @@ -594,6 +599,7 @@ public void update() {
int numSelected;
boolean cancelled;


// Run first, in EDT:
@Override
public void init() {
Expand Down Expand Up @@ -630,8 +636,7 @@ public void run() {
Localization.lang("One or more keys will be overwritten. Continue?"),
Localization.lang("Disable this confirmation dialog"), false);
final int answer = JOptionPane.showConfirmDialog(frame, cbm,
Localization.lang("Overwrite keys"),
JOptionPane.YES_NO_OPTION);
Localization.lang("Overwrite keys"), JOptionPane.YES_NO_OPTION);
if (cbm.isSelected()) {
Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, false);
}
Expand All @@ -647,7 +652,6 @@ public void run() {
}
}


HashMap<BibEntry, Object> oldvals = new HashMap<>();
// Iterate again, removing already set keys. This is skipped if overwriting
// is disabled, since all entries with keys set will have been removed.
Expand Down Expand Up @@ -820,32 +824,32 @@ public void update() {

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

actions.put(Actions.OPEN_EXTERNAL_FILE, (BaseAction) () ->
JabRefExecutorService.INSTANCE.execute((Runnable) () -> {
final List<BibEntry> bes = mainTable.getSelectedEntries();
if (bes.size() != 1) {
output(Localization.lang("No entries or multiple entries selected."));
return;
}
actions.put(Actions.OPEN_EXTERNAL_FILE,
(BaseAction) () -> JabRefExecutorService.INSTANCE.execute((Runnable) () -> {
final List<BibEntry> bes = mainTable.getSelectedEntries();
if (bes.size() != 1) {
output(Localization.lang("No entries or multiple entries selected."));
return;
}

final BibEntry entry = bes.get(0);
if (!entry.hasField(Globals.FILE_FIELD)) {
// no bibtex field
new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen();
return;
}
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContent(entry.getField(Globals.FILE_FIELD));
if (tableModel.getRowCount() == 0) {
// content in bibtex field is not readable
new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen();
return;
}
FileListEntry flEntry = tableModel.getEntry(0);
ExternalFileMenuItem item = new ExternalFileMenuItem(frame(), entry, "", flEntry.link,
flEntry.type.get().getIcon(), bibDatabaseContext.getMetaData(), flEntry.type);
item.openLink();
}));
final BibEntry entry = bes.get(0);
if (!entry.hasField(Globals.FILE_FIELD)) {
// no bibtex field
new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen();
return;
}
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContent(entry.getField(Globals.FILE_FIELD));
if (tableModel.getRowCount() == 0) {
// content in bibtex field is not readable
new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen();
return;
}
FileListEntry flEntry = tableModel.getEntry(0);
ExternalFileMenuItem item = new ExternalFileMenuItem(frame(), entry, "", flEntry.link,
flEntry.type.get().getIcon(), bibDatabaseContext.getMetaData(), flEntry.type);
item.openLink();
}));

actions.put(Actions.OPEN_FOLDER, (BaseAction) () -> {
JabRefExecutorService.INSTANCE.execute(() -> {
Expand Down Expand Up @@ -964,17 +968,14 @@ public void update() {
actions.put(Relevance.getInstance().getValues().get(0).getActionName(),
new SpecialFieldAction(frame, Relevance.getInstance(),
Relevance.getInstance().getValues().get(0).getFieldValue().get(), true,
Localization.lang("Toggle relevance"),
Localization.lang("Toggled relevance for %0 entries")));
Localization.lang("Toggle relevance"), Localization.lang("Toggled relevance for %0 entries")));
actions.put(Quality.getInstance().getValues().get(0).getActionName(),
new SpecialFieldAction(frame, Quality.getInstance(),
Quality.getInstance().getValues().get(0).getFieldValue().get(), true,
Localization.lang("Toggle quality"),
Localization.lang("Toggled quality for %0 entries")));
Localization.lang("Toggle quality"), Localization.lang("Toggled quality for %0 entries")));
actions.put(Printed.getInstance().getValues().get(0).getActionName(), new SpecialFieldAction(frame,
Printed.getInstance(), Printed.getInstance().getValues().get(0).getFieldValue().get(), true,
Localization.lang("Toggle print status"),
Localization.lang("Toggled print status for %0 entries")));
Localization.lang("Toggle print status"), Localization.lang("Toggled print status for %0 entries")));

for (SpecialFieldValue prio : Priority.getInstance().getValues()) {
actions.put(prio.getActionName(), prio.getAction(this.frame));
Expand Down Expand Up @@ -1014,8 +1015,8 @@ public void update() {
actions.put(Actions.SWITCH_PREVIEW, (BaseAction) selectionListener::switchPreview);

actions.put(Actions.MANAGE_SELECTORS, (BaseAction) () -> {
ContentSelectorDialog2 csd = new ContentSelectorDialog2
(frame, frame, BasePanel.this, false, bibDatabaseContext.getMetaData(), null);
ContentSelectorDialog2 csd = new ContentSelectorDialog2(frame, frame, BasePanel.this, false,
bibDatabaseContext.getMetaData(), null);
csd.setLocationRelativeTo(frame);
csd.setVisible(true);
});
Expand Down Expand Up @@ -1086,14 +1087,13 @@ public void runCommand(final String _command) {
}
}

private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SavePreferences.DatabaseSaveType saveType)
throws SaveException {
private boolean saveDatabase(File file, boolean selectedOnly, Charset enc,
SavePreferences.DatabaseSaveType saveType) throws SaveException {
SaveSession session;
frame.block();
final String SAVE_DATABASE = Localization.lang("Save database");
try {
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs)
.withEncoding(enc)
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(enc)
.withSaveType(saveType);
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
if (selectedOnly) {
Expand All @@ -1105,7 +1105,8 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SaveP
registerUndoableChanges(session);

} catch (UnsupportedCharsetException ex2) {
JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + ' '
JOptionPane.showMessageDialog(frame,
Localization.lang("Could not save file.") + ' '
+ Localization.lang("Character encoding '%0' is not supported.", enc.displayName()),
SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
throw new SaveException("rt");
Expand Down Expand Up @@ -1143,8 +1144,7 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SaveP
String tryDiff = Localization.lang("Try different encoding");
int answer = JOptionPane.showOptionDialog(frame, builder.getPanel(), SAVE_DATABASE,
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null,
new String[] {Localization.lang("Save"), tryDiff,
Localization.lang("Cancel")}, tryDiff);
new String[] {Localization.lang("Save"), tryDiff, Localization.lang("Cancel")}, tryDiff);

if (answer == JOptionPane.NO_OPTION) {
// The user wants to use another encoding.
Expand Down Expand Up @@ -1247,6 +1247,7 @@ public SearchBar getSearchBar() {
return searchBar;
}


/**
* This listener is used to add a new entry to a group (or a set of groups) in case the Group View is selected and
* one or more groups are marked
Expand Down Expand Up @@ -1299,6 +1300,7 @@ public void databaseChanged(DatabaseChangeEvent e) {
}
}


/**
* This method is called from JabRefFrame when the user wants to create a new entry.
*
Expand Down Expand Up @@ -1374,8 +1376,9 @@ private void createMainTable() {

@Override
public void listChanged(ListEvent<BibEntry> listEvent) {
HighlightMatchingGroupPreferences highlightMatchingGroupPreferences = new HighlightMatchingGroupPreferences(Globals.prefs);
if(highlightMatchingGroupPreferences.isAny()) {
HighlightMatchingGroupPreferences highlightMatchingGroupPreferences = new HighlightMatchingGroupPreferences(
Globals.prefs);
if (highlightMatchingGroupPreferences.isAny()) {
getGroupSelector().showMatchingGroups(mainTable.getSelectedEntries(), false);
} else if (highlightMatchingGroupPreferences.isAll()) {
getGroupSelector().showMatchingGroups(mainTable.getSelectedEntries(), true);
Expand Down Expand Up @@ -1542,8 +1545,8 @@ protected boolean accept(Component c) {
AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(Globals.prefs);
// Set up AutoCompleters for this panel:
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_COMPLETE)) {
autoCompleters = new ContentAutoCompleters(getDatabase(), bibDatabaseContext.getMetaData(), autoCompletePreferences,
Globals.journalAbbreviationLoader);
autoCompleters = new ContentAutoCompleters(getDatabase(), bibDatabaseContext.getMetaData(),
autoCompletePreferences, Globals.journalAbbreviationLoader);
// ensure that the autocompleters are in sync with entries
this.getDatabase().addDatabaseChangeListener(new AutoCompletersUpdater());
} else {
Expand Down Expand Up @@ -1633,8 +1636,8 @@ public void showEntry(final BibEntry be) {
int divLoc = -1;
String visName = null;
if ((getShowing() != null) && isShowingEditor()) {
visName = ((EntryEditor) splitPane.getBottomComponent()).getVisiblePanelName();
}
visName = ((EntryEditor) splitPane.getBottomComponent()).getVisiblePanelName();
}
if (getShowing() != null) {
divLoc = splitPane.getDividerLocation();
}
Expand Down Expand Up @@ -1892,13 +1895,16 @@ public StartStopListAction<BibEntry> getFilterGroupToggle() {
return filterGroupToggle;
}


public static class StartStopListAction<E> {

private FilterList<E> list;
private final Matcher<E> active;
private final Matcher<E> inactive;

private boolean isActive;


private StartStopListAction(FilterList<E> list, Matcher<E> active, Matcher<E> inactive) {
this.list = list;
this.active = active;
Expand All @@ -1923,7 +1929,7 @@ public boolean isActive() {

public void updateFilterList(FilterList<E> filterList) {
list = filterList;
if(isActive) {
if (isActive) {
list.setMatcher(active);
} else {
list.setMatcher(inactive);
Expand Down Expand Up @@ -2190,6 +2196,7 @@ public void action() {
}
}


// Method pertaining to the ClipboardOwner interface.
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
Expand Down Expand Up @@ -2221,7 +2228,8 @@ public void fileUpdated() {
//LOGGER.debug("File '"+file.getPath()+"' has been modified.");
updatedExternally = true;

final ChangeScanner scanner = new ChangeScanner(frame, BasePanel.this, getBibDatabaseContext().getDatabaseFile());
final ChangeScanner scanner = new ChangeScanner(frame, BasePanel.this,
getBibDatabaseContext().getDatabaseFile());

// Test: running scan automatically in background
if ((getBibDatabaseContext().getDatabaseFile() != null)
Expand Down Expand Up @@ -2249,7 +2257,8 @@ public void run() {
sidePaneManager.hideComponent(FileUpdatePanel.NAME);
sidePaneManager.unregisterComponent(FileUpdatePanel.NAME);
}
FileUpdatePanel pan = new FileUpdatePanel(BasePanel.this, sidePaneManager, getBibDatabaseContext().getDatabaseFile(), scanner);
FileUpdatePanel pan = new FileUpdatePanel(BasePanel.this, sidePaneManager,
getBibDatabaseContext().getDatabaseFile(), scanner);
sidePaneManager.register(FileUpdatePanel.NAME, pan);
sidePaneManager.show(FileUpdatePanel.NAME);
//setUpdatedExternally(false);
Expand Down Expand Up @@ -2436,14 +2445,16 @@ public void setBackAndForwardEnabledState() {
}

private String formatOutputMessage(String start, int count) {
return String.format("%s %d %s.", start, count, (count > 1 ? Localization.lang("entries") :
Localization.lang("entry")));
return String.format("%s %d %s.", start, count,
(count > 1 ? Localization.lang("entries") : Localization.lang("entry")));
}


private class SaveSelectedAction implements BaseAction {

private final SavePreferences.DatabaseSaveType saveType;


public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) {
this.saveType = saveType;
}
Expand All @@ -2459,8 +2470,7 @@ public void action() throws SaveException {
if (!expFile.exists() || (JOptionPane.showConfirmDialog(frame,
Localization.lang("'%0' exists. Overwrite file?", expFile.getName()),
Localization.lang("Save database"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)) {
saveDatabase(expFile, true, Globals.prefs.getDefaultEncoding(),
saveType);
saveDatabase(expFile, true, Globals.prefs.getDefaultEncoding(), saveType);
frame.getFileHistory().newFile(expFile.getPath());
frame.output(Localization.lang("Saved selected to '%0'.", expFile.getPath()));
}
Expand Down Expand Up @@ -2494,7 +2504,8 @@ public Optional<String> searchAndOpen() {
final Collection<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final List<File> dirs = new ArrayList<>();
if (basePanel.getBibDatabaseContext().getMetaData().getFileDirectory(Globals.FILE_FIELD).size() > 0) {
final List<String> mdDirs = basePanel.getBibDatabaseContext().getMetaData().getFileDirectory(Globals.FILE_FIELD);
final List<String> mdDirs = basePanel.getBibDatabaseContext().getMetaData()
.getFileDirectory(Globals.FILE_FIELD);
for (final String mdDir : mdDirs) {
dirs.add(new File(mdDir));

Expand Down Expand Up @@ -2534,7 +2545,7 @@ public Optional<String> searchAndOpen() {
}
}

return Optional.empty();
return Optional.empty();
}
}
}
}

0 comments on commit f9f6cec

Please sign in to comment.