diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index c2877973859..3f1fb6c8e28 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -616,8 +616,6 @@ public Optional getExtra(final FieldEditor editor) { // double click AND datefield => insert the current date (today) return FieldExtraComponents.getDateTimeExtraComponent(editor, fieldExtras.contains(FieldProperty.DATE), fieldExtras.contains(FieldProperty.ISO_DATE)); - } else if (fieldExtras.contains(FieldProperty.EXTERNAL)) { - return FieldExtraComponents.getExternalExtraComponent(panel, editor); } else if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) { return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors, storeFieldAction); diff --git a/src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java b/src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java index 7f214d502ac..09c947cea9f 100644 --- a/src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java +++ b/src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java @@ -1,8 +1,6 @@ package org.jabref.gui.entryeditor; -import java.awt.BorderLayout; import java.awt.event.ActionEvent; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -14,14 +12,12 @@ import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; -import javax.swing.JPanel; import org.jabref.Globals; import org.jabref.gui.BasePanel; import org.jabref.gui.JabRefFrame; import org.jabref.gui.contentselector.FieldContentSelector; import org.jabref.gui.date.DatePickerButton; -import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.entryeditor.EntryEditor.StoreFieldAction; import org.jabref.gui.fieldeditors.FieldEditor; import org.jabref.logic.l10n.Localization; @@ -41,61 +37,6 @@ public class FieldExtraComponents { private FieldExtraComponents() { } - /** - * Set up a mouse listener for opening an external viewer for with with EXTRA_EXTERNAL - * - * @param fieldEditor - * @param panel - * @return - */ - public static Optional getExternalExtraComponent(BasePanel panel, FieldEditor fieldEditor) { - JPanel controls = new JPanel(); - controls.setLayout(new BorderLayout()); - JButton button = new JButton(Localization.lang("Open")); - button.setEnabled(false); - button.addActionListener(actionEvent -> { - try { - JabRefDesktop.openExternalViewer(panel.getBibDatabaseContext(), fieldEditor.getText(), fieldEditor.getFieldName()); - } catch (IOException ex) { - panel.output(Localization.lang("Unable to open link.")); - } - }); - - controls.add(button, BorderLayout.SOUTH); - - // enable/disable button - /* - JTextComponent url = (JTextComponent) fieldEditor; - - url.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void changedUpdate(DocumentEvent documentEvent) { - checkUrl(); - } - - @Override - public void insertUpdate(DocumentEvent documentEvent) { - checkUrl(); - } - - @Override - public void removeUpdate(DocumentEvent documentEvent) { - checkUrl(); - } - - private void checkUrl() { - if (URLUtil.isURL(url.getText())) { - button.setEnabled(true); - } else { - button.setEnabled(false); - } - } - }); - */ - - return Optional.of(controls); - } - /** * Return a dropdown list containing Yes and No for fields with EXTRA_YES_NO * diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index 35d5d55a1e9..80f00c34bd5 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -24,7 +24,7 @@ public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecu //return FieldExtraComponents.getDateTimeExtraComponent(editor, // fieldExtras.contains(FieldProperty.DATE), fieldExtras.contains(FieldProperty.ISO_DATE)); } else if (fieldExtras.contains(FieldProperty.EXTERNAL)) { - //return FieldExtraComponents.getExternalExtraComponent(panel, editor); + return new UrlEditor(fieldName, dialogService); } else if (fieldExtras.contains(FieldProperty.JOURNAL_NAME)) { return new JournalEditor(fieldName, journalAbbreviationLoader, journalAbbreviationPreferences); } else if (fieldExtras.contains(FieldProperty.DOI) || diff --git a/src/main/java/org/jabref/gui/fieldeditors/IdentifierEditor.fxml b/src/main/java/org/jabref/gui/fieldeditors/IdentifierEditor.fxml index f5f3c888828..6475d53ae00 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/IdentifierEditor.fxml +++ b/src/main/java/org/jabref/gui/fieldeditors/IdentifierEditor.fxml @@ -8,7 +8,7 @@ - + + diff --git a/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java new file mode 100644 index 00000000000..e8fe54855a6 --- /dev/null +++ b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java @@ -0,0 +1,50 @@ +package org.jabref.gui.fieldeditors; + +import java.util.Optional; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.Parent; +import javafx.scene.layout.HBox; + +import org.jabref.gui.DialogService; +import org.jabref.gui.util.ControlHelper; +import org.jabref.model.entry.BibEntry; + + +public class UrlEditor extends HBox implements FieldEditorFX { + + private final String fieldName; + @FXML private UrlEditorViewModel viewModel; + @FXML private EditorTextArea textArea; + private Optional entry; + + public UrlEditor(String fieldName, DialogService dialogService) { + this.fieldName = fieldName; + this.viewModel = new UrlEditorViewModel(dialogService); + + ControlHelper.loadFXMLForControl(this); + + viewModel.textProperty().bindBidirectional(textArea.textProperty()); + } + + public UrlEditorViewModel getViewModel() { + return viewModel; + } + + @Override + public void bindToEntry(BibEntry entry) { + this.entry = Optional.of(entry); + textArea.bindToEntry(fieldName, entry); + } + + @Override + public Parent getNode() { + return this; + } + + @FXML + private void openExternalLink(ActionEvent event) { + viewModel.openExternalLink(); + } +} diff --git a/src/main/java/org/jabref/gui/fieldeditors/UrlEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/UrlEditorViewModel.java new file mode 100644 index 00000000000..dfeb7cb6867 --- /dev/null +++ b/src/main/java/org/jabref/gui/fieldeditors/UrlEditorViewModel.java @@ -0,0 +1,55 @@ +package org.jabref.gui.fieldeditors; + +import java.io.IOException; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +import org.jabref.gui.AbstractViewModel; +import org.jabref.gui.DialogService; +import org.jabref.gui.desktop.JabRefDesktop; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.net.URLUtil; +import org.jabref.model.strings.StringUtil; + +import org.fxmisc.easybind.EasyBind; + +public class UrlEditorViewModel extends AbstractViewModel { + private StringProperty text = new SimpleStringProperty(""); + private DialogService dialogService; + private BooleanProperty validUrlIsNotPresent = new SimpleBooleanProperty(true); + + public UrlEditorViewModel(DialogService dialogService) { + this.dialogService = dialogService; + + validUrlIsNotPresent.bind( + EasyBind.map(text, input -> StringUtil.isBlank(input) || !URLUtil.isURL(input)) + ); + } + + public boolean isValidUrlIsNotPresent() { + return validUrlIsNotPresent.get(); + } + + public BooleanProperty validUrlIsNotPresentProperty() { + return validUrlIsNotPresent; + } + + public StringProperty textProperty() { + return text; + } + + public void openExternalLink() { + if (StringUtil.isBlank(text.get())) { + return; + } + + try { + JabRefDesktop.openBrowser(text.get()); + } catch (IOException ex) { + dialogService.notify(Localization.lang("Unable to open link.")); + } + } +}