Skip to content

Commit

Permalink
Added eye candy in entry editor context menus (#7268)
Browse files Browse the repository at this point in the history
* Added eye candy in entry editor context menus

* Refactor ProtectedTermsMenu

* Rafactored small cleanups

* CHANGELOG.md and l10n
  • Loading branch information
calixtus authored Jan 4, 2021
1 parent 7d08679 commit 0f3b016
Show file tree
Hide file tree
Showing 25 changed files with 338 additions and 256 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Added

- We added the extension support and the external application support (For Texshow, Texmaker and LyX) to the flatpak [#7248](https://github.com/JabRef/jabref/pull/7248)
- We added some symbols and keybindings to the context menu in the entry editor. [#7268]((https://github.com/JabRef/jabref/pull/7268))

### Changed

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/jabref/gui/actions/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import org.jabref.gui.keyboard.KeyBinding;

public interface Action {
Optional<JabRefIcon> getIcon();
default Optional<JabRefIcon> getIcon() {
return Optional.empty();
}

Optional<KeyBinding> getKeyBinding();
default Optional<KeyBinding> getKeyBinding() {
return Optional.empty();
}

String getText();

String getDescription();
default String getDescription() {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.fieldeditors;

import java.util.Collections;

import javax.swing.undo.UndoManager;

import javafx.fxml.FXML;
Expand Down Expand Up @@ -50,6 +52,8 @@ public CitationKeyEditor(Field field,

textField.textProperty().bindBidirectional(viewModel.textProperty());

textField.initContextMenu(Collections::emptyList);

new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface ContextMenuAddable {
* to be instantiated at this point. They are populated when the user needs them which prevents many unnecessary
* allocations when the main table is just scrolled with the entry editor open.
*/
void addToContextMenu(final Supplier<List<MenuItem>> items);
void initContextMenu(final Supplier<List<MenuItem>> items);
}
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import javafx.scene.control.MenuItem;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.Globals;
import org.jabref.gui.fieldeditors.contextmenu.EditorContextAction;

public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable, ContextMenuAddable {

Expand All @@ -36,9 +38,9 @@ public EditorTextArea(final String text) {
}

@Override
public void addToContextMenu(final Supplier<List<MenuItem>> items) {
public void initContextMenu(final Supplier<List<MenuItem>> items) {
setOnContextMenuRequested(event -> {
contextMenu.getItems().setAll(TextInputControlBehavior.getDefaultContextMenuItems(this));
contextMenu.getItems().setAll(EditorContextAction.getDefaultContextMenuItems(this, Globals.getKeyPrefs()));
contextMenu.getItems().addAll(0, items.get());

TextInputControlBehavior.showContextMenu(this, contextMenu, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import javafx.scene.layout.Priority;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.Globals;
import org.jabref.gui.fieldeditors.contextmenu.EditorContextAction;

public class EditorTextField extends javafx.scene.control.TextField implements Initializable, ContextMenuAddable {

Expand All @@ -32,9 +34,9 @@ public EditorTextField(final String text) {
}

@Override
public void addToContextMenu(final Supplier<List<MenuItem>> items) {
public void initContextMenu(final Supplier<List<MenuItem>> items) {
setOnContextMenuRequested(event -> {
contextMenu.getItems().setAll(TextInputControlBehavior.getDefaultContextMenuItems(this));
contextMenu.getItems().setAll(EditorContextAction.getDefaultContextMenuItems(this, Globals.getKeyPrefs()));
contextMenu.getItems().addAll(0, items.get());

TextInputControlBehavior.showContextMenu(this, contextMenu, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class GenderEditorViewModel extends MapBasedEditorViewModel<String> {

private BiMap<String, String> itemMap = HashBiMap.create(7);
private final BiMap<String, String> itemMap = HashBiMap.create(7);

public GenderEditorViewModel(Field field, SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers) {
super(field, suggestionProvider, fieldCheckers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.jabref.gui.DialogService;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.DefaultMenu;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.integrity.FieldCheckers;
Expand Down Expand Up @@ -49,9 +50,9 @@ public IdentifierEditor(Field field,
new Tooltip(Localization.lang("Look up %0", field.getDisplayName())));

if (field.equals(StandardField.DOI)) {
textArea.addToContextMenu(EditorMenus.getDOIMenu(textArea));
textArea.initContextMenu(EditorMenus.getDOIMenu(textArea));
} else {
textArea.addToContextMenu(EditorMenus.getDefaultMenu(textArea));
textArea.initContextMenu(new DefaultMenu(textArea));
}

new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.gui.fieldeditors.contextmenu.DefaultMenu;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.entry.BibEntry;
Expand All @@ -32,7 +32,7 @@ public JournalEditor(Field field,
.load();

textField.textProperty().bindBidirectional(viewModel.textProperty());
textField.addToContextMenu(EditorMenus.getDefaultMenu(textField));
textField.initContextMenu(new DefaultMenu(textField));

AutoCompletionTextInputBinding.autoComplete(textField, viewModel::complete);

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/OptionEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.layout.HBox;

import org.jabref.gui.Globals;
import org.jabref.gui.fieldeditors.contextmenu.EditorContextAction;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.model.entry.BibEntry;

Expand All @@ -15,7 +18,7 @@
*/
public class OptionEditor<T> extends HBox implements FieldEditorFX {

@FXML private OptionEditorViewModel<T> viewModel;
@FXML private final OptionEditorViewModel<T> viewModel;
@FXML private ComboBox<T> comboBox;

public OptionEditor(OptionEditorViewModel<T> viewModel) {
Expand All @@ -29,6 +32,12 @@ public OptionEditor(OptionEditorViewModel<T> viewModel) {
comboBox.setCellFactory(new ViewModelListCellFactory<T>().withText(viewModel::convertToDisplayText));
comboBox.getItems().setAll(viewModel.getItems());
comboBox.getEditor().textProperty().bindBidirectional(viewModel.textProperty());

comboBox.getEditor().setOnContextMenuRequested(event -> {
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().setAll(EditorContextAction.getDefaultContextMenuItems(comboBox.getEditor(), Globals.getKeyPrefs()));
TextInputControlBehavior.showContextMenu(comboBox.getEditor(), contextMenu, event);
});
}

public OptionEditorViewModel<T> getViewModel() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/OwnerEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.scene.layout.HBox;

import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand All @@ -29,6 +30,8 @@ public OwnerEditor(Field field,

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

textArea.initContextMenu(EditorMenus.getNameMenu(textArea));

new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public PersonsEditor(final Field field,

decoratedStringProperty = new UiThreadStringProperty(viewModel.textProperty());
textInput.textProperty().bindBidirectional(decoratedStringProperty);
((ContextMenuAddable) textInput).addToContextMenu(EditorMenus.getNameMenu(textInput));
((ContextMenuAddable) textInput).initContextMenu(EditorMenus.getNameMenu(textInput));
this.getChildren().add(textInput);

AutoCompletionTextInputBinding.autoComplete(textInput, viewModel::complete, viewModel.getAutoCompletionConverter(), viewModel.getAutoCompletionStrategy());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding;
import org.jabref.gui.autocompleter.ContentSelectorSuggestionProvider;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.gui.fieldeditors.contextmenu.DefaultMenu;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand All @@ -30,7 +30,7 @@ public SimpleEditor(final Field field,
HBox.setHgrow(textInput, Priority.ALWAYS);

textInput.textProperty().bindBidirectional(viewModel.textProperty());
((ContextMenuAddable) textInput).addToContextMenu(EditorMenus.getDefaultMenu(textInput));
((ContextMenuAddable) textInput).initContextMenu(new DefaultMenu(textInput));
this.getChildren().add(textInput);

if (!isMultiLine) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
package org.jabref.gui.fieldeditors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.IndexRange;
import javafx.scene.control.MenuItem;
import javafx.scene.control.PasswordField;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.skin.TextAreaSkin;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ContextMenuEvent;
import javafx.stage.Screen;
import javafx.stage.Window;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;

import com.sun.javafx.scene.control.Properties;

/**
Expand All @@ -35,63 +22,6 @@
*/
public class TextInputControlBehavior {

private static final boolean SHOW_HANDLES = Properties.IS_TOUCH_SUPPORTED && !OS.OS_X;

/**
* Returns the default context menu items (except undo/redo)
*/
public static List<MenuItem> getDefaultContextMenuItems(TextInputControl textInputControl) {
boolean editable = textInputControl.isEditable();
boolean hasText = (textInputControl.getLength() > 0);
boolean hasSelection = (textInputControl.getSelection().getLength() > 0);
boolean allSelected = (textInputControl.getSelection().getLength() == textInputControl.getLength());
boolean maskText = (textInputControl instanceof PasswordField); // (maskText("A") != "A");
ArrayList<MenuItem> items = new ArrayList<>();

MenuItem cutMI = new MenuItem(Localization.lang("Cut"));
cutMI.setOnAction(e -> textInputControl.cut());
MenuItem copyMI = new MenuItem(Localization.lang("Copy"));
copyMI.setOnAction(e -> textInputControl.copy());
MenuItem pasteMI = new MenuItem(Localization.lang("Paste"));
pasteMI.setOnAction(e -> textInputControl.paste());
MenuItem deleteMI = new MenuItem(Localization.lang("Delete"));
deleteMI.setOnAction(e -> {
IndexRange selection = textInputControl.getSelection();
textInputControl.deleteText(selection);
});
MenuItem selectAllMI = new MenuItem(Localization.lang("Select all"));
selectAllMI.setOnAction(e -> textInputControl.selectAll());
MenuItem separatorMI = new SeparatorMenuItem();

if (SHOW_HANDLES) {
if (!maskText && hasSelection) {
if (editable) {
items.add(cutMI);
}
items.add(copyMI);
}
if (editable && Clipboard.getSystemClipboard().hasString()) {
items.add(pasteMI);
}
if (hasText && !allSelected) {
items.add(selectAllMI);
}
selectAllMI.getProperties().put("refreshMenu", Boolean.TRUE);
} else {
if (editable) {
items.addAll(Arrays.asList(cutMI, copyMI, pasteMI, deleteMI, separatorMI, selectAllMI));
} else {
items.addAll(Arrays.asList(copyMI, separatorMI, selectAllMI));
}
cutMI.setDisable(maskText || !hasSelection);
copyMI.setDisable(maskText || !hasSelection);
pasteMI.setDisable(!Clipboard.getSystemClipboard().hasString());
deleteMI.setDisable(!hasSelection);
}

return items;
}

/**
* @implNote taken from {@link com.sun.javafx.scene.control.behavior.TextFieldBehavior#contextMenuRequested(javafx.scene.input.ContextMenuEvent)}
*/
Expand Down Expand Up @@ -146,6 +76,8 @@ public static void showContextMenu(TextField textField, ContextMenu contextMenu,
textField.getProperties().put("CONTEXT_MENU_SCENE_X", 0);
contextMenu.show(textField, menuX, screenY);
}

e.consume();
}

/**
Expand Down Expand Up @@ -202,5 +134,7 @@ public static void showContextMenu(TextArea textArea, ContextMenu contextMenu, C
textArea.getProperties().put("CONTEXT_MENU_SCENE_X", 0);
contextMenu.show(textArea, menuX, screenY);
}

e.consume();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public UrlEditor(Field field,

textArea.textProperty().bindBidirectional(viewModel.textProperty());
Supplier<List<MenuItem>> contextMenuSupplier = EditorMenus.getCleanupUrlMenu(textArea);
textArea.addToContextMenu(contextMenuSupplier);
textArea.initContextMenu(contextMenuSupplier);

// init paste handler for UrlEditor to format pasted url link in textArea
textArea.setPasteActionHandler(() -> textArea.setText(new CleanupUrlFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText()))));
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0f3b016

Please sign in to comment.