Skip to content

Commit

Permalink
Extracted BaseAction out of EditAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 23, 2020
1 parent a800bf4 commit dcdef68
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 148 deletions.
15 changes: 0 additions & 15 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,6 @@ private void setupActions() {

actions.put(Actions.SAVE_SELECTED_AS_PLAIN, saveAction::saveSelectedAsPlain);

// The action for copying selected entries.
actions.put(Actions.COPY, this::copy);

actions.put(Actions.CUT, this::cut);

actions.put(Actions.DELETE, () -> delete(false));

// The action for pasting entries or cell contents.
// - more robust detection of available content flavors (doesn't only look at first one offered)
// - support for parsing string-flavor clipboard contents which are bibtex entries.
// This allows you to (a) paste entire bibtex entries from a text editor, web browser, etc
// (b) copy and paste entries between multiple instances of JabRef (since
// only the text representation seems to get as far as the X clipboard, at least on my system)
actions.put(Actions.PASTE, this::paste);

actions.put(Actions.SELECT_ALL, mainTable.getSelectionModel()::selectAll);

// The action for auto-generating keys.
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/jabref/gui/EditAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.jabref.gui;

import javafx.scene.control.TextInputControl;

import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;

/**
* Class for handling general actions; cut, copy and paste. The focused component is kept track of by
* Globals.focusListener, and we call the action stored under the relevant name in its action map.
*/
public class EditAction extends SimpleCommand {

private final JabRefFrame frame;
private final StandardActions action;
private final StateManager stateManager;

public EditAction(StandardActions action, JabRefFrame frame, StateManager stateManager) {
this.action = action;
this.frame = frame;
this.stateManager = stateManager;
}

@Override
public String toString() {
return this.action.toString();
}

@Override
public void execute() {
stateManager.getFocusOwner().ifPresent(focusOwner -> {
if (focusOwner instanceof TextInputControl) {
// Focus is on text field -> copy/paste/cut selected text
TextInputControl textInput = (TextInputControl) focusOwner;
switch (action) {
case COPY:
textInput.copy();
break;
case CUT:
textInput.cut();
break;
case PASTE:
// handled by FX in TextInputControl#paste
break;
default:
throw new IllegalStateException("Only cut/copy/paste supported in TextInputControl but got " + action);
}
} else {
// Not sure what is selected -> copy/paste/cut selected entries

// ToDo: Should be handled by BibDatabaseContext instead of BasePanel
switch (action) {
case COPY:
frame.getCurrentBasePanel().copy();
break;
case CUT:
frame.getCurrentBasePanel().cut();
break;
case PASTE:
// handled by FX in TextInputControl#paste
break;
case DELETE_ENTRY:
frame.getCurrentBasePanel().delete(false);
break;
default:
throw new IllegalStateException("Only cut/copy/paste supported but got " + action);
}
}
});
}
}
77 changes: 11 additions & 66 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.ToolBar;
import javafx.scene.control.Tooltip;
import javafx.scene.control.skin.TabPaneSkin;
Expand Down Expand Up @@ -487,13 +486,13 @@ private Node createToolbar() {
factory.createIconButton(StandardActions.NEW_ARTICLE, new NewEntryAction(this, StandardEntryType.Article, dialogService, Globals.prefs, stateManager)),
factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, Globals.prefs, stateManager)),
factory.createIconButton(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new ExtractBibtexAction(stateManager)),
factory.createIconButton(StandardActions.DELETE_ENTRY, new OldDatabaseCommandWrapper(Actions.DELETE, this, stateManager)),
factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, this, stateManager)),
new Separator(Orientation.VERTICAL),
factory.createIconButton(StandardActions.UNDO, new OldDatabaseCommandWrapper(Actions.UNDO, this, stateManager)),
factory.createIconButton(StandardActions.REDO, new OldDatabaseCommandWrapper(Actions.REDO, this, stateManager)),
factory.createIconButton(StandardActions.CUT, new OldDatabaseCommandWrapper(Actions.CUT, this, stateManager)),
factory.createIconButton(StandardActions.COPY, new OldDatabaseCommandWrapper(Actions.COPY, this, stateManager)),
factory.createIconButton(StandardActions.PASTE, new OldDatabaseCommandWrapper(Actions.PASTE, this, stateManager)),
factory.createIconButton(StandardActions.CUT, new EditAction(StandardActions.CUT,this, stateManager)),
factory.createIconButton(StandardActions.COPY, new EditAction(StandardActions.COPY,this, stateManager)),
factory.createIconButton(StandardActions.PASTE, new EditAction(StandardActions.PASTE,this, stateManager)),
new Separator(Orientation.VERTICAL),
pushToApplicationButton,
factory.createIconButton(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, stateManager)),
Expand Down Expand Up @@ -577,6 +576,9 @@ public void init() {
}
});

// Wait for the scene to be created, otherwise focusOwnerProperty is not provided
Platform.runLater(() -> stateManager.focusOwnerProperty().bind(
EasyBind.map(mainStage.getScene().focusOwnerProperty(), Optional::ofNullable)));
/*
* The following state listener makes sure focus is registered with the
* correct database when the user switches tabs. Without this,
Expand Down Expand Up @@ -701,9 +703,9 @@ private MenuBar createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.CUT, new EditAction(Actions.CUT)),
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, this, stateManager)),

factory.createMenuItem(StandardActions.COPY, new EditAction(Actions.COPY)),
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, this, stateManager)),
factory.createSubMenu(StandardActions.COPY_MORE,
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, Globals.clipboardManager, prefs)),
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, Globals.clipboardManager, prefs)),
Expand All @@ -713,7 +715,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, prefs.getPreviewPreferences())),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService))),

factory.createMenuItem(StandardActions.PASTE, new EditAction(Actions.PASTE)),
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, this, stateManager)),

new SeparatorMenuItem(),

Expand Down Expand Up @@ -743,7 +745,7 @@ private MenuBar createMenu() {
library.getItems().addAll(
factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, Globals.prefs, stateManager)),
factory.createMenuItem(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new ExtractBibtexAction(stateManager)),
factory.createMenuItem(StandardActions.DELETE_ENTRY, new OldDatabaseCommandWrapper(Actions.DELETE, this, stateManager)),
factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, this, stateManager)),

new SeparatorMenuItem(),

Expand Down Expand Up @@ -1244,63 +1246,6 @@ public void execute() {
}
}

/**
* Class for handling general actions; cut, copy and paste. The focused component is kept track of by
* Globals.focusListener, and we call the action stored under the relevant name in its action map.
*/
private class EditAction extends SimpleCommand {

private final Actions command;

public EditAction(Actions command) {
this.command = command;
}

@Override
public String toString() {
return this.command.toString();
}

@Override
public void execute() {
Node focusOwner = mainStage.getScene().getFocusOwner();
if (focusOwner != null) {
if (focusOwner instanceof TextInputControl) {
// Focus is on text field -> copy/paste/cut selected text
TextInputControl textInput = (TextInputControl) focusOwner;
switch (command) {
case COPY:
textInput.copy();
break;
case CUT:
textInput.cut();
break;
case PASTE:
// handled by FX in TextInputControl#paste
break;
default:
throw new IllegalStateException("Only cut/copy/paste supported but got " + command);
}
} else {
// Not sure what is selected -> copy/paste/cut selected entries
switch (command) {
case COPY:
getCurrentBasePanel().copy();
break;
case CUT:
getCurrentBasePanel().cut();
break;
case PASTE:
// handled by FX in TextInputControl#paste
break;
default:
throw new IllegalStateException("Only cut/copy/paste supported but got " + command);
}
}
}
}
}

private class CloseDatabaseAction extends SimpleCommand {

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.Node;

import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.logic.search.SearchQuery;
Expand All @@ -27,6 +28,7 @@
* - currently selected group
* - active search
* - active number of search results
* - focus owner
*/
public class StateManager {

Expand All @@ -36,6 +38,7 @@ public class StateManager {
private final ObservableMap<BibDatabaseContext, ObservableList<GroupTreeNode>> selectedGroups = FXCollections.observableHashMap();
private final OptionalObjectProperty<SearchQuery> activeSearchQuery = OptionalObjectProperty.empty();
private final ObservableMap<BibDatabaseContext, IntegerProperty> searchResultMap = FXCollections.observableHashMap();
private final OptionalObjectProperty<Node> focusOwner = OptionalObjectProperty.empty();

public StateManager() {
activeGroups.bind(Bindings.valueAt(selectedGroups, activeDatabase.orElse(null)));
Expand Down Expand Up @@ -99,4 +102,8 @@ public void clearSearchQuery() {
public void setSearchQuery(SearchQuery searchQuery) {
activeSearchQuery.setValue(Optional.of(searchQuery));
}

public OptionalObjectProperty<Node> focusOwnerProperty() { return focusOwner; }

public Optional<Node> getFocusOwner() { return focusOwner.get(); }
}
4 changes: 0 additions & 4 deletions src/main/java/org/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ public enum Actions {
ABBREVIATE_SHORTEST_UNIQUE,
ADD_FILE_LINK,
CLEANUP,
COPY,
CUT,
DELETE,
DOWNLOAD_FULL_TEXT,
EDIT,
EDIT_PREAMBLE,
Expand All @@ -21,7 +18,6 @@ public enum Actions {
MAKE_KEY,
MANAGE_SELECTORS,
MERGE_DATABASE,
PASTE,
PULL_CHANGES_FROM_SHARED_DATABASE,
REDO,
REPLACE_ALL,
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/gui/actions/JabRefAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ private String getActionName(Action action, Command command) {
return action.getText();
} else {
String commandName = command.getClass().getSimpleName();
if ((command instanceof OldDatabaseCommandWrapper) || (command instanceof OldCommandWrapper) || commandName.contains("EditAction")) {
if ((command instanceof OldDatabaseCommandWrapper)
|| commandName.contains("EditAction")
|| commandName.contains("CopyMoreAction")
|| commandName.contains("CopyCitationAction")) {
return command.toString();
} else {
return commandName;
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/org/jabref/gui/actions/OldCommandWrapper.java

This file was deleted.

11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.EditAction;
import org.jabref.gui.SendAsEMailAction;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.Actions;
import org.jabref.gui.actions.OldCommandWrapper;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.edit.CopyMoreAction;
import org.jabref.gui.exporter.ExportToClipboardAction;
Expand All @@ -36,11 +35,11 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
ContextMenu contextMenu = new ContextMenu();
ActionFactory factory = new ActionFactory(keyBindingRepository);

contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new OldCommandWrapper(Actions.COPY, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, panel.frame(), stateManager)));
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService, stateManager, preferencesService));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new OldCommandWrapper(Actions.PASTE, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new OldCommandWrapper(Actions.CUT, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE, new OldCommandWrapper(Actions.DELETE, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, panel.frame(), stateManager)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, panel.frame(), stateManager)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, panel.frame(), stateManager)));

contextMenu.getItems().add(new SeparatorMenuItem());

Expand Down

0 comments on commit dcdef68

Please sign in to comment.