Skip to content

Commit

Permalink
Extracted Globals.prefs.get and .put out of push package
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Nov 29, 2020
1 parent ddfb805 commit bf36959
Show file tree
Hide file tree
Showing 20 changed files with 281 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void initialize() {
this.viewModel = new ExternalTabViewModel(dialogService, preferences, pushToApplicationsManager);

new ViewModelListCellFactory<PushToApplication>()
.withText(PushToApplication::getApplicationName)
.withText(PushToApplication::getDisplayName)
.withIcon(PushToApplication::getIcon)
.install(pushToApplicationCombo);

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jabref/gui/preferences/ExternalTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import org.jabref.gui.push.PushToApplication;
import org.jabref.gui.push.PushToApplicationSettings;
import org.jabref.gui.push.PushToApplicationsManager;
import org.jabref.gui.push.PushToEmacs;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.ExternalApplicationsPreferences;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.PushToApplicationPreferences;

import de.saxsys.mvvmfx.utils.validation.CompositeValidator;
import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
Expand Down Expand Up @@ -53,10 +55,15 @@ public class ExternalTabViewModel implements PreferenceTabViewModel {

private final FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().build();

private final ExternalApplicationsPreferences initialPreferences;
private final ObjectProperty<PushToApplicationPreferences> workingPushToApplicationPreferences;

public ExternalTabViewModel(DialogService dialogService, PreferencesService preferencesService, PushToApplicationsManager pushToApplicationsManager) {
this.dialogService = dialogService;
this.preferences = preferencesService;
this.pushToApplicationsManager = pushToApplicationsManager;
this.initialPreferences = preferences.getExternalApplicationsPreferences();
this.workingPushToApplicationPreferences = new SimpleObjectProperty<>(preferencesService.getPushToApplicationPreferences());

terminalCommandValidator = new FunctionBasedValidator<>(
customTerminalCommandProperty,
Expand All @@ -76,16 +83,14 @@ public ExternalTabViewModel(DialogService dialogService, PreferencesService pref
}

public void setValues() {
ExternalApplicationsPreferences initialPreferences =
preferences.getExternalApplicationsPreferences();

eMailReferenceSubjectProperty.setValue(initialPreferences.getEmailSubject());
autoOpenAttachedFoldersProperty.setValue(initialPreferences.shouldAutoOpenEmailAttachmentsFolder());

pushToApplicationsListProperty.setValue(
FXCollections.observableArrayList(pushToApplicationsManager.getApplications()));
selectedPushToApplicationProperty.setValue(
pushToApplicationsManager.getApplicationByName(initialPreferences.getPushToApplicationName()));
pushToApplicationsManager.getApplicationByName(initialPreferences.getPushToApplicationName())
.orElse(new PushToEmacs(dialogService, preferences)));

citeCommandProperty.setValue(initialPreferences.getCiteCommand());
useCustomTerminalProperty.setValue(initialPreferences.useCustomTerminal());
Expand All @@ -98,13 +103,15 @@ public void storeSettings() {
preferences.storeExternalApplicationsPreferences(new ExternalApplicationsPreferences(
eMailReferenceSubjectProperty.getValue(),
autoOpenAttachedFoldersProperty.getValue(),
selectedPushToApplicationProperty.getValue().getApplicationName(),
selectedPushToApplicationProperty.getValue().getDisplayName(),
citeCommandProperty.getValue(),
useCustomTerminalProperty.getValue(),
customTerminalCommandProperty.getValue(),
useCustomFileBrowserProperty.getValue(),
customFileBrowserCommandProperty.getValue()));

preferences.storePushToApplicationPreferences(workingPushToApplicationPreferences.get());

pushToApplicationsManager.updateApplicationAction(selectedPushToApplicationProperty.getValue());
}

Expand Down Expand Up @@ -143,7 +150,7 @@ public List<String> getRestartWarnings() {

public void pushToApplicationSettings() {
PushToApplication selectedApplication = selectedPushToApplicationProperty.getValue();
PushToApplicationSettings settings = pushToApplicationsManager.getSettings(selectedApplication);
PushToApplicationSettings settings = selectedApplication.getSettings(selectedApplication, workingPushToApplicationPreferences);

DialogPane dialogPane = new DialogPane();
dialogPane.setContent(settings.getSettingsPane());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.push.PushToApplicationsManager;
import org.jabref.gui.push.PushToEmacs;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.JabRefException;
import org.jabref.logic.exporter.ExporterFactory;
Expand Down Expand Up @@ -148,7 +149,8 @@ private void updateAfterPreferenceChanges() {

ExternalApplicationsPreferences externalApplicationsPreferences = preferences.getExternalApplicationsPreferences();
PushToApplicationsManager manager = frame.getPushToApplicationsManager();
manager.updateApplicationAction(manager.getApplicationByName(externalApplicationsPreferences.getPushToApplicationName()));
manager.updateApplicationAction(manager.getApplicationByName(externalApplicationsPreferences.getPushToApplicationName())
.orElse(new PushToEmacs(dialogService, preferences)));

frame.getLibraryTabs().forEach(panel -> panel.getMainTable().getTableModel().refresh());
}
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/org/jabref/gui/push/AbstractPushToApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import java.io.IOException;
import java.util.List;

import javafx.beans.property.ObjectProperty;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.PushToApplicationPreferences;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,22 +28,18 @@ public abstract class AbstractPushToApplication implements PushToApplication {
protected boolean notDefined; // Set to true if the corresponding path is not defined in the preferences

protected String commandPath;
protected String commandPathPreferenceKey;

protected DialogService dialogService;
protected final DialogService dialogService;
protected final PreferencesService preferencesService;

public AbstractPushToApplication(DialogService dialogService) {
public AbstractPushToApplication(DialogService dialogService, PreferencesService preferencesService) {
this.dialogService = dialogService;
}

@Override
public String getName() {
return Localization.lang("Push entries to external application (%0)", getApplicationName());
this.preferencesService = preferencesService;
}

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

@Override
Expand All @@ -50,8 +48,7 @@ public void pushEntries(BibDatabaseContext database, List<BibEntry> entries, Str
couldNotCall = false;
notDefined = false;

initParameters();
commandPath = Globals.prefs.get(commandPathPreferenceKey);
commandPath = preferencesService.getPushToApplicationPreferences().getPushToApplicationCommandPaths().get(this.getDisplayName());

// Check if a path to the command has been specified
if ((commandPath == null) || commandPath.trim().isEmpty()) {
Expand Down Expand Up @@ -92,17 +89,17 @@ public void operationCompleted() {
if (notDefined) {
dialogService.showErrorDialogAndWait(
Localization.lang("Error pushing entries"),
Localization.lang("Path to %0 not defined", getApplicationName()) + ".");
Localization.lang("Path to %0 not defined", getDisplayName()) + ".");
} else if (couldNotCall) {
dialogService.showErrorDialogAndWait(
Localization.lang("Error pushing entries"),
Localization.lang("Could not call executable") + " '" + commandPath + "'.");
} else if (couldNotConnect) {
dialogService.showErrorDialogAndWait(
Localization.lang("Error pushing entries"),
Localization.lang("Could not connect to %0", getApplicationName()) + ".");
Localization.lang("Could not connect to %0", getDisplayName()) + ".");
} else {
dialogService.notify(Localization.lang("Pushed citations to %0", getApplicationName()) + ".");
dialogService.notify(Localization.lang("Pushed citations to %0", getDisplayName()) + ".");
}
}

Expand Down Expand Up @@ -131,13 +128,11 @@ protected String getCommandName() {
return null;
}

/**
* Function to initialize parameters. Currently it is expected that commandPathPreferenceKey is set to the path of
* the application.
*/
protected abstract void initParameters();

protected String getCiteCommand() {
return Globals.prefs.get(JabRefPreferences.CITE_COMMAND);
return preferencesService.getExternalApplicationsPreferences().getCiteCommand();
}

public PushToApplicationSettings getSettings(PushToApplication application, ObjectProperty<PushToApplicationPreferences> preferences) {
return new PushToApplicationSettings(application, dialogService, preferencesService, preferences);
}
}
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/gui/push/PushToApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import java.util.List;

import javafx.beans.property.ObjectProperty;

import org.jabref.gui.icon.JabRefIcon;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PushToApplicationPreferences;

/**
* Class that defines interaction with an external application in the form of "pushing" selected entries to it.
*/
public interface PushToApplication {

String getName();

String getApplicationName();
String getDisplayName();

String getTooltip();

JabRefIcon getIcon();


/**
* The actual operation. This method will not be called on the event dispatch thread, so it should not do GUI
* operations without utilizing invokeLater().
Expand All @@ -39,4 +39,5 @@ public interface PushToApplication {
*/
boolean requiresCitationKeys();

PushToApplicationSettings getSettings(PushToApplication application, ObjectProperty<PushToApplicationPreferences> preferences);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Optional<KeyBinding> getKeyBinding() {

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

@Override
Expand Down Expand Up @@ -95,7 +95,7 @@ public void execute() {
for (BibEntry entry : stateManager.getSelectedEntries()) {
if (StringUtil.isBlank(entry.getCitationKey())) {
dialogService.showErrorDialogAndWait(
application.getApplicationName(),
application.getDisplayName(),
Localization.lang("This operation requires all selected entries to have citation keys defined."));

return;
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/org/jabref/gui/push/PushToApplicationSettings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jabref.gui.push;

import java.util.Map;

import javafx.beans.property.ObjectProperty;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
Expand All @@ -9,40 +12,43 @@
import javafx.scene.layout.Priority;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.PushToApplicationPreferences;

public class PushToApplicationSettings {

protected final Label commandLabel;
protected final TextField path;
protected final GridPane settingsPane;
private final AbstractPushToApplication application;
private final DialogService dialogService;
private final Button browse;
protected final ObjectProperty<PushToApplicationPreferences> preferences;
protected final AbstractPushToApplication application;

public PushToApplicationSettings(PushToApplication application, DialogService dialogService) {
public PushToApplicationSettings(PushToApplication application,
DialogService dialogService,
PreferencesService preferencesService,
ObjectProperty<PushToApplicationPreferences> preferences) {
this.application = (AbstractPushToApplication) application;
this.dialogService = dialogService;
this.preferences = preferences;

settingsPane = new GridPane();
commandLabel = new Label();
path = new TextField();
Button browse = new Button();

settingsPane.setHgap(4.0);
settingsPane.setVgap(4.0);

commandLabel = new Label();
path = new TextField();
browse = new Button();
browse.setTooltip(new Tooltip(Localization.lang("Browse")));
browse.setGraphic(IconTheme.JabRefIcons.OPEN.getGraphicNode());
browse.getStyleClass().addAll("icon-button", "narrow");
browse.setPrefHeight(20.0);
browse.setPrefWidth(20.0);

this.application.initParameters();

// In case the application name and the actual command is not the same, add the command in brackets
StringBuilder commandLine = new StringBuilder(Localization.lang("Path to %0", application.getApplicationName()));
StringBuilder commandLine = new StringBuilder(Localization.lang("Path to %0", application.getDisplayName()));
if (this.application.getCommandName() == null) {
commandLine.append(':');
} else {
Expand All @@ -51,11 +57,11 @@ public PushToApplicationSettings(PushToApplication application, DialogService di
commandLabel.setText(commandLine.toString());
settingsPane.add(commandLabel, 0, 0);

path.setText(Globals.prefs.get(this.application.commandPathPreferenceKey));
path.setText(preferences.get().getPushToApplicationCommandPaths().get(this.application.getDisplayName()));
settingsPane.add(path, 1, 0);

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(Globals.prefs.getWorkingDir()).build();
.withInitialDirectory(preferencesService.getWorkingDir()).build();
browse.setOnAction(e -> dialogService.showFileOpenDialog(fileDialogConfiguration)
.ifPresent(f -> path.setText(f.toAbsolutePath().toString())));
settingsPane.add(browse, 2, 0);
Expand All @@ -74,10 +80,12 @@ public PushToApplicationSettings(PushToApplication application, DialogService di
* state of the widgets in the settings panel to Globals.prefs.
*/
public void storeSettings() {
Globals.prefs.put(application.commandPathPreferenceKey, path.getText());
Map<String, String> commandPaths = preferences.get().getPushToApplicationCommandPaths();
commandPaths.put(application.getDisplayName(), path.getText());
preferences.setValue(preferences.get().withPushToApplicationCommandPaths(commandPaths));
}

public GridPane getSettingsPane() {
return settingsPane;
return this.settingsPane;
}
}
Loading

0 comments on commit bf36959

Please sign in to comment.