Skip to content

Commit

Permalink
Migrate EditAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Nov 28, 2024
1 parent 83cc4b1 commit 6ec77c9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
99 changes: 56 additions & 43 deletions src/main/java/org/jabref/gui/edit/EditAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.swing.undo.UndoManager;

import javafx.scene.Node;
import javafx.scene.control.TextInputControl;
import javafx.scene.web.WebView;

Expand All @@ -19,14 +20,14 @@

/**
* 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.
* stateManager.getFocusOwner, and we call the action stored under the relevant name in its action map.
*/
public class EditAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(EditAction.class);

private final Supplier<LibraryTab> tabSupplier;
private final StandardActions action;
private final Supplier<LibraryTab> tabSupplier;
private final StateManager stateManager;
private final UndoManager undoManager;

Expand All @@ -43,58 +44,70 @@ public EditAction(StandardActions action, Supplier<LibraryTab> tabSupplier, Stat
}
}

public EditAction(StandardActions action, StateManager stateManager, UndoManager undoManager) {
this(action, () -> stateManager.activeTabProperty().get().get(), stateManager, undoManager);
}

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

@Override
public void execute() {
stateManager.getFocusOwner().ifPresent(focusOwner -> {
LOGGER.debug("focusOwner: {}; Action: {}", focusOwner, action.getText());
if (focusOwner instanceof TextInputControl textInput) {
// Focus is on text field -> copy/paste/cut selected text
// DELETE_ENTRY in text field should do forward delete
switch (action) {
case SELECT_ALL -> textInput.selectAll();
case COPY -> textInput.copy();
case CUT -> textInput.cut();
case PASTE -> textInput.paste();
case DELETE -> textInput.clear();
case DELETE_ENTRY -> textInput.deleteNextChar();
case UNDO -> textInput.undo();
case REDO -> textInput.redo();
default -> {
String message = "Only cut/copy/paste supported in TextInputControl but got " + action;
LOGGER.error(message);
throw new IllegalStateException(message);
}
if (tabSupplier.get() == null) {
LOGGER.debug("No library selected");
return;
}

Node focusOwner = stateManager.getFocusOwner().get();
if (stateManager.getFocusOwner().isEmpty()) {
LOGGER.debug("No control in focus");
return;
}

LOGGER.debug("focusOwner: {}; Action: {}", focusOwner, action.getText());
if (focusOwner instanceof TextInputControl textInput) {
// Focus is on text field -> copy/paste/cut selected text
// DELETE_ENTRY in text field should do forward delete
switch (action) {
case SELECT_ALL -> textInput.selectAll();
case COPY -> textInput.copy();
case CUT -> textInput.cut();
case PASTE -> textInput.paste();
case DELETE -> textInput.clear();
case DELETE_ENTRY -> textInput.deleteNextChar();
case UNDO -> textInput.undo();
case REDO -> textInput.redo();
default -> {
String message = "Only cut/copy/paste supported in TextInputControl but got " + action;
LOGGER.error(message);
throw new IllegalStateException(message);
}
} else if ((focusOwner instanceof CodeArea) || (focusOwner instanceof WebView)) {
LOGGER.debug("Ignoring request in CodeArea or WebView");
return;
} else {
LOGGER.debug("Else: {}", focusOwner.getClass().getSimpleName());
// Not sure what is selected -> copy/paste/cut selected entries except for Preview and CodeArea

switch (action) {
case COPY -> tabSupplier.get().copyEntry();
case CUT -> tabSupplier.get().cutEntry();
case PASTE -> tabSupplier.get().pasteEntry();
case DELETE_ENTRY -> tabSupplier.get().deleteEntry();
case UNDO -> {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
} else if ((focusOwner instanceof CodeArea) || (focusOwner instanceof WebView)) {
LOGGER.debug("Ignoring request in CodeArea or WebView");
} else {
LOGGER.debug("Else: {}", focusOwner.getClass().getSimpleName());
// Not sure what is selected -> copy/paste/cut selected entries except for Preview and CodeArea

switch (action) {
case COPY -> tabSupplier.get().copyEntry();
case CUT -> tabSupplier.get().cutEntry();
case PASTE -> tabSupplier.get().pasteEntry();
case DELETE_ENTRY -> tabSupplier.get().deleteEntry();
case UNDO -> {
if (undoManager.canUndo()) {
undoManager.undo();
}
case REDO -> {
if (undoManager.canRedo()) {
undoManager.redo();
}
}
case REDO -> {
if (undoManager.canRedo()) {
undoManager.redo();
}
default -> LOGGER.debug("Only cut/copy/paste/deleteEntry supported but got: {} and focus owner {}", action, focusOwner);
}
default -> LOGGER.debug("Only cut/copy/paste/deleteEntry supported but got: {} and focus owner {}", action, focusOwner);
}
});
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ private void createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, stateManager, undoManager)),

factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, stateManager, undoManager)),
factory.createSubMenu(StandardActions.COPY_MORE,
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
Expand All @@ -203,7 +203,7 @@ private void createMenu() {
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(dialogService, stateManager, clipBoardManager, taskExecutor, preferences))),

factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, stateManager, undoManager)),

new SeparatorMenuItem(),

Expand Down Expand Up @@ -238,7 +238,7 @@ private void createMenu() {
library.getItems().addAll(
factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(dialogService, preferences, stateManager)),
factory.createMenuItem(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new PlainCitationParserAction(dialogService, stateManager)),
factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, stateManager, undoManager)),

new SeparatorMenuItem(),

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/frame/MainToolBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ private void createToolBar() {
factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(dialogService, preferences, stateManager)),
createNewEntryFromIdButton(),
factory.createIconButton(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new PlainCitationParserAction(dialogService, stateManager)),
factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, frame::getCurrentLibraryTab, stateManager, undoManager))),
factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, stateManager, undoManager))),

new Separator(Orientation.VERTICAL),

new HBox(
factory.createIconButton(StandardActions.UNDO, new UndoAction(undoManager, dialogService, stateManager)),
factory.createIconButton(StandardActions.REDO, new RedoAction(undoManager, dialogService, stateManager)),
factory.createIconButton(StandardActions.CUT, new EditAction(StandardActions.CUT, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createIconButton(StandardActions.COPY, new EditAction(StandardActions.COPY, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createIconButton(StandardActions.PASTE, new EditAction(StandardActions.PASTE, frame::getCurrentLibraryTab, stateManager, undoManager))),
factory.createIconButton(StandardActions.CUT, new EditAction(StandardActions.CUT, stateManager, undoManager)),
factory.createIconButton(StandardActions.COPY, new EditAction(StandardActions.COPY, stateManager, undoManager)),
factory.createIconButton(StandardActions.PASTE, new EditAction(StandardActions.PASTE, stateManager, undoManager))),

new Separator(Orientation.VERTICAL),

Expand Down

0 comments on commit 6ec77c9

Please sign in to comment.