Skip to content

Commit 3d24714

Browse files
authored
[WIP] Remove BaseAction from JabRefFrame (#6056)
* Added UndoRedoAction * Refactored AppendDatabaseAction * Refactored ReplaceStringAction and GenerateBibtexKeyAction * Refactored CleanupAction * Added OpenEntryEditorAction, fixed bug about selected entries without open database * Refactored AbbreviateAction, merged UnabbreviateAction, removed unused Actions * Refactored DownloadFullTextAction * Added PullChangesFromSharedAction * Added SaveAction, removed remaining deprecated OldDatabaseCommandWrapper, BaseAction and Actions * Added UndoRedoAction * Refactored AppendDatabaseAction * Refactored ReplaceStringAction and GenerateBibtexKeyAction * Refactored CleanupAction * Added OpenEntryEditorAction, fixed bug about selected entries without open database * Refactored AbbreviateAction, merged UnabbreviateAction, removed unused Actions * Refactored DownloadFullTextAction * Added PullChangesFromSharedAction * Added SaveAction, removed remaining deprecated OldDatabaseCommandWrapper, BaseAction and Actions * Removed test, added default case for codacy compliance * Removed test for notifications
1 parent 8060338 commit 3d24714

20 files changed

+499
-569
lines changed

src/main/java/org/jabref/gui/BasePanel.java

+4-127
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
import java.nio.file.Path;
44
import java.util.Collections;
5-
import java.util.HashMap;
65
import java.util.List;
7-
import java.util.Map;
86
import java.util.Objects;
97
import java.util.Optional;
108

11-
import javax.swing.undo.CannotRedoException;
12-
import javax.swing.undo.CannotUndoException;
13-
149
import javafx.application.Platform;
1510
import javafx.beans.binding.Bindings;
1611
import javafx.geometry.Orientation;
@@ -20,25 +15,14 @@
2015

2116
import org.jabref.Globals;
2217
import org.jabref.JabRefExecutorService;
23-
import org.jabref.gui.actions.Actions;
24-
import org.jabref.gui.actions.BaseAction;
2518
import org.jabref.gui.autocompleter.AutoCompletePreferences;
2619
import org.jabref.gui.autocompleter.AutoCompleteUpdater;
2720
import org.jabref.gui.autocompleter.PersonNameSuggestionProvider;
2821
import org.jabref.gui.autocompleter.SuggestionProviders;
29-
import org.jabref.gui.bibtexkeypattern.GenerateBibtexKeyAction;
30-
import org.jabref.gui.cleanup.CleanupAction;
3122
import org.jabref.gui.collab.DatabaseChangeMonitor;
3223
import org.jabref.gui.collab.DatabaseChangePane;
33-
import org.jabref.gui.edit.ReplaceStringAction;
3424
import org.jabref.gui.entryeditor.EntryEditor;
35-
import org.jabref.gui.exporter.SaveDatabaseAction;
36-
import org.jabref.gui.externalfiles.DownloadFullTextAction;
3725
import org.jabref.gui.externalfiletype.ExternalFileTypes;
38-
import org.jabref.gui.importer.actions.AppendDatabaseAction;
39-
import org.jabref.gui.journals.AbbreviateAction;
40-
import org.jabref.gui.journals.AbbreviationType;
41-
import org.jabref.gui.journals.UnabbreviateAction;
4226
import org.jabref.gui.maintable.MainTable;
4327
import org.jabref.gui.maintable.MainTableDataModel;
4428
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
@@ -62,7 +46,6 @@
6246
import org.jabref.model.database.event.EntriesAddedEvent;
6347
import org.jabref.model.database.event.EntriesRemovedEvent;
6448
import org.jabref.model.database.shared.DatabaseLocation;
65-
import org.jabref.model.database.shared.DatabaseSynchronizer;
6649
import org.jabref.model.entry.BibEntry;
6750
import org.jabref.model.entry.event.EntriesEventSource;
6851
import org.jabref.model.entry.event.EntryChangedEvent;
@@ -88,11 +71,8 @@ public class BasePanel extends StackPane {
8871

8972
private final JabRefFrame frame;
9073
// The undo manager.
91-
private final UndoAction undoAction = new UndoAction();
92-
private final RedoAction redoAction = new RedoAction();
9374
private final CountingUndoManager undoManager;
94-
// Keeps track of the string dialog if it is open.
95-
private final Map<Actions, BaseAction> actions = new HashMap<>();
75+
9676
private final SidePaneManager sidePaneManager;
9777
private final ExternalFileTypes externalFileTypes;
9878

@@ -139,8 +119,6 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
139119

140120
setupMainPanel();
141121

142-
setupActions();
143-
144122
this.getDatabase().registerListener(new SearchListener());
145123
this.getDatabase().registerListener(new EntriesRemovedListener());
146124

@@ -153,7 +131,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
153131

154132
this.entryEditor = new EntryEditor(this, externalFileTypes);
155133
// Open entry editor for first entry on start up.
156-
Platform.runLater(() -> clearAndSelectFirst());
134+
Platform.runLater(this::clearAndSelectFirst);
157135
}
158136

159137
@Subscribe
@@ -216,48 +194,6 @@ public void output(String s) {
216194
dialogService.notify(s);
217195
}
218196

219-
private void setupActions() {
220-
SaveDatabaseAction saveAction = new SaveDatabaseAction(this, Globals.prefs, Globals.entryTypesManager);
221-
CleanupAction cleanUpAction = new CleanupAction(this, Globals.prefs, Globals.TASK_EXECUTOR);
222-
223-
actions.put(Actions.UNDO, undoAction);
224-
actions.put(Actions.REDO, redoAction);
225-
226-
// The action for opening an entry editor.
227-
actions.put(Actions.EDIT, this::showAndEdit);
228-
229-
// The action for saving a database.
230-
actions.put(Actions.SAVE, saveAction::save);
231-
232-
actions.put(Actions.SAVE_AS, saveAction::saveAs);
233-
234-
actions.put(Actions.SAVE_SELECTED_AS_PLAIN, saveAction::saveSelectedAsPlain);
235-
236-
actions.put(Actions.SELECT_ALL, mainTable.getSelectionModel()::selectAll);
237-
238-
// The action for auto-generating keys.
239-
actions.put(Actions.MAKE_KEY, new GenerateBibtexKeyAction(this, frame.getDialogService()));
240-
241-
// The action for cleaning up entry.
242-
actions.put(Actions.CLEANUP, cleanUpAction);
243-
244-
actions.put(Actions.MERGE_DATABASE, new AppendDatabaseAction(frame, this));
245-
246-
actions.put(Actions.PULL_CHANGES_FROM_SHARED_DATABASE, () -> {
247-
DatabaseSynchronizer dbmsSynchronizer = frame.getCurrentBasePanel().getBibDatabaseContext().getDBMSSynchronizer();
248-
dbmsSynchronizer.pullChanges();
249-
});
250-
251-
actions.put(Actions.REPLACE_ALL, () -> (new ReplaceStringAction(this)).execute());
252-
253-
actions.put(Actions.ABBREVIATE_DEFAULT, new AbbreviateAction(this, AbbreviationType.DEFAULT));
254-
actions.put(Actions.ABBREVIATE_MEDLINE, new AbbreviateAction(this, AbbreviationType.MEDLINE));
255-
actions.put(Actions.ABBREVIATE_SHORTEST_UNIQUE, new AbbreviateAction(this, AbbreviationType.SHORTEST_UNIQUE));
256-
actions.put(Actions.UNABBREVIATE, new UnabbreviateAction(this));
257-
258-
actions.put(Actions.DOWNLOAD_FULL_TEXT, new DownloadFullTextAction(this)::execute);
259-
}
260-
261197
/**
262198
* Removes the selected entries from the database
263199
*
@@ -297,26 +233,6 @@ public void delete(BibEntry entry) {
297233
delete(false, Collections.singletonList(entry));
298234
}
299235

300-
/**
301-
* This method is called from JabRefFrame if a database specific action is requested by the user. Runs the command
302-
* if it is defined, or prints an error message to the standard error stream.
303-
*
304-
* @param command The name of the command to run.
305-
*/
306-
public void runCommand(final Actions command) {
307-
if (!actions.containsKey(command)) {
308-
LOGGER.info("No action defined for '" + command + '\'');
309-
return;
310-
}
311-
312-
BaseAction action = actions.get(command);
313-
try {
314-
action.action();
315-
} catch (Throwable ex) {
316-
LOGGER.error("runCommand error: " + ex.getMessage(), ex);
317-
}
318-
}
319-
320236
public void registerUndoableChanges(List<FieldChange> changes) {
321237
NamedCompound ce = new NamedCompound(Localization.lang("Save actions"));
322238
for (FieldChange change : changes) {
@@ -567,12 +483,6 @@ private void showBottomPane(BasePanelMode newMode) {
567483
adjustSplitter();
568484
}
569485

570-
private void showAndEdit() {
571-
if (!mainTable.getSelectedEntries().isEmpty()) {
572-
showAndEdit(mainTable.getSelectedEntries().get(0));
573-
}
574-
}
575-
576486
/**
577487
* Removes the bottom component.
578488
*/
@@ -594,7 +504,7 @@ public void clearAndSelect(final BibEntry bibEntry) {
594504
*/
595505
private void clearAndSelectFirst() {
596506
mainTable.clearAndSelectFirst();
597-
showAndEdit();
507+
showAndEdit(mainTable.getSelectedEntries().get(0));
598508
}
599509

600510
public void selectPreviousEntry() {
@@ -645,7 +555,7 @@ public void markNonUndoableBaseChanged() {
645555
markBaseChanged();
646556
}
647557

648-
private synchronized void markChangedOrUnChanged() {
558+
public synchronized void markChangedOrUnChanged() {
649559
if (getUndoManager().hasChanged()) {
650560
if (!baseChanged) {
651561
markBaseChanged();
@@ -864,37 +774,4 @@ public void listen(EntriesRemovedEvent removedEntriesEvent) {
864774
DefaultTaskExecutor.runInJavaFXThread(() -> frame.getGlobalSearchBar().performSearch());
865775
}
866776
}
867-
868-
private class UndoAction implements BaseAction {
869-
870-
@Override
871-
public void action() {
872-
try {
873-
getUndoManager().undo();
874-
markBaseChanged();
875-
output(Localization.lang("Undo"));
876-
} catch (CannotUndoException ex) {
877-
LOGGER.warn("Nothing to undo", ex);
878-
output(Localization.lang("Nothing to undo") + '.');
879-
}
880-
881-
markChangedOrUnChanged();
882-
}
883-
}
884-
885-
private class RedoAction implements BaseAction {
886-
887-
@Override
888-
public void action() {
889-
try {
890-
getUndoManager().redo();
891-
markBaseChanged();
892-
output(Localization.lang("Redo"));
893-
} catch (CannotRedoException ex) {
894-
output(Localization.lang("Nothing to redo") + '.');
895-
}
896-
897-
markChangedOrUnChanged();
898-
}
899-
}
900777
}

0 commit comments

Comments
 (0)