Skip to content

Commit

Permalink
Add DOI/URL field buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Apr 4, 2016
1 parent cc779b6 commit cdc5108
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ public enum BibtexSingleFieldProperties {
DATEPICKER,
JOURNAL_NAMES,
EXTERNAL,
DOI,
SET_OWNER,
MONTH,
FILE_EDITOR,
NUMERIC,
PERSON_NAMES;

public static final EnumSet<BibtexSingleFieldProperties> ALL_OPTS = EnumSet
.allOf(BibtexSingleFieldProperties.class);

public static final EnumSet<BibtexSingleFieldProperties> ALL_OPTS = EnumSet.allOf(BibtexSingleFieldProperties.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private InternalBibtexFields() {
add(dummy);

dummy = new BibtexSingleField("doi", true, BibtexSingleField.SMALL_W);
dummy.setExtras(EnumSet.of(BibtexSingleFieldProperties.EXTERNAL));
dummy.setExtras(EnumSet.of(BibtexSingleFieldProperties.DOI));
add(dummy);
add(new BibtexSingleField("eid", true, BibtexSingleField.SMALL_W));

Expand Down
37 changes: 5 additions & 32 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public class EntryEditor extends JPanel implements VetoableChangeListener, Entry


public EntryEditor(JabRefFrame frame, BasePanel panel, BibEntry entry) {

this.frame = frame;
this.panel = panel;
this.entry = entry;
Expand Down Expand Up @@ -463,7 +462,7 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
fieldExtras.contains(BibtexSingleFieldProperties.DATEPICKER));
} else if (fieldExtras.contains(BibtexSingleFieldProperties.EXTERNAL)) {
// Add external viewer listener for "pdf" and "url" fields.
return FieldExtraComponents.getExternalExtraComponent(editor, this);
return FieldExtraComponents.getExternalExtraComponent(panel, editor);
} else if (fieldExtras.contains(BibtexSingleFieldProperties.JOURNAL_NAMES)) {
// Add controls for switching between abbreviated and full journal names.
// If this field also has a FieldContentSelector, we need to combine these.
Expand All @@ -474,12 +473,14 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
getStoreFieldAction());
} else if (fieldExtras.contains(BibtexSingleFieldProperties.URL)) {
return FieldExtraComponents.getURLExtraComponent(editor, getStoreFieldAction());
} else if (fieldExtras.contains(BibtexSingleFieldProperties.DOI)) {
return FieldExtraComponents.getDoiExtraComponent(panel, editor);
} else if (fieldExtras.contains(BibtexSingleFieldProperties.SET_OWNER)) {
return FieldExtraComponents.getSetOwnerExtraComponent(editor, getStoreFieldAction());
} else if (fieldExtras.contains(BibtexSingleFieldProperties.YES_NO)) {
return FieldExtraComponents.getYesNoExtraComponent(editor, this);
} else if (fieldExtras.contains(BibtexSingleFieldProperties.MONTH)) {
return FieldExtraComponents.getMonthExtraComponent(editor, this, this.frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
return FieldExtraComponents.getMonthExtraComponent(editor, this, frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
}
return Optional.empty();
}
Expand All @@ -492,9 +493,7 @@ private void setupSourcePanel() {
source.setLineWrap(true);
source.setTabSize(GUIGlobals.INDENT);
source.addFocusListener(new FieldEditorFocusListener());
// Add the global focus listener, so a menu item can see if this field
// was focused when
// an action was called.
// Add the global focus listener, so a menu item can see if this field was focused when an action was called.
source.addFocusListener(Globals.focusListener);
source.setFont(new Font("Monospaced", Font.PLAIN, Globals.prefs.getInt(JabRefPreferences.FONT_SIZE)));
setupJTextComponent(source);
Expand All @@ -505,7 +504,6 @@ private void setupSourcePanel() {

srcPanel.setLayout(new BorderLayout());
srcPanel.add(scrollPane, BorderLayout.CENTER);

}

public void updateSource() {
Expand Down Expand Up @@ -839,13 +837,11 @@ private boolean storeSource() {
}

private void setField(String fieldName, String newFieldData) {

for (Object tab : tabs) {
if (tab instanceof EntryEditorTab) {
((EntryEditorTab) tab).updateField(fieldName, newFieldData);
}
}

}

/**
Expand Down Expand Up @@ -1378,29 +1374,6 @@ public void actionPerformed(ActionEvent e) {
}
}

class ExternalViewerListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2) {
TextArea tf = (TextArea) evt.getSource();

if (tf.getText().isEmpty()) {
return;
}

tf.selectAll();

String link = tf.getText(); // get selected ? String

try {
JabRefDesktop.openExternalViewer(panel.getBibDatabaseContext().getMetaData(), link, tf.getFieldName());
} catch (IOException ex) {
LOGGER.warn("Error opening file.", ex);
}
}
}
}

private void warnDuplicateBibtexkey() {
panel.output(Localization.lang("Duplicate BibTeX key") + ". "
+ Localization.lang("Grouping may not work for this entry."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.Set;

Expand All @@ -39,9 +40,13 @@
import net.sf.jabref.gui.FieldContentSelector;
import net.sf.jabref.gui.FileDialogs;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.actions.Actions;
import net.sf.jabref.gui.date.DatePickerButton;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.gui.desktop.os.NativeDesktop;
import net.sf.jabref.gui.entryeditor.EntryEditor.StoreFieldAction;
import net.sf.jabref.gui.fieldeditors.FieldEditor;
import net.sf.jabref.gui.mergeentries.MergeEntryDOIDialog;
import net.sf.jabref.gui.undo.UndoableFieldChange;
import net.sf.jabref.logic.journals.JournalAbbreviationRepository;
import net.sf.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -103,50 +108,52 @@ public static Optional<JComponent> getJournalExtraComponent(JabRefFrame frame, B
}

/**
* Return a "Browse" button for fields with EXTRA_BROWSE
* Set up a mouse listener for opening an external viewer for with with EXTRA_EXTERNAL
*
* @param frame
* @param fieldEditor
* @param entryEditor
* @param panel
* @return
*/
public static Optional<JComponent> getBrowseExtraComponent(JabRefFrame frame, FieldEditor fieldEditor,
EntryEditor entryEditor) {
JButton but = new JButton(Localization.lang("Browse"));
((JComponent) fieldEditor).addMouseListener(entryEditor.new ExternalViewerListener());

but.addActionListener(e -> {
String dir = fieldEditor.getText();

if (dir.isEmpty()) {
dir = Globals.prefs.get(fieldEditor.getFieldName() + Globals.FILETYPE_PREFS_EXT, "");
}

String chosenFile = FileDialogs.getNewFile(frame, new File(dir), '.' + fieldEditor.getFieldName(),
JFileChooser.OPEN_DIALOG, false);

if (chosenFile != null) {
File newFile = new File(chosenFile);
fieldEditor.setText(newFile.getPath());
Globals.prefs.put(fieldEditor.getFieldName() + Globals.FILETYPE_PREFS_EXT, newFile.getPath());
entryEditor.updateField(fieldEditor);
public static Optional<JComponent> getExternalExtraComponent(BasePanel panel, FieldEditor fieldEditor) {
JPanel controls = new JPanel();
controls.setLayout(new BorderLayout());
JButton button = new JButton(Localization.lang("Open"));
button.addActionListener(actionEvent -> {
try {
JabRefDesktop.openExternalViewer(panel.getBibDatabaseContext().getMetaData(), fieldEditor.getText(), fieldEditor.getFieldName());
} catch (IOException ex) {
panel.output(Localization.lang("Unable to open link."));
}
});

return Optional.of(but);
controls.add(button, BorderLayout.SOUTH);
return Optional.of(controls);
}

/**
* Set up a mouse listener for opening an external viewer for with with EXTRA_EXTERNAL
* Set up a mouse listener for opening an external viewer and fetching by DOI
*
* @param fieldEditor
* @param entryEditor
* @param panel
* @return
*/
public static Optional<JComponent> getExternalExtraComponent(FieldEditor fieldEditor, EntryEditor entryEditor) {
((JComponent) fieldEditor).addMouseListener(entryEditor.new ExternalViewerListener());
public static Optional<JComponent> getDoiExtraComponent(BasePanel panel, FieldEditor fieldEditor) {
JPanel controls = new JPanel();
controls.setLayout(new BorderLayout());
JButton button = new JButton(Localization.lang("Open"));
button.addActionListener(actionEvent -> {
try {
JabRefDesktop.openExternalViewer(panel.getBibDatabaseContext().getMetaData(), fieldEditor.getText(), fieldEditor.getFieldName());
} catch (IOException ex) {
panel.output(Localization.lang("Unable to open link."));
}
});
JButton fetchButton = new JButton(Localization.lang("Get BibTeX data from DOI"));
fetchButton.addActionListener(actionEvent -> new MergeEntryDOIDialog(panel));

return Optional.empty();
controls.add(button, BorderLayout.NORTH);
controls.add(fetchButton, BorderLayout.SOUTH);
return Optional.of(controls);
}

/**
Expand Down

0 comments on commit cdc5108

Please sign in to comment.