Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4795 disable menu item if database not connected #4828

Merged
merged 13 commits into from
Apr 9, 2019
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
import org.jabref.gui.push.PushToApplicationAction;
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
import org.jabref.gui.specialfields.SpecialFieldValueViewModel;
import org.jabref.gui.specialfields.SpecialFieldViewModel;
Expand Down Expand Up @@ -389,6 +390,7 @@ private void setupActions() {
actions.put(Actions.ABBREVIATE_MEDLINE, new AbbreviateAction(this, false));
actions.put(Actions.UNABBREVIATE, new UnabbreviateAction(this));

actions.put(Actions.PUSH_TO_APPLICATION, new PushToApplicationAction(frame));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion, but that is actually the old way of defining actions. It is now preferred that actions derive from SimpleCommand, which makes the OldDatabaseCommandWrapper obsolete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change it. What about putting deprecated annotation on OldDatabaseCommandWrapper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

actions.put(Actions.DOWNLOAD_FULL_TEXT, new FindFullTextAction(this)::execute);
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
import org.jabref.gui.metadata.BibtexStringEditorAction;
import org.jabref.gui.metadata.PreambleEditor;
import org.jabref.gui.protectedterms.ManageProtectedTermsAction;
import org.jabref.gui.push.PushToApplicationButton;
import org.jabref.gui.push.PushToApplicationAction;
import org.jabref.gui.push.PushToApplications;
import org.jabref.gui.search.GlobalSearchBar;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
Expand Down Expand Up @@ -583,7 +583,7 @@ private Node createToolbar() {
leftSide.setMinWidth(100);
leftSide.prefWidthProperty().bind(sidePane.widthProperty());
leftSide.maxWidthProperty().bind(sidePane.widthProperty());
PushToApplicationButton pushToExternal = new PushToApplicationButton(this, pushApplications.getApplications());

HBox rightSide = new HBox(
factory.createIconButton(StandardActions.NEW_ARTICLE, new NewEntryAction(this, BiblatexEntryTypes.ARTICLE, dialogService, Globals.prefs)),
factory.createIconButton(StandardActions.DELETE_ENTRY, new OldDatabaseCommandWrapper(Actions.DELETE, this, Globals.stateManager)),
Expand All @@ -594,7 +594,7 @@ private Node createToolbar() {
factory.createIconButton(StandardActions.COPY, new OldDatabaseCommandWrapper(Actions.COPY, this, Globals.stateManager)),
factory.createIconButton(StandardActions.PASTE, new OldDatabaseCommandWrapper(Actions.PASTE, this, Globals.stateManager)),
new Separator(Orientation.VERTICAL),
factory.createIconButton(pushToExternal.getMenuAction(), pushToExternal),
factory.createIconButton(new PushToApplicationAction(this), new OldDatabaseCommandWrapper(Actions.PUSH_TO_APPLICATION, this, Globals.stateManager)),
factory.createIconButton(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, Globals.stateManager)),
factory.createIconButton(StandardActions.CLEANUP_ENTRIES, new OldDatabaseCommandWrapper(Actions.CLEANUP, this, Globals.stateManager)),
new Separator(Orientation.VERTICAL),
Expand Down Expand Up @@ -822,7 +822,6 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction())
);

PushToApplicationButton pushToExternal = new PushToApplicationButton(this, pushApplications.getApplications());
tools.getItems().addAll(
factory.createMenuItem(StandardActions.NEW_SUB_LIBRARY_FROM_AUX, new NewSubLibraryAction(this)),
factory.createMenuItem(StandardActions.FIND_UNLINKED_FILES, new FindUnlinkedFilesAction(this)),
Expand All @@ -839,7 +838,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, Globals.stateManager)),
factory.createMenuItem(StandardActions.REPLACE_ALL, new OldDatabaseCommandWrapper(Actions.REPLACE_ALL, this, Globals.stateManager)),
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new OldDatabaseCommandWrapper(Actions.SEND_AS_EMAIL, this, Globals.stateManager)),
factory.createMenuItem(pushToExternal.getMenuAction(), pushToExternal),
factory.createMenuItem(new PushToApplicationAction(this), new OldDatabaseCommandWrapper(Actions.PUSH_TO_APPLICATION, this, Globals.stateManager)),

factory.createSubMenu(StandardActions.ABBREVIATE,
factory.createMenuItem(StandardActions.ABBREVIATE_ISO, new OldDatabaseCommandWrapper(Actions.ABBREVIATE_ISO, this, Globals.stateManager)),
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ public enum Actions {
CLEAR_READ_STATUS,
SET_READ_STATUS_TO_READ,
SET_READ_STATUS_TO_SKIMMED,
TOGGLE_RELEVANCE
TOGGLE_RELEVANCE,
PUSH_TO_APPLICATION
}
56 changes: 44 additions & 12 deletions src/main/java/org/jabref/gui/push/PushToApplicationAction.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
package org.jabref.gui.push;

import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Optional;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.SwingUtilities;

import org.jabref.Globals;
import org.jabref.JabRefExecutorService;
import org.jabref.gui.BasePanel;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.BaseAction;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

/**
* An Action class representing the process of invoking a PushToApplication operation.
*/
class PushToApplicationAction extends AbstractAction implements Runnable {
public class PushToApplicationAction implements Runnable, BaseAction, Action {

private final PushToApplication operation;
private final JabRefFrame frame;
private BasePanel panel;
private List<BibEntry> entries;

public PushToApplicationAction(JabRefFrame frame, PushToApplication operation) {
public PushToApplicationAction(JabRefFrame frame) {
this.frame = frame;
putValue(Action.SMALL_ICON, operation.getIcon());
putValue(Action.NAME, operation.getName());
putValue(Action.SHORT_DESCRIPTION, operation.getTooltip());
this.operation = operation;
this.operation = getLastUsedApplication(frame.getPushApplications().getApplications());
}

private PushToApplication getLastUsedApplication(List<PushToApplication> pushActions) {
String appSelected = Globals.prefs.get(JabRefPreferences.PUSH_TO_APPLICATION);
for (PushToApplication application : pushActions) {
if (application.getApplicationName().equals(appSelected)) {
return application;
}
}

// Nothing found, pick first
return pushActions.get(0);
}

@Override
public Optional<JabRefIcon> getIcon() {
return Optional.of(operation.getIcon());
}

@Override
public Optional<KeyBinding> getKeyBinding() {
return Optional.of(KeyBinding.PUSH_TO_APPLICATION);
}

@Override
public String getText() {
return Localization.lang("Push entries to external application (%0)", operation.getApplicationName());
}

@Override
public String getDescription() {
return "";
}

@Override
public void actionPerformed(ActionEvent e) {
public void action() {
panel = frame.getCurrentBasePanel();

// Check if a BasePanel exists:
Expand All @@ -44,7 +76,7 @@ public void actionPerformed(ActionEvent e) {
// Check if any entries are selected:
entries = panel.getSelectedEntries();
if (entries.isEmpty()) {
frame.getDialogService().showErrorDialogAndWait((String) getValue(Action.NAME),
frame.getDialogService().showErrorDialogAndWait(operation.getApplicationName(),
Localization.lang("This operation requires one or more entries to be selected."));

return;
Expand All @@ -54,7 +86,7 @@ public void actionPerformed(ActionEvent e) {
if (operation.requiresBibtexKeys()) {
for (BibEntry entry : entries) {
if (!(entry.getCiteKeyOptional().isPresent()) || entry.getCiteKeyOptional().get().trim().isEmpty()) {
frame.getDialogService().showErrorDialogAndWait((String) getValue(Action.NAME),
frame.getDialogService().showErrorDialogAndWait(operation.getApplicationName(),
Localization.lang("This operation requires all selected entries to have BibTeX keys defined."));

return;
Expand Down
90 changes: 0 additions & 90 deletions src/main/java/org/jabref/gui/push/PushToApplicationButton.java

This file was deleted.