Skip to content

Commit dcdef68

Browse files
committed
Extracted BaseAction out of EditAction
1 parent a800bf4 commit dcdef68

File tree

8 files changed

+98
-148
lines changed

8 files changed

+98
-148
lines changed

Diff for: src/main/java/org/jabref/gui/BasePanel.java

-15
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,6 @@ private void setupActions() {
233233

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

236-
// The action for copying selected entries.
237-
actions.put(Actions.COPY, this::copy);
238-
239-
actions.put(Actions.CUT, this::cut);
240-
241-
actions.put(Actions.DELETE, () -> delete(false));
242-
243-
// The action for pasting entries or cell contents.
244-
// - more robust detection of available content flavors (doesn't only look at first one offered)
245-
// - support for parsing string-flavor clipboard contents which are bibtex entries.
246-
// This allows you to (a) paste entire bibtex entries from a text editor, web browser, etc
247-
// (b) copy and paste entries between multiple instances of JabRef (since
248-
// only the text representation seems to get as far as the X clipboard, at least on my system)
249-
actions.put(Actions.PASTE, this::paste);
250-
251236
actions.put(Actions.SELECT_ALL, mainTable.getSelectionModel()::selectAll);
252237

253238
// The action for auto-generating keys.

Diff for: src/main/java/org/jabref/gui/EditAction.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.jabref.gui;
2+
3+
import javafx.scene.control.TextInputControl;
4+
5+
import org.jabref.gui.actions.SimpleCommand;
6+
import org.jabref.gui.actions.StandardActions;
7+
8+
/**
9+
* Class for handling general actions; cut, copy and paste. The focused component is kept track of by
10+
* Globals.focusListener, and we call the action stored under the relevant name in its action map.
11+
*/
12+
public class EditAction extends SimpleCommand {
13+
14+
private final JabRefFrame frame;
15+
private final StandardActions action;
16+
private final StateManager stateManager;
17+
18+
public EditAction(StandardActions action, JabRefFrame frame, StateManager stateManager) {
19+
this.action = action;
20+
this.frame = frame;
21+
this.stateManager = stateManager;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return this.action.toString();
27+
}
28+
29+
@Override
30+
public void execute() {
31+
stateManager.getFocusOwner().ifPresent(focusOwner -> {
32+
if (focusOwner instanceof TextInputControl) {
33+
// Focus is on text field -> copy/paste/cut selected text
34+
TextInputControl textInput = (TextInputControl) focusOwner;
35+
switch (action) {
36+
case COPY:
37+
textInput.copy();
38+
break;
39+
case CUT:
40+
textInput.cut();
41+
break;
42+
case PASTE:
43+
// handled by FX in TextInputControl#paste
44+
break;
45+
default:
46+
throw new IllegalStateException("Only cut/copy/paste supported in TextInputControl but got " + action);
47+
}
48+
} else {
49+
// Not sure what is selected -> copy/paste/cut selected entries
50+
51+
// ToDo: Should be handled by BibDatabaseContext instead of BasePanel
52+
switch (action) {
53+
case COPY:
54+
frame.getCurrentBasePanel().copy();
55+
break;
56+
case CUT:
57+
frame.getCurrentBasePanel().cut();
58+
break;
59+
case PASTE:
60+
// handled by FX in TextInputControl#paste
61+
break;
62+
case DELETE_ENTRY:
63+
frame.getCurrentBasePanel().delete(false);
64+
break;
65+
default:
66+
throw new IllegalStateException("Only cut/copy/paste supported but got " + action);
67+
}
68+
}
69+
});
70+
}
71+
}

Diff for: src/main/java/org/jabref/gui/JabRefFrame.java

+11-66
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javafx.scene.control.SplitPane;
3131
import javafx.scene.control.Tab;
3232
import javafx.scene.control.TabPane;
33-
import javafx.scene.control.TextInputControl;
3433
import javafx.scene.control.ToolBar;
3534
import javafx.scene.control.Tooltip;
3635
import javafx.scene.control.skin.TabPaneSkin;
@@ -487,13 +486,13 @@ private Node createToolbar() {
487486
factory.createIconButton(StandardActions.NEW_ARTICLE, new NewEntryAction(this, StandardEntryType.Article, dialogService, Globals.prefs, stateManager)),
488487
factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, Globals.prefs, stateManager)),
489488
factory.createIconButton(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new ExtractBibtexAction(stateManager)),
490-
factory.createIconButton(StandardActions.DELETE_ENTRY, new OldDatabaseCommandWrapper(Actions.DELETE, this, stateManager)),
489+
factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, this, stateManager)),
491490
new Separator(Orientation.VERTICAL),
492491
factory.createIconButton(StandardActions.UNDO, new OldDatabaseCommandWrapper(Actions.UNDO, this, stateManager)),
493492
factory.createIconButton(StandardActions.REDO, new OldDatabaseCommandWrapper(Actions.REDO, this, stateManager)),
494-
factory.createIconButton(StandardActions.CUT, new OldDatabaseCommandWrapper(Actions.CUT, this, stateManager)),
495-
factory.createIconButton(StandardActions.COPY, new OldDatabaseCommandWrapper(Actions.COPY, this, stateManager)),
496-
factory.createIconButton(StandardActions.PASTE, new OldDatabaseCommandWrapper(Actions.PASTE, this, stateManager)),
493+
factory.createIconButton(StandardActions.CUT, new EditAction(StandardActions.CUT,this, stateManager)),
494+
factory.createIconButton(StandardActions.COPY, new EditAction(StandardActions.COPY,this, stateManager)),
495+
factory.createIconButton(StandardActions.PASTE, new EditAction(StandardActions.PASTE,this, stateManager)),
497496
new Separator(Orientation.VERTICAL),
498497
pushToApplicationButton,
499498
factory.createIconButton(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, stateManager)),
@@ -577,6 +576,9 @@ public void init() {
577576
}
578577
});
579578

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

702704
new SeparatorMenuItem(),
703705

704-
factory.createMenuItem(StandardActions.CUT, new EditAction(Actions.CUT)),
706+
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, this, stateManager)),
705707

706-
factory.createMenuItem(StandardActions.COPY, new EditAction(Actions.COPY)),
708+
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, this, stateManager)),
707709
factory.createSubMenu(StandardActions.COPY_MORE,
708710
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, Globals.clipboardManager, prefs)),
709711
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, Globals.clipboardManager, prefs)),
@@ -713,7 +715,7 @@ private MenuBar createMenu() {
713715
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, prefs.getPreviewPreferences())),
714716
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService))),
715717

716-
factory.createMenuItem(StandardActions.PASTE, new EditAction(Actions.PASTE)),
718+
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, this, stateManager)),
717719

718720
new SeparatorMenuItem(),
719721

@@ -743,7 +745,7 @@ private MenuBar createMenu() {
743745
library.getItems().addAll(
744746
factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, Globals.prefs, stateManager)),
745747
factory.createMenuItem(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new ExtractBibtexAction(stateManager)),
746-
factory.createMenuItem(StandardActions.DELETE_ENTRY, new OldDatabaseCommandWrapper(Actions.DELETE, this, stateManager)),
748+
factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, this, stateManager)),
747749

748750
new SeparatorMenuItem(),
749751

@@ -1244,63 +1246,6 @@ public void execute() {
12441246
}
12451247
}
12461248

1247-
/**
1248-
* Class for handling general actions; cut, copy and paste. The focused component is kept track of by
1249-
* Globals.focusListener, and we call the action stored under the relevant name in its action map.
1250-
*/
1251-
private class EditAction extends SimpleCommand {
1252-
1253-
private final Actions command;
1254-
1255-
public EditAction(Actions command) {
1256-
this.command = command;
1257-
}
1258-
1259-
@Override
1260-
public String toString() {
1261-
return this.command.toString();
1262-
}
1263-
1264-
@Override
1265-
public void execute() {
1266-
Node focusOwner = mainStage.getScene().getFocusOwner();
1267-
if (focusOwner != null) {
1268-
if (focusOwner instanceof TextInputControl) {
1269-
// Focus is on text field -> copy/paste/cut selected text
1270-
TextInputControl textInput = (TextInputControl) focusOwner;
1271-
switch (command) {
1272-
case COPY:
1273-
textInput.copy();
1274-
break;
1275-
case CUT:
1276-
textInput.cut();
1277-
break;
1278-
case PASTE:
1279-
// handled by FX in TextInputControl#paste
1280-
break;
1281-
default:
1282-
throw new IllegalStateException("Only cut/copy/paste supported but got " + command);
1283-
}
1284-
} else {
1285-
// Not sure what is selected -> copy/paste/cut selected entries
1286-
switch (command) {
1287-
case COPY:
1288-
getCurrentBasePanel().copy();
1289-
break;
1290-
case CUT:
1291-
getCurrentBasePanel().cut();
1292-
break;
1293-
case PASTE:
1294-
// handled by FX in TextInputControl#paste
1295-
break;
1296-
default:
1297-
throw new IllegalStateException("Only cut/copy/paste supported but got " + command);
1298-
}
1299-
}
1300-
}
1301-
}
1302-
}
1303-
13041249
private class CloseDatabaseAction extends SimpleCommand {
13051250

13061251
@Override

Diff for: src/main/java/org/jabref/gui/StateManager.java

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javafx.collections.FXCollections;
1414
import javafx.collections.ObservableList;
1515
import javafx.collections.ObservableMap;
16+
import javafx.scene.Node;
1617

1718
import org.jabref.gui.util.OptionalObjectProperty;
1819
import org.jabref.logic.search.SearchQuery;
@@ -27,6 +28,7 @@
2728
* - currently selected group
2829
* - active search
2930
* - active number of search results
31+
* - focus owner
3032
*/
3133
public class StateManager {
3234

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

4043
public StateManager() {
4144
activeGroups.bind(Bindings.valueAt(selectedGroups, activeDatabase.orElse(null)));
@@ -99,4 +102,8 @@ public void clearSearchQuery() {
99102
public void setSearchQuery(SearchQuery searchQuery) {
100103
activeSearchQuery.setValue(Optional.of(searchQuery));
101104
}
105+
106+
public OptionalObjectProperty<Node> focusOwnerProperty() { return focusOwner; }
107+
108+
public Optional<Node> getFocusOwner() { return focusOwner.get(); }
102109
}

Diff for: src/main/java/org/jabref/gui/actions/Actions.java

-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ public enum Actions {
1010
ABBREVIATE_SHORTEST_UNIQUE,
1111
ADD_FILE_LINK,
1212
CLEANUP,
13-
COPY,
14-
CUT,
15-
DELETE,
1613
DOWNLOAD_FULL_TEXT,
1714
EDIT,
1815
EDIT_PREAMBLE,
@@ -21,7 +18,6 @@ public enum Actions {
2118
MAKE_KEY,
2219
MANAGE_SELECTORS,
2320
MERGE_DATABASE,
24-
PASTE,
2521
PULL_CHANGES_FROM_SHARED_DATABASE,
2622
REDO,
2723
REPLACE_ALL,

Diff for: src/main/java/org/jabref/gui/actions/JabRefAction.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ private String getActionName(Action action, Command command) {
5858
return action.getText();
5959
} else {
6060
String commandName = command.getClass().getSimpleName();
61-
if ((command instanceof OldDatabaseCommandWrapper) || (command instanceof OldCommandWrapper) || commandName.contains("EditAction")) {
61+
if ((command instanceof OldDatabaseCommandWrapper)
62+
|| commandName.contains("EditAction")
63+
|| commandName.contains("CopyMoreAction")
64+
|| commandName.contains("CopyCitationAction")) {
6265
return command.toString();
6366
} else {
6467
return commandName;

Diff for: src/main/java/org/jabref/gui/actions/OldCommandWrapper.java

-56
This file was deleted.

Diff for: src/main/java/org/jabref/gui/maintable/RightClickMenu.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import org.jabref.Globals;
88
import org.jabref.gui.BasePanel;
99
import org.jabref.gui.DialogService;
10+
import org.jabref.gui.EditAction;
1011
import org.jabref.gui.SendAsEMailAction;
1112
import org.jabref.gui.StateManager;
1213
import org.jabref.gui.actions.ActionFactory;
13-
import org.jabref.gui.actions.Actions;
14-
import org.jabref.gui.actions.OldCommandWrapper;
1514
import org.jabref.gui.actions.StandardActions;
1615
import org.jabref.gui.edit.CopyMoreAction;
1716
import org.jabref.gui.exporter.ExportToClipboardAction;
@@ -36,11 +35,11 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
3635
ContextMenu contextMenu = new ContextMenu();
3736
ActionFactory factory = new ActionFactory(keyBindingRepository);
3837

39-
contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new OldCommandWrapper(Actions.COPY, panel)));
38+
contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, panel.frame(), stateManager)));
4039
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService, stateManager, preferencesService));
41-
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new OldCommandWrapper(Actions.PASTE, panel)));
42-
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new OldCommandWrapper(Actions.CUT, panel)));
43-
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE, new OldCommandWrapper(Actions.DELETE, panel)));
40+
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, panel.frame(), stateManager)));
41+
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, panel.frame(), stateManager)));
42+
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, panel.frame(), stateManager)));
4443

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

0 commit comments

Comments
 (0)