Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bibentrysource contextmenu #5007

Merged
merged 17 commits into from
Jun 10, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
-fx-font-weight: normal;
}

.code-area .context-menu {
-fx-font-family: sans-serif;
}

.icon-button.narrow {
-fx-padding: 0.1em;
}
Expand Down
50 changes: 47 additions & 3 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,10 +68,37 @@ public class SourceTab extends EntryEditorTab {
private final FileUpdateMonitor fileMonitor;
private final DialogService dialogService;
private final StateManager stateManager;

private Optional<Pattern> 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()));
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 -> {
Expand All @@ -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" +
Expand Down Expand Up @@ -271,5 +316,4 @@ private void storeSource(BibEntry outOfFocusEntry, String text) {
LOGGER.debug("Incorrect source", ex);
}
}

}