Skip to content

Commit

Permalink
Migrate RemoteListenerServerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed May 26, 2024
1 parent efc6767 commit 103a718
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.gui.util.DefaultDirectoryMonitor;
import org.jabref.gui.util.DefaultFileUpdateMonitor;
import org.jabref.logic.remote.server.RemoteListenerServerManager;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.util.DirectoryMonitor;
import org.jabref.model.util.FileUpdateMonitor;
Expand All @@ -20,8 +19,6 @@ public class Globals {
*/
public static final BuildInfo BUILD_INFO = new BuildInfo();

public static final RemoteListenerServerManager REMOTE_LISTENER = new RemoteListenerServerManager();

private static DefaultFileUpdateMonitor fileUpdateMonitor;
private static DefaultDirectoryMonitor directoryMonitor;

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.ProxyRegisterer;
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.server.RemoteListenerServerManager;
import org.jabref.logic.util.WebViewStore;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -61,6 +62,8 @@ public class JabRefGUI extends Application {
private static DialogService dialogService;
private static JabRefFrame mainFrame;

private static RemoteListenerServerManager remoteListenerServerManager;

private boolean correctedWindowPos = false;
private Stage mainStage;

Expand All @@ -77,7 +80,6 @@ public void start(Stage stage) {
this.mainStage = stage;

FallbackExceptionHandler.installExceptionHandler();
startBackgroundTasks();

initialize();

Expand All @@ -94,6 +96,8 @@ public void start(Stage stage) {

openWindow();

startBackgroundTasks();

if (!fileUpdateMonitor.isActive()) {
dialogService.showErrorDialogAndWait(
Localization.lang("Unable to monitor file changes. Please close files " +
Expand All @@ -117,6 +121,9 @@ public void start(Stage stage) {
public void initialize() {
WebViewStore.init();

JabRefGUI.remoteListenerServerManager = new RemoteListenerServerManager();
Injector.setModelOrService(RemoteListenerServerManager.class, remoteListenerServerManager);

JabRefGUI.stateManager = new StateManager();
Injector.setModelOrService(StateManager.class, stateManager);

Expand Down Expand Up @@ -290,8 +297,9 @@ public void startBackgroundTasks() {
// }
RemotePreferences remotePreferences = preferencesService.getRemotePreferences();
if (remotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.openAndStart(
remoteListenerServerManager.openAndStart(
new CLIMessageHandler(
mainFrame,
preferencesService,
Globals.getFileUpdateMonitor(),
Injector.instantiateModelOrService(BibEntryTypesManager.class)),
Expand All @@ -317,8 +325,4 @@ public static void shutdownThreadPools() {
Globals.getDirectoryMonitor().shutdown();
JabRefExecutorService.INSTANCE.shutdownEverything();
}

public static JabRefFrame getMainFrame() {
return mainFrame;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
/**
* Represents the inner frame of the JabRef window
*/
public class JabRefFrame extends BorderPane implements LibraryTabContainer {
public class JabRefFrame extends BorderPane implements LibraryTabContainer, UiMessageHandler {

public static final String FRAME_TITLE = "JabRef";

Expand Down Expand Up @@ -541,6 +541,7 @@ public Stage getMainStage() {
return mainStage;
}

@Override
public void handleUiCommands(List<UiCommand> uiCommands) {
viewModel.handleUiCommands(uiCommands);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/frame/JabRefFrameViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JabRefFrameViewModel {
public class JabRefFrameViewModel implements UiMessageHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefFrameViewModel.class);

private final PreferencesService prefs;
Expand Down Expand Up @@ -136,6 +136,7 @@ public boolean close() {
*
* @param uiCommands to be handled
*/
@Override
public void handleUiCommands(List<UiCommand> uiCommands) {
LOGGER.debug("Handling UI commands {}", uiCommands);
if (uiCommands.isEmpty()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/gui/frame/UiMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jabref.gui.frame;

import java.util.List;

import org.jabref.logic.UiCommand;

public interface UiMessageHandler {

void handleUiCommands(List<UiCommand> uiCommands);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import javafx.scene.control.SpinnerValueFactory;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.frame.UiMessageHandler;
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.gui.remote.CLIMessageHandler;
import org.jabref.gui.telemetry.TelemetryPreferences;
Expand All @@ -34,6 +34,7 @@
import org.jabref.logic.net.ssl.TrustStoreManager;
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.RemoteUtil;
import org.jabref.logic.remote.server.RemoteListenerServerManager;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntryTypesManager;
Expand All @@ -46,6 +47,7 @@
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.WorkspacePreferences;

import com.airhacks.afterburner.injection.Injector;
import de.saxsys.mvvmfx.utils.validation.CompositeValidator;
import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
Expand Down Expand Up @@ -260,23 +262,28 @@ public void storeSettings() {
}
});

// stop in all cases, because the port might have changed
Globals.REMOTE_LISTENER.stop();
UiMessageHandler uiMessageHandler = Injector.instantiateModelOrService(UiMessageHandler.class);
RemoteListenerServerManager remoteListenerServerManager = Injector.instantiateModelOrService(RemoteListenerServerManager.class);
remoteListenerServerManager.stop(); // stop in all cases, because the port might have changed

if (remoteServerProperty.getValue()) {
remotePreferences.setUseRemoteServer(true);
Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(preferences, fileUpdateMonitor, entryTypesManager), remotePreferences.getPort());
remoteListenerServerManager.openAndStart(
new CLIMessageHandler(uiMessageHandler, preferences, fileUpdateMonitor, entryTypesManager),
remotePreferences.getPort());
} else {
remotePreferences.setUseRemoteServer(false);
}
trustStoreManager.flush();

if (remoteServerProperty.getValue()) {
remotePreferences.setUseRemoteServer(true);
Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(preferences, fileUpdateMonitor, entryTypesManager), remotePreferences.getPort());
remoteListenerServerManager.openAndStart(
new CLIMessageHandler(uiMessageHandler, preferences, fileUpdateMonitor, entryTypesManager),
remotePreferences.getPort());
} else {
remotePreferences.setUseRemoteServer(false);
Globals.REMOTE_LISTENER.stop();
remoteListenerServerManager.stop();
}
trustStoreManager.flush();
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/jabref/gui/remote/CLIMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import javafx.application.Platform;

import org.jabref.cli.ArgumentProcessor;
import org.jabref.gui.JabRefGUI;
import org.jabref.gui.frame.UiMessageHandler;
import org.jabref.logic.remote.server.RemoteMessageHandler;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.util.FileUpdateMonitor;
Expand All @@ -21,8 +21,13 @@ public class CLIMessageHandler implements RemoteMessageHandler {
private final PreferencesService preferencesService;
private final FileUpdateMonitor fileUpdateMonitor;
private final BibEntryTypesManager entryTypesManager;
private final UiMessageHandler uiMessageHandler;

public CLIMessageHandler(PreferencesService preferencesService, FileUpdateMonitor fileUpdateMonitor, BibEntryTypesManager entryTypesManager) {
public CLIMessageHandler(UiMessageHandler uiMessageHandler,
PreferencesService preferencesService,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager) {
this.uiMessageHandler = uiMessageHandler;
this.preferencesService = preferencesService;
this.fileUpdateMonitor = fileUpdateMonitor;
this.entryTypesManager = entryTypesManager;
Expand All @@ -39,7 +44,7 @@ public void handleCommandLineArguments(String[] message) {
fileUpdateMonitor,
entryTypesManager);
argumentProcessor.processArguments();
Platform.runLater(() -> JabRefGUI.getMainFrame().handleUiCommands(argumentProcessor.getUiCommands()));
Platform.runLater(() -> uiMessageHandler.handleUiCommands(argumentProcessor.getUiCommands()));
} catch (ParseException e) {
LOGGER.error("Error when parsing CLI args", e);
}
Expand Down

0 comments on commit 103a718

Please sign in to comment.