Skip to content

Commit

Permalink
Refactored ActionHelper, removed some deprecated Actions and minor co…
Browse files Browse the repository at this point in the history
…rrections
  • Loading branch information
calixtus committed Feb 20, 2020
1 parent f922be3 commit bca3a0b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 91 deletions.
40 changes: 0 additions & 40 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,42 +260,13 @@ private void setupActions() {
// The action for cleaning up entry.
actions.put(Actions.CLEANUP, cleanUpAction);

// actions.put(Actions.MERGE_ENTRIES, () -> new MergeEntriesAction(frame, Globals.stateManager).execute());

// The action for copying the selected entry's key.
// actions.put(Actions.COPY_KEY, this::copyKey);

// The action for copying the selected entry's title.
// actions.put(Actions.COPY_TITLE, this::copyTitle);

// The action for copying a cite for the selected entry.
// actions.put(Actions.COPY_CITE_KEY, this::copyCiteKey);

// The action for copying the BibTeX key and the title for the first selected entry
// actions.put(Actions.COPY_KEY_AND_TITLE, this::copyKeyAndTitle);

/*
actions.put(Actions.COPY_CITATION_ASCII_DOC, () -> copyCitationToClipboard(CitationStyleOutputFormat.ASCII_DOC));
actions.put(Actions.COPY_CITATION_XSLFO, () -> copyCitationToClipboard(CitationStyleOutputFormat.XSL_FO));
actions.put(Actions.COPY_CITATION_HTML, () -> copyCitationToClipboard(CitationStyleOutputFormat.HTML));
actions.put(Actions.COPY_CITATION_RTF, () -> copyCitationToClipboard(CitationStyleOutputFormat.RTF));
actions.put(Actions.COPY_CITATION_TEXT, () -> copyCitationToClipboard(CitationStyleOutputFormat.TEXT));
*/

// The action for copying the BibTeX keys as hyperlinks to the urls of the selected entries
// actions.put(Actions.COPY_KEY_AND_LINK, new CopyBibTeXKeyAndLinkAction(mainTable, Globals.clipboardManager));

actions.put(Actions.MERGE_DATABASE, new AppendDatabaseAction(frame, this));

actions.put(Actions.PULL_CHANGES_FROM_SHARED_DATABASE, () -> {
DatabaseSynchronizer dbmsSynchronizer = frame.getCurrentBasePanel().getBibDatabaseContext().getDBMSSynchronizer();
dbmsSynchronizer.pullChanges();
});

// actions.put(Actions.OPEN_URL, new OpenURLAction());

// actions.put(Actions.MERGE_WITH_FETCHED_ENTRY, new MergeWithFetchedEntryAction(this, frame.getDialogService()));

actions.put(Actions.REPLACE_ALL, () -> (new ReplaceStringAction(this)).execute());

actions.put(new SpecialFieldValueViewModel(SpecialField.RELEVANCE.getValues().get(0)).getCommand(),
Expand All @@ -320,17 +291,6 @@ private void setupActions() {
new SpecialFieldViewModel(SpecialField.READ_STATUS, undoManager).getSpecialFieldAction(status, this.frame));
}

/* actions.put(Actions.NEXT_PREVIEW_STYLE, () -> {
entryEditor.nextPreviewStyle();
});
actions.put(Actions.PREVIOUS_PREVIEW_STYLE, () -> {
entryEditor.previousPreviewStyle();
}); */

// actions.put(Actions.SEND_AS_EMAIL, new SendAsEMailAction(frame));

// actions.put(Actions.WRITE_XMP, new WriteXMPAction(this)::execute);

actions.put(Actions.ABBREVIATE_DEFAULT, new AbbreviateAction(this, AbbreviationType.DEFAULT));
actions.put(Actions.ABBREVIATE_MEDLINE, new AbbreviateAction(this, AbbreviationType.MEDLINE));
actions.put(Actions.ABBREVIATE_SHORTEST_UNIQUE, new AbbreviateAction(this, AbbreviationType.SHORTEST_UNIQUE));
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.jabref.gui.dialogs.AutosaveUIManager;
import org.jabref.gui.documentviewer.ShowDocumentViewerAction;
import org.jabref.gui.duplicationFinder.DuplicateSearch;
import org.jabref.gui.edit.CopyMoreAction;
import org.jabref.gui.edit.ManageKeywordsAction;
import org.jabref.gui.edit.MassSetFieldsAction;
import org.jabref.gui.edit.OpenBrowserAction;
Expand Down Expand Up @@ -703,11 +704,11 @@ private MenuBar createMenu() {

factory.createMenuItem(StandardActions.COPY, new EditAction(Actions.COPY)),
factory.createSubMenu(StandardActions.COPY_MORE,
factory.createMenuItem(StandardActions.COPY_TITLE, new OldDatabaseCommandWrapper(Actions.COPY_TITLE, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_KEY, new OldDatabaseCommandWrapper(Actions.COPY_KEY, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new OldDatabaseCommandWrapper(Actions.COPY_CITE_KEY, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_TITLE, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_LINK, this, stateManager)),
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)),
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new CopyMoreAction(StandardActions.COPY_CITE_KEY, dialogService, stateManager, Globals.clipboardManager, prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.clipboardManager, prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.clipboardManager, prefs)),
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))),

Expand Down Expand Up @@ -752,7 +753,7 @@ private MenuBar createMenu() {

quality.getItems().addAll(
factory.createMenuItem(StandardActions.FIND_DUPLICATES, new DuplicateSearch(this, dialogService, stateManager)),
factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(this.getCurrentBasePanel(), dialogService, stateManager)),
factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(dialogService, stateManager)),
factory.createMenuItem(StandardActions.CHECK_INTEGRITY, new IntegrityCheckAction(this, stateManager, Globals.TASK_EXECUTOR)),
factory.createMenuItem(StandardActions.CLEANUP_ENTRIES, new OldDatabaseCommandWrapper(Actions.CLEANUP, this, stateManager)),

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/OpenConsoleAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.StandardField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,7 +18,7 @@ public class OpenConsoleAction extends SimpleCommand {
public OpenConsoleAction(StateManager stateManager) {
this.stateManager = stateManager;

this.executable.bind(ActionHelper.isFieldSetForSelectedEntry(StandardField.FILE, stateManager));
this.executable.bind(ActionHelper.needsDatabase(stateManager));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/ActionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static BooleanExpression isFieldSetForSelectedEntry(Field field, StateMan
public static BooleanExpression isAnyFieldSetForSelectedEntry(List<Field> fields, StateManager stateManager) {
BibEntry entry = stateManager.getSelectedEntries().get(0);
return Bindings.createBooleanBinding(
() -> !Collections.disjoint(fields, entry.getFields()),
() -> entry.getFields().stream().anyMatch(fields::contains),
entry.getFieldsObservable(),
stateManager.getSelectedEntries());
}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/org/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ public enum Actions {
ADD_FILE_LINK,
CLEANUP,
COPY,
COPY_CITATION_ASCII_DOC,
COPY_CITATION_XSLFO,
COPY_CITATION_HTML,
COPY_CITATION_RTF,
COPY_CITATION_TEXT,
COPY_KEY,
COPY_CITE_KEY,
COPY_KEY_AND_TITLE,
COPY_KEY_AND_LINK,
COPY_TITLE,
CUT,
DELETE,
DOWNLOAD_FULL_TEXT,
Expand All @@ -31,27 +21,17 @@ public enum Actions {
MAKE_KEY,
MANAGE_SELECTORS,
MERGE_DATABASE,
MERGE_ENTRIES,
MERGE_WITH_FETCHED_ENTRY,
NEXT_PREVIEW_STYLE,
OPEN_CONSOLE,
OPEN_EXTERNAL_FILE,
OPEN_FOLDER,
OPEN_URL,
PASTE,
PREVIOUS_PREVIEW_STYLE,
PULL_CHANGES_FROM_SHARED_DATABASE,
REDO,
REPLACE_ALL,
SAVE,
SAVE_AS,
SAVE_SELECTED_AS_PLAIN,
SELECT_ALL,
SEND_AS_EMAIL,
TOGGLE_GROUPS,
UNABBREVIATE,
UNDO,
WRITE_XMP,
PRINT_PREVIEW,
TOGGLE_PRINTED,
CLEAR_PRIORITY,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
public class CopyMoreAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(CopyMoreAction.class);
private StandardActions action;
private DialogService dialogService;
private StateManager stateManager;
private final StandardActions action;
private final DialogService dialogService;
private final StateManager stateManager;
private final ClipBoardManager clipBoardManager;
private PreferencesService preferencesService;
private final PreferencesService preferencesService;

public CopyMoreAction(StandardActions action, DialogService dialogService, StateManager stateManager, ClipBoardManager clipBoardManager, PreferencesService preferencesService) {
this.action = action;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/jabref/gui/maintable/OpenUrlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Optional;

import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.BooleanExpression;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
Expand All @@ -25,10 +25,9 @@ public OpenUrlAction(DialogService dialogService, StateManager stateManager) {
this.dialogService = dialogService;
this.stateManager = stateManager;

BooleanBinding fieldIsSet = ActionHelper.isFieldSetForSelectedEntry(StandardField.URL, stateManager)
.or(ActionHelper.isFieldSetForSelectedEntry(StandardField.DOI, stateManager)
.or(ActionHelper.isFieldSetForSelectedEntry(StandardField.URI, stateManager)
.or(ActionHelper.isFieldSetForSelectedEntry(StandardField.EPRINT, stateManager))));
BooleanExpression fieldIsSet = ActionHelper.isAnyFieldSetForSelectedEntry(
List.of(StandardField.URL, StandardField.DOI, StandardField.URI, StandardField.EPRINT),
stateManager);
this.executable.bind(ActionHelper.needsEntriesSelected(1, stateManager).and(fieldIsSet));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
contextMenu.getItems().add(new ChangeEntryTypeMenu().getChangeEntryTypeMenu(entry.getEntry(), panel.getBibDatabaseContext(), panel.getUndoManager()));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.MERGE_WITH_FETCHED_ENTRY, new MergeWithFetchedEntryAction(panel, dialogService, stateManager)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.ATTACH_FILE, new AttachFileAction(panel, dialogService, stateManager, preferencesService)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(panel, dialogService, stateManager)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(dialogService, stateManager)));

return contextMenu;
}
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/jabref/gui/mergeentries/MergeEntriesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Optional;

import org.jabref.gui.BasePanel;
import org.jabref.Globals;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
Expand All @@ -13,31 +13,35 @@
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

public class MergeEntriesAction extends SimpleCommand {

private final BasePanel basePanel;
private final DialogService dialogService;
private final StateManager stateManager;

public MergeEntriesAction(BasePanel panel, DialogService dialogService, StateManager stateManager) {
this.basePanel = panel;
public MergeEntriesAction(DialogService dialogService, StateManager stateManager) {
this.dialogService = dialogService;
this.stateManager = stateManager;

this.executable.bind(ActionHelper.needsEntriesSelected(2, stateManager));
}

@Override
public void execute() {
if (stateManager.getActiveDatabase().isEmpty()) {
return;
}
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();

// Check if there are two entries selected
List<BibEntry> selectedEntries = basePanel.getSelectedEntries();
List<BibEntry> selectedEntries = stateManager.getSelectedEntries();
if (selectedEntries.size() != 2) {
// Inform the user to select entries first.
dialogService.showInformationDialogAndWait(
Localization.lang("Merge entries"),
Localization.lang("You have to choose exactly two entries to merge."));

return;
}

Expand All @@ -49,17 +53,19 @@ public void execute() {
dlg.setTitle(Localization.lang("Merge entries"));
Optional<BibEntry> mergedEntry = dlg.showAndWait();
if (mergedEntry.isPresent()) {
basePanel.insertEntry(mergedEntry.get());
// FixMe: BibDatabase::insertEntry does not contain logic to mark the BasePanel as changed and to mark
// entries with a timestamp, only BasePanel::insertEntry does.
databaseContext.getDatabase().insertEntry(mergedEntry.get());

// Create a new entry and add it to the undo stack
// Remove the other two entries and add them to the undo stack (which is not working...)
NamedCompound ce = new NamedCompound(Localization.lang("Merge entries"));
ce.addEdit(new UndoableInsertEntries(basePanel.getDatabase(), mergedEntry.get()));
ce.addEdit(new UndoableInsertEntries(databaseContext.getDatabase(), mergedEntry.get()));
List<BibEntry> entriesToRemove = Arrays.asList(one, two);
ce.addEdit(new UndoableRemoveEntries(basePanel.getDatabase(), entriesToRemove));
basePanel.getDatabase().removeEntries(entriesToRemove);
ce.addEdit(new UndoableRemoveEntries(databaseContext.getDatabase(), entriesToRemove));
databaseContext.getDatabase().removeEntries(entriesToRemove);
ce.end();
basePanel.getUndoManager().addEdit(ce);
Globals.undoManager.addEdit(ce); // ToDo: Rework UndoManager and extract Globals

dialogService.notify(Localization.lang("Merged entries"));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CopyCitationAction extends SimpleCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(CopyCitationAction.class);

private final List<BibEntry> selectedEntries;
private StateManager stateManager;
private final StateManager stateManager;
private final PreviewLayout style;
private final String previewStyle;
private final CitationStyleOutputFormat outputFormat;
Expand Down

0 comments on commit bca3a0b

Please sign in to comment.