Skip to content

Commit

Permalink
Merge pull request #2759 from JabRef/moreEditors
Browse files Browse the repository at this point in the history
More JavaFX editors
  • Loading branch information
tobiasdiez authored Apr 20, 2017
2 parents cbd6844 + 51ce1ec commit a9e2066
Show file tree
Hide file tree
Showing 34 changed files with 407 additions and 306 deletions.
9 changes: 0 additions & 9 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,18 +616,9 @@ 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 (fieldExtras.contains(FieldProperty.JOURNAL_NAME)) {
// Add controls for switching between abbreviated and full journal names.
// If this field also has a FieldContentSelector, we need to combine these.
return FieldExtraComponents.getJournalExtraComponent(frame, panel, editor, entry, contentSelectors,
storeFieldAction);
} else if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) {
return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors,
storeFieldAction);
} else if (fieldExtras.contains(FieldProperty.OWNER)) {
return FieldExtraComponents.getSetOwnerExtraComponent(editor, storeFieldAction);
} else if (fieldExtras.contains(FieldProperty.YES_NO)) {
return FieldExtraComponents.getYesNoExtraComponent(editor, this);
} else if (fieldExtras.contains(FieldProperty.MONTH)) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField
fieldEditor.setAutoCompleteListener(autoCompleteListener);
*/

FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.taskExecutor, new FXDialogService());
FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.taskExecutor, new FXDialogService(), Globals.journalAbbreviationLoader, Globals.prefs.getJournalAbbreviationPreferences(), Globals.prefs);
editors.put(fieldName, fieldEditor);
/*
// TODO: Reenable this
Expand All @@ -182,6 +182,18 @@ private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField
}
*/

/*
// TODO: Reenable content selector
if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(editor.getFieldName()).isEmpty()) {
FieldContentSelector ws = new FieldContentSelector(frame, panel, frame, editor, storeFieldAction, false,
", ");
contentSelectors.add(ws);
controls.add(ws, BorderLayout.NORTH);
}
//} else if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) {
//return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors, storeFieldAction);
*/

builder.append(new FieldNameLabel(fieldName));

JFXPanel swingPanel = new JFXPanel();
Expand Down
131 changes: 0 additions & 131 deletions src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 @@ -11,142 +8,31 @@
import java.util.Set;
import java.util.stream.Collectors;

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.gui.undo.UndoableFieldChange;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;
import org.jabref.model.entry.Month;
import org.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FieldExtraComponents {

private static final Log LOGGER = LogFactory.getLog(FieldExtraComponents.class);
private static final String ABBREVIATION_TOOLTIP_TEXT = "<HTML>"
+ Localization.lang("Switches between full and abbreviated journal name if the journal name is known.")
+ "<BR>" + Localization.lang("To set up, go to") + " <B>" + Localization.lang("Options") + " -> "
+ Localization.lang("Manage journal abbreviations") + "</B></HTML>";

private FieldExtraComponents() {
}

/**
* Add controls for switching between abbreviated and full journal names.
* If this field also has a FieldContentSelector, we need to combine these.
*
* @param panel
* @param editor
* @param entry
* @param storeFieldAction
* @return
*/
public static Optional<JComponent> getJournalExtraComponent(JabRefFrame frame, BasePanel panel, FieldEditor editor,
BibEntry entry, Set<FieldContentSelector> contentSelectors, StoreFieldAction storeFieldAction) {
JPanel controls = new JPanel();
controls.setLayout(new BorderLayout());
if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(editor.getFieldName()).isEmpty()) {
FieldContentSelector ws = new FieldContentSelector(frame, panel, frame, editor, storeFieldAction, false,
", ");
contentSelectors.add(ws);
controls.add(ws, BorderLayout.NORTH);
}


// Button to toggle abbreviated/full journal names
JButton button = new JButton(Localization.lang("Toggle abbreviation"));
button.setToolTipText(ABBREVIATION_TOOLTIP_TEXT);
button.addActionListener(actionEvent -> {
String text = editor.getText();
JournalAbbreviationRepository abbreviationRepository = Globals.journalAbbreviationLoader
.getRepository(Globals.prefs.getJournalAbbreviationPreferences());
if (abbreviationRepository.isKnownName(text)) {
String s = abbreviationRepository.getNextAbbreviation(text).orElse(text);

if (s != null) {
editor.setText(s);
storeFieldAction.actionPerformed(new ActionEvent(editor, 0, ""));
panel.getUndoManager().addEdit(new UndoableFieldChange(entry, editor.getFieldName(), text, s));
}
}
});

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

/**
* 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 Expand Up @@ -198,23 +84,6 @@ public static Optional<JComponent> getMonthExtraComponent(FieldEditor fieldEdito

}

/**
* Return a button which sets the owner if the field for fields with EXTRA_SET_OWNER
* @param fieldEditor
* @param storeFieldAction
* @return
*/
public static Optional<JComponent> getSetOwnerExtraComponent(FieldEditor fieldEditor,
StoreFieldAction storeFieldAction) {
JButton button = new JButton(Localization.lang("Auto"));
button.addActionListener(actionEvent -> {
fieldEditor.setText(Globals.prefs.get(JabRefPreferences.DEFAULT_OWNER));
storeFieldAction.actionPerformed(new ActionEvent(fieldEditor, 0, ""));
});
return Optional.of(button);

}

/**
* Return a button opening a content selector for fields where one exists
*
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import org.jabref.Globals;
import org.jabref.gui.DialogService;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.journals.JournalAbbreviationPreferences;
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;
import org.jabref.preferences.JabRefPreferences;


public class FieldEditors {

public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecutor, DialogService dialogService) {
public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecutor, DialogService dialogService, JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences, JabRefPreferences preferences) {
final Set<FieldProperty> fieldExtras = InternalBibtexFields.getFieldProperties(fieldName);

// TODO: Implement this
Expand All @@ -22,19 +24,15 @@ 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)) {
// Add controls for switching between abbreviated and full journal names.
// If this field also has a FieldContentSelector, we need to combine these.
//return FieldExtraComponents.getJournalExtraComponent(frame, panel, editor, entry, contentSelectors, storeFieldAction);
//} else if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) {
//return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors, storeFieldAction);
return new JournalEditor(fieldName, journalAbbreviationLoader, journalAbbreviationPreferences);
} else if (fieldExtras.contains(FieldProperty.DOI) ||
fieldExtras.contains(FieldProperty.EPRINT) ||
fieldExtras.contains(FieldProperty.ISBN)) {
return new IdentifierEditor(fieldName, taskExecutor, dialogService);
} else if (fieldExtras.contains(FieldProperty.OWNER)) {
//return FieldExtraComponents.getSetOwnerExtraComponent(editor, storeFieldAction);
return new OwnerEditor(fieldName, preferences);
} else if (fieldExtras.contains(FieldProperty.YES_NO)) {
//return FieldExtraComponents.getYesNoExtraComponent(editor, this);
} else if (fieldExtras.contains(FieldProperty.MONTH)) {
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
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/JournalEditor.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 onAction="#toggleAbbreviation"
styleClass="flatButton">
<graphic>
<MaterialDesignIconView glyphName="TEXT_SHADOW"/>
</graphic>
<tooltip>
<Tooltip text="%Switches between full and abbreviated journal name if the journal name is known."/>
</tooltip>
</Button>
</fx:root>
51 changes: 51 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.util.ControlHelper;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.journals.JournalAbbreviationPreferences;
import org.jabref.model.entry.BibEntry;


public class JournalEditor extends HBox implements FieldEditorFX {

private final String fieldName;
@FXML private JournalEditorViewModel viewModel;
@FXML private EditorTextArea textArea;
private Optional<BibEntry> entry;

public JournalEditor(String fieldName, JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences) {
this.fieldName = fieldName;
this.viewModel = new JournalEditorViewModel(journalAbbreviationLoader, journalAbbreviationPreferences);

ControlHelper.loadFXMLForControl(this);

viewModel.textProperty().bindBidirectional(textArea.textProperty());
}

public JournalEditorViewModel 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 toggleAbbreviation(ActionEvent event) {
viewModel.toggleAbbreviation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jabref.gui.fieldeditors;

import java.util.Optional;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.gui.AbstractViewModel;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.journals.JournalAbbreviationPreferences;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.strings.StringUtil;

public class JournalEditorViewModel extends AbstractViewModel {
private final JournalAbbreviationLoader journalAbbreviationLoader;
private final JournalAbbreviationPreferences journalAbbreviationPreferences;
private StringProperty text = new SimpleStringProperty();

public JournalEditorViewModel(JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences) {
this.journalAbbreviationLoader = journalAbbreviationLoader;
this.journalAbbreviationPreferences = journalAbbreviationPreferences;
}

public StringProperty textProperty() {
return text;
}

public void toggleAbbreviation() {
if (StringUtil.isBlank(text.get())) {
return;
}

JournalAbbreviationRepository abbreviationRepository = journalAbbreviationLoader.getRepository(journalAbbreviationPreferences);
if (abbreviationRepository.isKnownName(text.get())) {
Optional<String> nextAbbreviation = abbreviationRepository.getNextAbbreviation(text.get());

if (nextAbbreviation.isPresent()) {
text.set(nextAbbreviation.get());
// TODO: Add undo
//panel.getUndoManager().addEdit(new UndoableFieldChange(entry, editor.getFieldName(), text, nextAbbreviation));
}
}
}
}
Loading

0 comments on commit a9e2066

Please sign in to comment.