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
11 changes: 6 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,8 @@ private Node createToolbar() {
leftSide.setMinWidth(100);
leftSide.prefWidthProperty().bind(sidePane.widthProperty());
leftSide.maxWidthProperty().bind(sidePane.widthProperty());
PushToApplicationButton pushToExternal = new PushToApplicationButton(this, pushApplications.getApplications());

PushToApplicationAction pushToApplicationAction = new PushToApplicationAction(this, Globals.stateManager);
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 +595,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(pushToApplicationAction.getActionInformation(), pushToApplicationAction),
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 +823,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction())
);

PushToApplicationButton pushToExternal = new PushToApplicationButton(this, pushApplications.getApplications());
final PushToApplicationAction pushToApplicationAction = new PushToApplicationAction(this, Globals.stateManager);
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 +840,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(pushToApplicationAction.getActionInformation(), pushToApplicationAction),

factory.createSubMenu(StandardActions.ABBREVIATE,
factory.createMenuItem(StandardActions.ABBREVIATE_ISO, new OldDatabaseCommandWrapper(Actions.ABBREVIATE_ISO, this, Globals.stateManager)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

/**
* A command that is only executable if a database is open.
* Deprecated use instead
* @see org.jabref.gui.actions.SimpleCommand
*/
@Deprecated
public class OldDatabaseCommandWrapper extends CommandBase {

private static final Logger LOGGER = LoggerFactory.getLogger(OldDatabaseCommandWrapper.class);
Expand Down
68 changes: 54 additions & 14 deletions src/main/java/org/jabref/gui/push/PushToApplicationAction.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,79 @@
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.StateManager;
import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.SimpleCommand;
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;

import org.fxmisc.easybind.EasyBind;

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

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

public PushToApplicationAction(JabRefFrame frame, PushToApplication operation) {
public PushToApplicationAction(final JabRefFrame frame, final StateManager stateManager) {
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());
this.executable.bind(EasyBind.map(stateManager.activeDatabaseProperty(), Optional::isPresent));
}

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);
}

public Action getActionInformation() {
return new Action() {
@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 execute() {
panel = frame.getCurrentBasePanel();

// Check if a BasePanel exists:
Expand All @@ -44,7 +84,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 +94,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.