Skip to content

Commit

Permalink
Merge pull request #8166 from JabRef/guiPreferences
Browse files Browse the repository at this point in the history
Observable preferences F (GuiPreferences, Proxy and Remote)
  • Loading branch information
Siedlerchr authored Oct 22, 2021
2 parents 6cd1a52 + 1b7e583 commit ae82589
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 197 deletions.
8 changes: 3 additions & 5 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ private void tearDownJabRef(List<String> filenames) {
Path focusedDatabase = getCurrentLibraryTab().getBibDatabaseContext()
.getDatabasePath()
.orElse(null);
prefs.storeGuiPreferences(prefs.getGuiPreferences()
.withLastFilesOpened(filenames)
.withLastFocusedFile(focusedDatabase));
prefs.getGuiPreferences().setLastFilesOpened(filenames);
prefs.getGuiPreferences().setLastFocusedFile(focusedDatabase);
}
}

Expand Down Expand Up @@ -462,8 +461,7 @@ private void setDividerPosition() {
splitPane.setDividerPositions(prefs.getGuiPreferences().getSidePaneWidth());
if (!splitPane.getDividers().isEmpty()) {
EasyBind.subscribe(splitPane.getDividers().get(0).positionProperty(),
position -> prefs.storeGuiPreferences(prefs.getGuiPreferences()
.withSidePaneWidth(position.doubleValue())));
position -> prefs.getGuiPreferences().setSidePaneWidth(position.doubleValue()));
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,11 @@ private void openDatabases() {

private void saveWindowState(Stage mainStage) {
GuiPreferences preferences = preferencesService.getGuiPreferences();
preferencesService.storeGuiPreferences(new GuiPreferences(
mainStage.getX(),
mainStage.getY(),
mainStage.getWidth(),
mainStage.getHeight(),
mainStage.isMaximized(),
preferences.shouldOpenLastEdited(),
preferences.getLastFilesOpened(),
preferences.getLastFocusedFile(),
preferences.getSidePaneWidth()));
preferences.setPositionX(mainStage.getX());
preferences.setPositionY(mainStage.getY());
preferences.setSizeX(mainStage.getWidth());
preferences.setSizeY(mainStage.getHeight());
preferences.setWindowMaximised(mainStage.isMaximized());
debugLogWindowState(mainStage);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private static void applyPreferences(PreferencesService preferences) {

private static void configureProxy(ProxyPreferences proxyPreferences) {
ProxyRegisterer.register(proxyPreferences);
if (proxyPreferences.isUseProxy() && proxyPreferences.isUseAuthentication()) {
if (proxyPreferences.shouldUseProxy() && proxyPreferences.shouldUseAuthentication()) {
Authenticator.setDefault(new ProxyAuthenticator());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,25 @@ public class NetworkTabViewModel implements PreferenceTabViewModel {

private final DialogService dialogService;
private final PreferencesService preferences;
private final RemotePreferences initialRemotePreferences;
private final ProxyPreferences initialProxyPreferences;
private final RemotePreferences remotePreferences;
private final ProxyPreferences proxyPreferences;
private final ProxyPreferences backupProxyPreferences;

private final List<String> restartWarning = new ArrayList<>();

public NetworkTabViewModel(DialogService dialogService, PreferencesService preferences) {
this.dialogService = dialogService;
this.preferences = preferences;
this.initialRemotePreferences = preferences.getRemotePreferences();
this.initialProxyPreferences = preferences.getProxyPreferences();
this.remotePreferences = preferences.getRemotePreferences();
this.proxyPreferences = preferences.getProxyPreferences();

backupProxyPreferences = new ProxyPreferences(
proxyPreferences.shouldUseProxy(),
proxyPreferences.getHostname(),
proxyPreferences.getPort(),
proxyPreferences.shouldUseAuthentication(),
proxyPreferences.getUsername(),
proxyPreferences.getPassword());

remotePortValidator = new FunctionBasedValidator<>(
remotePortProperty,
Expand Down Expand Up @@ -108,65 +117,66 @@ public NetworkTabViewModel(DialogService dialogService, PreferencesService prefe
}

public void setValues() {
remoteServerProperty.setValue(initialRemotePreferences.useRemoteServer());
remotePortProperty.setValue(String.valueOf(initialRemotePreferences.getPort()));
remoteServerProperty.setValue(remotePreferences.useRemoteServer());
remotePortProperty.setValue(String.valueOf(remotePreferences.getPort()));

setProxyValues();
}

private void setProxyValues() {
proxyUseProperty.setValue(initialProxyPreferences.isUseProxy());
proxyHostnameProperty.setValue(initialProxyPreferences.getHostname());
proxyPortProperty.setValue(initialProxyPreferences.getPort());
proxyUseAuthenticationProperty.setValue(initialProxyPreferences.isUseAuthentication());
proxyUsernameProperty.setValue(initialProxyPreferences.getUsername());
proxyPasswordProperty.setValue(initialProxyPreferences.getPassword());
proxyUseProperty.setValue(proxyPreferences.shouldUseProxy());
proxyHostnameProperty.setValue(proxyPreferences.getHostname());
proxyPortProperty.setValue(proxyPreferences.getPort());
proxyUseAuthenticationProperty.setValue(proxyPreferences.shouldUseAuthentication());
proxyUsernameProperty.setValue(proxyPreferences.getUsername());
proxyPasswordProperty.setValue(proxyPreferences.getPassword());
}

public void storeSettings() {
storeRemoteSettings();
storeProxySettings();

storeProxySettings(new ProxyPreferences(
proxyUseProperty.getValue(),
proxyHostnameProperty.getValue().trim(),
proxyPortProperty.getValue().trim(),
proxyUseAuthenticationProperty.getValue(),
proxyUsernameProperty.getValue().trim(),
proxyPasswordProperty.getValue()
));
}

private void storeRemoteSettings() {
RemotePreferences newRemotePreferences = new RemotePreferences(
initialRemotePreferences.getPort(),
remotePreferences.getPort(),
remoteServerProperty.getValue()
);

getPortAsInt(remotePortProperty.getValue()).ifPresent(newPort -> {
if (initialRemotePreferences.isDifferentPort(newPort)) {
newRemotePreferences.setPort(newPort);

if (newRemotePreferences.useRemoteServer()) {
restartWarning.add(Localization.lang("Remote server port") + ": " + newPort);
}
if (remotePreferences.isDifferentPort(newPort)) {
remotePreferences.setPort(newPort);
}
});

if (newRemotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), initialRemotePreferences.getPort(), preferences);
if (remoteServerProperty.getValue()) {
remotePreferences.setUseRemoteServer(true);
Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort(), preferences);
} else {
remotePreferences.setUseRemoteServer(false);
Globals.REMOTE_LISTENER.stop();
}

preferences.storeRemotePreferences(newRemotePreferences);
}

private void storeProxySettings() {
ProxyPreferences newProxyPreferences = new ProxyPreferences(
proxyUseProperty.getValue(),
proxyHostnameProperty.getValue().trim(),
proxyPortProperty.getValue().trim(),
proxyUseAuthenticationProperty.getValue(),
proxyUsernameProperty.getValue().trim(),
proxyPasswordProperty.getValue()
);

if (!newProxyPreferences.equals(initialProxyPreferences)) {
private void storeProxySettings(ProxyPreferences newProxyPreferences) {
if (!newProxyPreferences.equals(proxyPreferences)) {
ProxyRegisterer.register(newProxyPreferences);
}
preferences.storeProxyPreferences(newProxyPreferences);

proxyPreferences.setUseProxy(newProxyPreferences.shouldUseProxy());
proxyPreferences.setHostname(newProxyPreferences.getHostname());
proxyPreferences.setPort(newProxyPreferences.getPort());
proxyPreferences.setUseAuthentication(newProxyPreferences.shouldUseAuthentication());
proxyPreferences.setUsername(newProxyPreferences.getUsername());
proxyPreferences.setPassword(newProxyPreferences.getPassword());
}

private Optional<Integer> getPortAsInt(String value) {
Expand Down Expand Up @@ -237,7 +247,14 @@ public void checkConnection() {

// Workaround for testing, since the URLDownload uses stored proxy settings, see
// preferences.storeProxyPreferences(...) below.
storeProxySettings();
storeProxySettings(new ProxyPreferences(
proxyUseProperty.getValue(),
proxyHostnameProperty.getValue().trim(),
proxyPortProperty.getValue().trim(),
proxyUseAuthenticationProperty.getValue(),
proxyUsernameProperty.getValue().trim(),
proxyPasswordProperty.getValue()
));

URLDownload urlDownload;
try {
Expand All @@ -253,7 +270,7 @@ public void checkConnection() {
dialogService.showErrorDialogAndWait(dialogTitle, connectionFailedText);
}

preferences.storeProxyPreferences(initialProxyPreferences);
storeProxySettings(backupProxyPreferences);
}

@Override
Expand Down
Loading

0 comments on commit ae82589

Please sign in to comment.