Skip to content

Commit

Permalink
Add ID Fetcher in Entrytypedialog (JabRef#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
zesaro committed Oct 27, 2016
1 parent aa35019 commit 471cd39
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- [#1897](https://github.com/JabRef/jabref/issues/1897) Implemented integrity check for `year` field: Last four nonpunctuation characters should be numerals
- Address in MS-Office 2007 xml format is now imported as `location`
- [#1912](https://github.com/JabRef/jabref/issues/1912) Implemented integrity check for `edition` field: Should have the first letter capitalized (BibTeX), Should contain an integer or a literal (BibLaTeX)
- The dialog for choosing new entries additionally supports ID-based entry generation
- `number` field is now exported as `number` field in MS-Office 2007 xml format, if no `issue` field is present and the entry type is not `patent`
- `note` field is now exported as `comments` field in MS-Office 2007 xml format
- `comments` field in MS-Office 2007 xml format is now imported as `note` field
Expand Down
115 changes: 112 additions & 3 deletions src/main/java/net/sf/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,51 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
import net.sf.jabref.gui.importer.fetcher.EntryFetchers;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;
import net.sf.jabref.model.entry.EntryType;
import net.sf.jabref.model.entry.IEEETranEntryTypes;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.VerticalLayout;

/**
* Dialog that prompts the user to choose a type for an entry.
* Returns null if canceled.
*/
public class EntryTypeDialog extends JDialog implements ActionListener {

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

private EntryType type;
private SwingWorker<Optional<BibEntry>, Void> fetcherWorker;
private JabRefFrame frame;
private static final int COLUMN = 3;
private final boolean biblatexMode;

Expand All @@ -47,7 +65,7 @@ static class TypeButton extends JButton implements Comparable<TypeButton> {
private final EntryType type;


public TypeButton(String label, EntryType type) {
TypeButton(String label, EntryType type) {
super(label);
this.type = type;
}
Expand All @@ -66,6 +84,8 @@ public EntryTypeDialog(JabRefFrame frame) {
// modal dialog
super(frame, true);

this.frame = frame;

bibDatabaseContext = frame.getCurrentBasePanel().getBibDatabaseContext();
biblatexMode = bibDatabaseContext.isBiblatexMode();

Expand All @@ -91,12 +111,17 @@ private JPanel createEntryGroupsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new VerticalLayout());

if(biblatexMode) {
if (biblatexMode) {
panel.add(createEntryGroupPanel("BibLateX", EntryTypes.getAllValues(bibDatabaseContext.getMode())));
} else {
panel.add(createEntryGroupPanel("BibTeX", BibtexEntryTypes.ALL));
panel.add(createEntryGroupPanel("IEEETran", IEEETranEntryTypes.ALL));
panel.add(createEntryGroupPanel(Localization.lang("Custom"), CustomEntryTypesManager.ALL));

if (!CustomEntryTypesManager.ALL.isEmpty()) {
panel.add(createEntryGroupPanel(Localization.lang("Custom"), CustomEntryTypesManager.ALL));
}

panel.add(createIdFetcherPanel());
}

return panel;
Expand Down Expand Up @@ -148,6 +173,89 @@ private JPanel createEntryGroupPanel(String groupTitle, Collection<EntryType> en
return panel;
}

private JPanel createIdFetcherPanel() {
JButton generateButton = new JButton(Localization.lang("Generate"));
JTextField idTextField = new JTextField("");
JComboBox<String> comboBox = new JComboBox<>();
EntryFetchers.getIdFetchers().forEach(fetcher -> comboBox.addItem(fetcher.getName()));
JLabel fetcherLabel = new JLabel(Localization.lang("ID type"));
JLabel idLabel = new JLabel(Localization.lang("ID"));

fetcherWorker = new SwingWorker<Optional<BibEntry>, Void>() {
Optional<BibEntry> bibEntry = Optional.empty();
IdBasedFetcher fetcher = null;
String searchID = "";

@Override
protected Optional<BibEntry> doInBackground() throws Exception {
generateButton.setEnabled(false);
generateButton.setText(Localization.lang("Searching..."));
searchID = idTextField.getText().trim();
fetcher = EntryFetchers.getIdFetchers().get(comboBox.getSelectedIndex());
if (!searchID.isEmpty()) {
try {
bibEntry = fetcher.performSearchById(searchID);
} catch (FetcherException e) {
LOGGER.error("Error fetching from " + fetcher.getName(), e);
JOptionPane.showMessageDialog(null, Localization.lang("Error while fetching from %0", fetcher.getName()), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
}
}
dispose();
return bibEntry;
}

@Override
protected void done() {
try {
Optional<BibEntry> result = get();
if (result.isPresent()) {
frame.getCurrentBasePanel().insertEntry(result.get());
} else {
JOptionPane.showMessageDialog(null, Localization.lang("Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.", fetcher.getName(), searchID), Localization.lang("No files found."), JOptionPane.WARNING_MESSAGE);
}
} catch (ExecutionException | InterruptedException e) {
LOGGER.error(String.format("Exception during fetching when using fetcher '%s' with entry id '%s'.", searchID, fetcher.getName()), e);
}
}
};

generateButton.addActionListener(action -> fetcherWorker.execute());

JPanel jPanel = new JPanel();

GridBagConstraints constraints = new GridBagConstraints();

GridBagLayout layout = new GridBagLayout();
jPanel.setLayout(layout);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
jPanel.add(fetcherLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 2;
jPanel.add(comboBox, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 1;
jPanel.add(idLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.weightx = 2;
jPanel.add(idTextField, constraints);

constraints.gridy = 2;
JPanel buttons = new JPanel();
ButtonBarBuilder bb = new ButtonBarBuilder(buttons);
bb.addButton(generateButton);

jPanel.add(buttons, constraints);
jPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), Localization.lang("ID-based_entry_generator")));

return jPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof TypeButton) {
Expand All @@ -168,6 +276,7 @@ public CancelAction() {

@Override
public void actionPerformed(ActionEvent e) {
fetcherWorker.cancel(true);
dispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.sf.jabref.gui.importer.fetcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.importer.fetcher.ArXiv;
import net.sf.jabref.logic.importer.fetcher.DiVA;
import net.sf.jabref.logic.importer.fetcher.GvkFetcher;
Expand All @@ -30,13 +32,19 @@ public EntryFetchers(JournalAbbreviationLoader abbreviationLoader) {
entryFetchers.add(new DOAJFetcher());
entryFetchers.add(new SpringerFetcher());

entryFetchers.add(new IdBasedEntryFetcher(new DiVA(Globals.prefs.getImportFormatPreferences())));
entryFetchers.add(new IdBasedEntryFetcher(new IsbnFetcher(Globals.prefs.getImportFormatPreferences())));
entryFetchers.add(new SearchBasedEntryFetcher(new ArXiv()));
entryFetchers.add(new SearchBasedEntryFetcher(new GvkFetcher()));
}

public List<EntryFetcher> getEntryFetchers() {
return Collections.unmodifiableList(this.entryFetchers);
}

public static ArrayList<IdBasedFetcher> getIdFetchers() {
ArrayList<IdBasedFetcher> list = new ArrayList<>();
list.add(new IsbnFetcher(Globals.prefs.getImportFormatPreferences()));
list.add(new DiVA(Globals.prefs.getImportFormatPreferences()));
list.sort((fetcher1, fetcher2) -> fetcher1.getName().compareTo(fetcher2.getName()));
return list;
}
}
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=

BibTeX_key=
Message=

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
5 changes: 4 additions & 1 deletion src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=Version_in_die_Zwischenablage_kopiert

BibTeX_key=BibTeX-Key
Message=Nachricht

Decryption_not_supported.=Entschlüsselung_wird_nicht_unterstützt.

Cleared_'%0'_for_%1_entries='%0'_für_%1_Einträge_entfernt
Expand Down Expand Up @@ -2362,3 +2361,7 @@ Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=Nutz

You_can_up-_and_download_entries.=Du_kannst_Einträge_hoch-_und_runterladen.
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=Nach dem_einloggen_auf_bibsonomy.org_kannst_du_deine_Einträge_einsehen_und_editieren.
ID=ID
ID_type=ID_typ
ID-based_entry_generator=ID_basierter_Eintragsgenerator
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=Der_Fetcher_%0_hat_keinen_Eintrag_für_die_ID_%1_gefunden.
7 changes: 5 additions & 2 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=Copied_version_to_clipboard

BibTeX_key=BibTeX_key
Message=Message

Decryption_not_supported.=Decryption_not_supported.

Cleared_'%0'_for_%1_entries=Cleared_'%0'_for_%1_entries
Expand Down Expand Up @@ -2358,4 +2357,8 @@ PLEASE_NOTE\:_the_current_API_access_data_is_for_testing_purposes_only.=PLEASE_N
To_obtain_your_own_personal_API_key,_visit=To_obtain_your_own_personal_API_key,_visit
You_can_up-_and_download_entries.=You_can_up-_and_download_entries.
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.
ID=ID
ID_type=ID_type
ID-based_entry_generator=ID-based_entry_generator
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=

BibTeX_key=
Message=

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=Ajustes_de_'%0'_para_%1_entradas
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=

BibTeX_key=
Message=

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=Version_copiée_dans_le_presse-papiers

BibTeX_key=Clef_BibTeX
Message=Message

Decryption_not_supported.=Déchiffrement_non_supporté.

Cleared_'%0'_for_%1_entries=Réinitialisés_'%0'_pour_%1_entrées
Expand Down Expand Up @@ -2363,3 +2362,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=

BibTeX_key=
Message=

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=

BibTeX_key=
Message=

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=Reinizializzati_'%0'_per_%1_voce/i
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ Copied_version_to_clipboard=クリップボードにコピーされたバージ

BibTeX_key=BibTeX鍵
Message=メッセージ

Decryption_not_supported.=

Cleared_'%0'_for_%1_entries=
Expand Down Expand Up @@ -2367,3 +2366,8 @@ To_obtain_your_own_personal_API_key,_visit=
You_can_up-_and_download_entries.=
After_logging_in_you_can_see_and_edit_your_entries_on_www.bibsonomy.org.=
Do_not_use_this_account_for_personal_data,_as_it_is_accessible_by_everyone.=

ID=
ID_type=
ID-based_entry_generator=
Fetcher_'%0'_did_not_find_an_entry_for_id_'%1'.=
Loading

0 comments on commit 471cd39

Please sign in to comment.