From eb42850f7853aff1f98459e533bc254217a2fa8d Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Mon, 10 Jun 2019 08:58:24 +0200 Subject: [PATCH] Bibentrysource contextmenu (#5007) * Added ContextMenu * Refactor deprecated Actions * changelog * whitespace * whitespaces the second * fix codeArea * Added styleclass * Forgot requestFocus * Whitespace and refactor * Changelog * Checkstyle * minor corrections * minor corrections * Refactor overlooked --- CHANGELOG.md | 1 + .../jabref/gui/entryeditor/EntryEditor.css | 4 ++ .../org/jabref/gui/entryeditor/SourceTab.java | 50 +++++++++++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52d2348fd79..bca01e2d50f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We added an option in the preference dialog box that allows user to enable helpful tooltips.[#3599](https://github.com/JabRef/jabref/issues/3599) - We moved the dropdown menu for selecting the push-application from the toolbar into the external application preferences. [#674](https://github.com/JabRef/jabref/issues/674) - We removed the alphabetical ordering of the custom tabs and updated the error message when trying to create a general field with a name containing an illegal character. [#5019](https://github.com/JabRef/jabref/issues/5019) +- We added a context menu to the bib(la)tex-source-editor to copy'n'paste. [#5007](https://github.com/JabRef/jabref/pull/5007) ### Fixed diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.css b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.css index d0a97309716..5d7c406cb2f 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.css +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.css @@ -41,6 +41,10 @@ -fx-font-weight: normal; } +.code-area .context-menu { + -fx-font-family: sans-serif; +} + .icon-button.narrow { -fx-padding: 0.1em; } diff --git a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java index c8fa3c87b37..1aaa7d2b5ef 100644 --- a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java @@ -15,11 +15,16 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.collections.ListChangeListener; import javafx.geometry.Point2D; +import javafx.scene.control.ContextMenu; import javafx.scene.control.Tooltip; import javafx.scene.input.InputMethodRequests; +import org.jabref.Globals; import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; +import org.jabref.gui.actions.ActionFactory; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.actions.StandardActions; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.undo.CountingUndoManager; import org.jabref.gui.undo.NamedCompound; @@ -63,10 +68,37 @@ public class SourceTab extends EntryEditorTab { private final FileUpdateMonitor fileMonitor; private final DialogService dialogService; private final StateManager stateManager; - private Optional searchHighlightPattern = Optional.empty(); private CodeArea codeArea; + private class EditAction extends SimpleCommand { + + private final StandardActions command; + + public EditAction(StandardActions command) { this.command = command; } + + @Override + public void execute() { + if (codeArea != null) { + switch (command) { + case COPY: + codeArea.copy(); + break; + case CUT: + codeArea.cut(); + break; + case PASTE: + codeArea.paste(); + break; + case SELECT_ALL: + codeArea.selectAll(); + break; + } + codeArea.requestFocus(); + } + } + } + public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService, StateManager stateManager) { this.mode = bibDatabaseContext.getMode(); this.setText(Localization.lang("%0 source", mode.getFormattedName())); @@ -143,6 +175,19 @@ private CodeArea createSourceEditor() { } }); codeArea.setId("bibtexSourceCodeArea"); + + ActionFactory factory = new ActionFactory(Globals.getKeyPrefs()); + ContextMenu contextMenu = new ContextMenu(); + contextMenu.getItems().addAll( + factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT)), + factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY)), + factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE)), + factory.createMenuItem(StandardActions.SELECT_ALL, new EditAction(StandardActions.SELECT_ALL)) + ); + + contextMenu.getStyleClass().add("context-menu"); + codeArea.setContextMenu(contextMenu); + return codeArea; } @@ -169,6 +214,7 @@ protected void bindToEntry(BibEntry entry) { }); this.setContent(codeArea); this.codeArea = codeArea; + // Store source for on focus out event in the source code (within its text area) // and update source code for every change of entry field values BindingsHelper.bindContentBidirectional(entry.getFieldsObservable(), codeArea.focusedProperty(), onFocus -> { @@ -181,7 +227,6 @@ protected void bindToEntry(BibEntry entry) { try { codeArea.appendText(getSourceString(entry, mode, fieldFormatterPreferences)); highlightSearchPattern(); - } catch (IOException ex) { codeArea.setEditable(false); codeArea.appendText(ex.getMessage() + "\n\n" + @@ -271,5 +316,4 @@ private void storeSource(BibEntry outOfFocusEntry, String text) { LOGGER.debug("Incorrect source", ex); } } - }