Skip to content

Commit

Permalink
Extracted BaseAction out of SpecialFieldMenuItemFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 22, 2020
1 parent c84be95 commit a5bf6b7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 184 deletions.
26 changes: 0 additions & 26 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
import org.jabref.gui.specialfields.SpecialFieldValueViewModel;
import org.jabref.gui.specialfields.SpecialFieldViewModel;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
Expand All @@ -70,8 +68,6 @@
import org.jabref.model.entry.event.EntryChangedEvent;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.SpecialFieldValue;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
Expand Down Expand Up @@ -269,28 +265,6 @@ private void setupActions() {

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

actions.put(new SpecialFieldValueViewModel(SpecialField.RELEVANCE.getValues().get(0)).getCommand(),
new SpecialFieldViewModel(SpecialField.RELEVANCE, undoManager).getSpecialFieldAction(SpecialField.RELEVANCE.getValues().get(0), frame));

actions.put(new SpecialFieldValueViewModel(SpecialField.QUALITY.getValues().get(0)).getCommand(),
new SpecialFieldViewModel(SpecialField.QUALITY, undoManager).getSpecialFieldAction(SpecialField.QUALITY.getValues().get(0), frame));

actions.put(new SpecialFieldValueViewModel(SpecialField.PRINTED.getValues().get(0)).getCommand(),
new SpecialFieldViewModel(SpecialField.PRINTED, undoManager).getSpecialFieldAction(SpecialField.PRINTED.getValues().get(0), frame));

for (SpecialFieldValue prio : SpecialField.PRIORITY.getValues()) {
actions.put(new SpecialFieldValueViewModel(prio).getCommand(),
new SpecialFieldViewModel(SpecialField.PRIORITY, undoManager).getSpecialFieldAction(prio, this.frame));
}
for (SpecialFieldValue rank : SpecialField.RANKING.getValues()) {
actions.put(new SpecialFieldValueViewModel(rank).getCommand(),
new SpecialFieldViewModel(SpecialField.RANKING, undoManager).getSpecialFieldAction(rank, this.frame));
}
for (SpecialFieldValue status : SpecialField.READ_STATUS.getValues()) {
actions.put(new SpecialFieldValueViewModel(status).getCommand(),
new SpecialFieldViewModel(SpecialField.READ_STATUS, undoManager).getSpecialFieldAction(status, this.frame));
}

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
12 changes: 6 additions & 6 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,12 @@ private MenuBar createMenu() {
if (Globals.prefs.getBoolean(JabRefPreferences.SPECIALFIELDSENABLED)) {
edit.getItems().addAll(
new SeparatorMenuItem(),
SpecialFieldMenuItemFactory.createSpecialFieldMenuForActiveDatabase(SpecialField.RANKING, factory, undoManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItemForActiveDatabase(SpecialField.RELEVANCE, factory),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItemForActiveDatabase(SpecialField.QUALITY, factory),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItemForActiveDatabase(SpecialField.PRINTED, factory),
SpecialFieldMenuItemFactory.createSpecialFieldMenuForActiveDatabase(SpecialField.PRIORITY, factory, undoManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenuForActiveDatabase(SpecialField.READ_STATUS, factory, undoManager)
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, getCurrentBasePanel(), dialogService, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, getCurrentBasePanel(), dialogService, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, getCurrentBasePanel(), dialogService, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, getCurrentBasePanel(), dialogService, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, getCurrentBasePanel(), dialogService, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, getCurrentBasePanel(), dialogService, stateManager)
);
}

Expand Down
19 changes: 1 addition & 18 deletions src/main/java/org/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,5 @@ public enum Actions {
SELECT_ALL,
TOGGLE_GROUPS,
UNABBREVIATE,
UNDO,
PRINT_PREVIEW,
TOGGLE_PRINTED,
CLEAR_PRIORITY,
SET_PRIORITY_1,
SET_PRIORITY_2,
SET_PRIORITY_3,
TOGGLE_QUALITY_ASSURED,
CLEAR_RANK,
SET_RANK_1,
SET_RANK_2,
SET_RANK_3,
SET_RANK_4,
SET_RANK_5,
CLEAR_READ_STATUS,
SET_READ_STATUS_TO_READ,
SET_READ_STATUS_TO_SKIMMED,
TOGGLE_RELEVANCE
UNDO
}

This file was deleted.

12 changes: 6 additions & 6 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
contextMenu.getItems().add(new SeparatorMenuItem());

if (Globals.prefs.getBoolean(JabRefPreferences.SPECIALFIELDSENABLED)) {
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, panel));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, panel, dialogService, stateManager));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, panel, dialogService, stateManager));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, panel, dialogService, stateManager));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, panel, dialogService, stateManager));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, panel, dialogService, stateManager));
contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, panel, dialogService, stateManager));
}

contextMenu.getItems().add(new SeparatorMenuItem());
Expand Down
41 changes: 25 additions & 16 deletions src/main/java/org/jabref/gui/specialfields/SpecialFieldAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.util.Objects;

import org.jabref.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.actions.BaseAction;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.logic.l10n.Localization;
Expand All @@ -17,37 +20,43 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SpecialFieldAction implements BaseAction {
public class SpecialFieldAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(SpecialFieldAction.class);
private final JabRefFrame frame;
private final BasePanel panel;
private final SpecialField specialField;
private final String value;
private final boolean nullFieldIfValueIsTheSame;

private final String undoText;

private final DialogService dialogService;
private final StateManager stateManager;

/**
* @param nullFieldIfValueIsTheSame - false also causes that doneTextPattern has two place holders %0 for the value and %1 for the sum of entries
*/
public SpecialFieldAction(
JabRefFrame frame,
BasePanel panel,
SpecialField specialField,
String value,
boolean nullFieldIfValueIsTheSame,
String undoText) {
this.frame = frame;
String undoText,
DialogService dialogService,
StateManager stateManager) {
this.panel = panel;
this.specialField = specialField;
this.value = value;
this.nullFieldIfValueIsTheSame = nullFieldIfValueIsTheSame;
this.undoText = undoText;
this.dialogService = dialogService;
this.stateManager = stateManager;

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

@Override
public void action() {
public void execute() {
try {
List<BibEntry> bes = frame.getCurrentBasePanel().getSelectedEntries();
List<BibEntry> bes = stateManager.getSelectedEntries();
if ((bes == null) || bes.isEmpty()) {
return;
}
Expand All @@ -61,16 +70,16 @@ public void action() {
}
ce.end();
if (ce.hasEdits()) {
frame.getCurrentBasePanel().getUndoManager().addEdit(ce);
frame.getCurrentBasePanel().markBaseChanged();
frame.getCurrentBasePanel().updateEntryEditorIfShowing();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
panel.updateEntryEditorIfShowing();
String outText;
if (nullFieldIfValueIsTheSame || value == null) {
outText = getTextDone(specialField, Integer.toString(bes.size()));
} else {
outText = getTextDone(specialField, value, Integer.toString(bes.size()));
}
frame.getDialogService().notify(outText);
dialogService.notify(outText);
} else {
// if user does not change anything with his action, we do not do anything either
// even no output message
Expand All @@ -83,7 +92,7 @@ public void action() {
private String getTextDone(SpecialField field, String... params) {
Objects.requireNonNull(params);

SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, frame.getCurrentBasePanel().getUndoManager());
SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, panel.getUndoManager());

if (field.isSingleValueField() && (params.length == 1) && (params[0] != null)) {
// Single value fields can be toggled only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,34 @@
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.OldCommandWrapper;
import org.jabref.gui.actions.OldCommandWrapperForActiveDatabase;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.SpecialFieldValue;

import de.saxsys.mvvmfx.utils.commands.Command;

public class SpecialFieldMenuItemFactory {
public static MenuItem getSpecialFieldSingleItem(SpecialField field, ActionFactory factory, BasePanel panel) {
public static MenuItem getSpecialFieldSingleItem(SpecialField field, ActionFactory factory, BasePanel panel, DialogService dialogService, StateManager stateManager) {
SpecialFieldValueViewModel specialField = new SpecialFieldValueViewModel(field.getValues().get(0));
return factory.createMenuItem(specialField.getAction(), new OldCommandWrapper(specialField.getCommand(), panel));
return factory.createMenuItem(specialField.getAction(),
new SpecialFieldViewModel(field, Globals.undoManager).getSpecialFieldAction(field.getValues().get(0), panel, dialogService, stateManager));
}

public static MenuItem getSpecialFieldSingleItemForActiveDatabase(SpecialField field, ActionFactory factory) {
SpecialFieldValueViewModel specialField = new SpecialFieldValueViewModel(field.getValues().get(0));
return factory.createMenuItem(specialField.getAction(), new OldCommandWrapperForActiveDatabase(specialField.getCommand()));
}

public static Menu createSpecialFieldMenu(SpecialField field, ActionFactory factory, BasePanel panel) {
return createSpecialFieldMenu(field, factory, panel.getUndoManager(), specialField -> new OldCommandWrapper(specialField.getCommand(), panel));
}

public static Menu createSpecialFieldMenuForActiveDatabase(SpecialField field, ActionFactory factory, UndoManager undoManager) {
return createSpecialFieldMenu(field, factory, undoManager, specialField -> new OldCommandWrapperForActiveDatabase(specialField.getCommand()));
public static Menu createSpecialFieldMenu(SpecialField field, ActionFactory factory, BasePanel panel, DialogService dialogService, StateManager stateManager) {
return createSpecialFieldMenu(field, factory, Globals.undoManager, specialField ->
new SpecialFieldViewModel(field, Globals.undoManager).getSpecialFieldAction(specialField.getValue(), panel, dialogService, stateManager));
}

public static Menu createSpecialFieldMenu(SpecialField field, ActionFactory factory, UndoManager undoManager, Function<SpecialFieldValueViewModel, Command> commandFactory) {
SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, undoManager);
Menu menu = factory.createMenu(viewModel.getAction());
for (SpecialFieldValue val : field.getValues()) {
SpecialFieldValueViewModel specialField = new SpecialFieldValueViewModel(val);
menu.getItems().add(factory.createMenuItem(specialField.getAction(), commandFactory.apply(specialField)));
for (SpecialFieldValue Value : field.getValues()) {
SpecialFieldValueViewModel valueViewModel = new SpecialFieldValueViewModel(Value);
menu.getItems().add(factory.createMenuItem(valueViewModel.getAction(), commandFactory.apply(valueViewModel)));
}
return menu;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Optional;

import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.Actions;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -71,45 +70,6 @@ public String getToolTipText() {
}
}

public Actions getCommand() {
switch (value) {
case PRINTED:
return Actions.TOGGLE_PRINTED;
case CLEAR_PRIORITY:
return Actions.CLEAR_PRIORITY;
case PRIORITY_HIGH:
return Actions.SET_PRIORITY_1;
case PRIORITY_MEDIUM:
return Actions.SET_PRIORITY_2;
case PRIORITY_LOW:
return Actions.SET_PRIORITY_3;
case QUALITY_ASSURED:
return Actions.TOGGLE_QUALITY_ASSURED;
case CLEAR_RANK:
return Actions.CLEAR_RANK;
case RANK_1:
return Actions.SET_RANK_1;
case RANK_2:
return Actions.SET_RANK_2;
case RANK_3:
return Actions.SET_RANK_3;
case RANK_4:
return Actions.SET_RANK_4;
case RANK_5:
return Actions.SET_RANK_5;
case CLEAR_READ_STATUS:
return Actions.CLEAR_READ_STATUS;
case READ:
return Actions.SET_READ_STATUS_TO_READ;
case SKIMMED:
return Actions.SET_READ_STATUS_TO_SKIMMED;
case RELEVANT:
return Actions.TOGGLE_RELEVANCE;
default:
throw new IllegalArgumentException("There is no action name for special field value " + value);
}
}

public Action getAction() {
switch (value) {
case PRINTED:
Expand Down Expand Up @@ -145,7 +105,7 @@ public Action getAction() {
case RELEVANT:
return StandardActions.RELEVANT;
default:
throw new IllegalArgumentException("There is no tooltip localization for special field value " + value);
throw new IllegalArgumentException("There is no action name for special field value " + value);
}
}
}
Loading

0 comments on commit a5bf6b7

Please sign in to comment.