Skip to content

Commit

Permalink
Reimplement url editior in JavaFX
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Apr 18, 2017
1 parent 9f04b08 commit 91d19f9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 65 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,6 @@ public Optional<JComponent> 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);
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<JComponent> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import org.jabref.gui.fieldeditors.EditorTextArea?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112">
<EditorTextArea fx:id="textArea" prefHeight="0.0" text="Halo" HBox.hgrow="ALWAYS" />
<EditorTextArea fx:id="textArea" prefHeight="0.0" HBox.hgrow="ALWAYS"/>
<Button disable="${controller.viewModel.validIdentifierIsNotPresent}" onAction="#openExternalLink"
styleClass="flatButton">
<graphic>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<?import org.jabref.gui.fieldeditors.EditorTextArea?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112">
<EditorTextArea fx:id="textArea" prefHeight="0.0" text="Halo" HBox.hgrow="ALWAYS"/>
<EditorTextArea fx:id="textArea" prefHeight="0.0" HBox.hgrow="ALWAYS"/>
<Button onAction="#toggleAbbreviation"
styleClass="flatButton">
<graphic>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<?import javafx.scene.layout.HBox?>
<?import org.jabref.gui.fieldeditors.EditorTextArea?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112">
<EditorTextArea fx:id="textArea" prefHeight="0.0" text="Halo" HBox.hgrow="ALWAYS"/>
<EditorTextArea fx:id="textArea" prefHeight="0.0" HBox.hgrow="ALWAYS"/>
</fx:root>
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/UrlEditor.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.HBox?>
<?import org.jabref.gui.fieldeditors.EditorTextArea?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112">
<EditorTextArea fx:id="textArea" prefHeight="0.0" HBox.hgrow="ALWAYS"/>
<Button disable="${controller.viewModel.validUrlIsNotPresent}" onAction="#openExternalLink"
styleClass="flatButton">
<graphic>
<MaterialDesignIconView glyphName="OPEN_IN_NEW"/>
</graphic>
<tooltip>
<Tooltip text="%Open"/>
</tooltip>
</Button>
</fx:root>
50 changes: 50 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java
Original file line number Diff line number Diff line change
@@ -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<BibEntry> 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();
}
}
55 changes: 55 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/UrlEditorViewModel.java
Original file line number Diff line number Diff line change
@@ -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."));
}
}
}

0 comments on commit 91d19f9

Please sign in to comment.