Skip to content

Commit

Permalink
Extracted many callbacks to JabRefGUI and Globals, extracted Telemetr…
Browse files Browse the repository at this point in the history
…y class (#10360)

* Extracted dialogservice

* Extracted call to JabRefGUI

* Extracted call to JabRefGUI

* Extracted call to JabRefGUI

* Fixed some NPEs

* Extracted Telemetry out of Globals

* Extracted Telemetry out of Globals

* Privatize Telemetry class

* l10n
  • Loading branch information
calixtus authored Sep 11, 2023
1 parent 40b4d77 commit a0e2564
Show file tree
Hide file tree
Showing 99 changed files with 861 additions and 426 deletions.
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/FallbackExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jabref.gui.util.DefaultTaskExecutor;

import com.airhacks.afterburner.injection.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,11 +20,9 @@ public static void installExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable exception) {
LOGGER.error("Uncaught exception occurred in " + thread, exception);

DefaultTaskExecutor.runInJavaFXThread(() ->
JabRefGUI.getMainFrame()
.getDialogService()
.showErrorDialogAndWait("Uncaught exception occurred in " + thread, exception)
);
DefaultTaskExecutor.runInJavaFXThread(() -> {
DialogService dialogService = Injector.instantiateModelOrService(DialogService.class);
dialogService.showErrorDialogAndWait("Uncaught exception occurred in " + thread, exception);
});
}
}
44 changes: 2 additions & 42 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.jabref.gui;

import java.util.Optional;
import java.util.UUID;

import javafx.stage.Screen;

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.remote.CLIMessageHandler;
Expand All @@ -19,14 +14,9 @@
import org.jabref.logic.remote.server.RemoteListenerServerManager;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.base.StandardSystemProperty;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.telemetry.SessionState;
import kong.unirest.Unirest;

/**
Expand Down Expand Up @@ -77,7 +67,6 @@ public class Globals {
private static ThemeManager themeManager;

private static DefaultFileUpdateMonitor fileUpdateMonitor;
private static TelemetryClient telemetryClient;

private Globals() {
}
Expand Down Expand Up @@ -119,39 +108,14 @@ public static synchronized FileUpdateMonitor getFileUpdateMonitor() {
public static void startBackgroundTasks() {
// TODO Currently deactivated due to incompatibilities in XML
/* if (Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
startTelemetryClient();
Telemetry.start(prefs.getTelemetryPreferences());
} */
RemotePreferences remotePreferences = prefs.getRemotePreferences();
if (remotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(prefs, fileUpdateMonitor, entryTypesManager), remotePreferences.getPort());
}
}

private static void stopTelemetryClient() {
getTelemetryClient().ifPresent(client -> {
client.trackSessionState(SessionState.End);
client.flush();
});
}

private static void startTelemetryClient() {
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive();
if (!StringUtil.isNullOrEmpty(Globals.BUILD_INFO.azureInstrumentationKey)) {
telemetryConfiguration.setInstrumentationKey(Globals.BUILD_INFO.azureInstrumentationKey);
}
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry());
telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString());
telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value());
telemetryClient.getContext().getUser().setId(Globals.prefs.getTelemetryPreferences().getUserId());
telemetryClient.getContext().getSession().setId(UUID.randomUUID().toString());
telemetryClient.getContext().getDevice().setOperatingSystem(StandardSystemProperty.OS_NAME.value());
telemetryClient.getContext().getDevice().setOperatingSystemVersion(StandardSystemProperty.OS_VERSION.value());
telemetryClient.getContext().getDevice().setScreenResolution(Screen.getPrimary().getVisualBounds().toString());

telemetryClient.trackSessionState(SessionState.Start);
}

public static void shutdownThreadPools() {
TASK_EXECUTOR.shutdown();
if (fileUpdateMonitor != null) {
Expand All @@ -161,11 +125,7 @@ public static void shutdownThreadPools() {
}

public static void stopBackgroundTasks() {
stopTelemetryClient();
Telemetry.shutdown();
Unirest.shutDown();
}

public static Optional<TelemetryClient> getTelemetryClient() {
return Optional.ofNullable(telemetryClient);
}
}
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,16 @@ public void showLibraryTab(LibraryTab libraryTab) {
}

public void init() {
sidePane = new SidePane(prefs, Globals.journalAbbreviationRepository, taskExecutor, dialogService, stateManager, undoManager);
sidePane = new SidePane(
this,
prefs,
Globals.journalAbbreviationRepository,
taskExecutor,
dialogService,
stateManager,
fileUpdateMonitor,
entryTypesManager,
undoManager);
tabbedPane = new TabPane();
tabbedPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);

Expand Down Expand Up @@ -735,7 +744,8 @@ public void addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
stateManager,
fileUpdateMonitor,
entryTypesManager,
undoManager);
undoManager,
taskExecutor);

addTab(libraryTab, raisePanel);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ private void openDatabases() {
Globals.stateManager,
Globals.entryTypesManager,
fileUpdateMonitor,
mainFrame.getUndoManager());
mainFrame.getUndoManager(),
Globals.TASK_EXECUTOR);
} catch (SQLException |
DatabaseNotSupportedException |
InvalidDBMSConnectionPropertiesException |
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public class LibraryTab extends Tab {

private BackgroundTask<ParserResult> dataLoadingTask;

private final IndexingTaskManager indexingTaskManager = new IndexingTaskManager(Globals.TASK_EXECUTOR);
private final IndexingTaskManager indexingTaskManager;
private final TaskExecutor taskExecutor;

public LibraryTab(BibDatabaseContext bibDatabaseContext,
JabRefFrame frame,
Expand All @@ -130,7 +131,8 @@ public LibraryTab(BibDatabaseContext bibDatabaseContext,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager,
CountingUndoManager undoManager) {
CountingUndoManager undoManager,
TaskExecutor taskExecutor) {
this.frame = Objects.requireNonNull(frame);
this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
this.undoManager = undoManager;
Expand All @@ -139,6 +141,8 @@ public LibraryTab(BibDatabaseContext bibDatabaseContext,
this.stateManager = Objects.requireNonNull(stateManager);
this.fileUpdateMonitor = fileUpdateMonitor;
this.entryTypesManager = entryTypesManager;
this.indexingTaskManager = new IndexingTaskManager(taskExecutor);
this.taskExecutor = taskExecutor;

bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
Expand Down Expand Up @@ -511,7 +515,7 @@ private void createMainTable() {
Globals.getKeyPrefs(),
Globals.getClipboardManager(),
entryTypesManager,
Globals.TASK_EXECUTOR,
taskExecutor,
fileUpdateMonitor);

// Add the listener that binds selection to state manager (TODO: should be replaced by proper JavaFX binding as soon as table is implemented in JavaFX)
Expand Down Expand Up @@ -775,7 +779,7 @@ public void resetChangeMonitor() {
changeMonitor.ifPresent(DatabaseChangeMonitor::unregister);
changeMonitor = Optional.of(new DatabaseChangeMonitor(bibDatabaseContext,
fileUpdateMonitor,
Globals.TASK_EXECUTOR,
taskExecutor,
dialogService,
preferencesService,
databaseNotificationPane));
Expand Down Expand Up @@ -850,7 +854,8 @@ public static LibraryTab createLibraryTab(BackgroundTask<ParserResult> dataLoadi
stateManager,
fileUpdateMonitor,
entryTypesManager,
undoManager);
undoManager,
taskExecutor);

newTab.setDataLoadingTask(dataLoadingTask);
dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted)
Expand All @@ -868,7 +873,8 @@ public static LibraryTab createLibraryTab(BibDatabaseContext databaseContext,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager,
UndoManager undoManager) {
UndoManager undoManager,
TaskExecutor taskExecutor) {
Objects.requireNonNull(databaseContext);

LibraryTab libraryTab = new LibraryTab(
Expand All @@ -879,7 +885,8 @@ public static LibraryTab createLibraryTab(BibDatabaseContext databaseContext,
stateManager,
fileUpdateMonitor,
entryTypesManager,
(CountingUndoManager) undoManager);
(CountingUndoManager) undoManager,
taskExecutor);

return libraryTab;
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/jabref/gui/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ private void createMenu() {
factory.createMenuItem(StandardActions.IMPORT_INTO_NEW_LIBRARY, new ImportCommand(frame, ImportCommand.ImportMethod.AS_NEW, preferencesService, stateManager, fileUpdateMonitor, taskExecutor, dialogService))),

factory.createSubMenu(StandardActions.EXPORT,
factory.createMenuItem(StandardActions.EXPORT_ALL, new ExportCommand(ExportCommand.ExportMethod.EXPORT_ALL, frame, stateManager, dialogService, preferencesService)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED, new ExportCommand(ExportCommand.ExportMethod.EXPORT_SELECTED, frame, stateManager, dialogService, preferencesService)),
factory.createMenuItem(StandardActions.EXPORT_ALL, new ExportCommand(ExportCommand.ExportMethod.EXPORT_ALL, frame, stateManager, dialogService, preferencesService, entryTypesManager, abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED, new ExportCommand(ExportCommand.ExportMethod.EXPORT_SELECTED, frame, stateManager, dialogService, preferencesService, entryTypesManager, abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.SAVE_SELECTED_AS_PLAIN_BIBTEX, new SaveAction(SaveAction.SaveMethod.SAVE_SELECTED, frame, dialogService, preferencesService, stateManager))),

new SeparatorMenuItem(),
Expand Down Expand Up @@ -222,10 +222,10 @@ private void createMenu() {
);

quality.getItems().addAll(
factory.createMenuItem(StandardActions.FIND_DUPLICATES, new DuplicateSearch(frame, dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.FIND_DUPLICATES, new DuplicateSearch(frame, dialogService, stateManager, preferencesService, entryTypesManager, taskExecutor)),
factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.CHECK_INTEGRITY, new IntegrityCheckAction(frame, preferencesService, dialogService, stateManager, taskExecutor, abbreviationRepository)),
factory.createMenuItem(StandardActions.CLEANUP_ENTRIES, new CleanupAction(frame, preferencesService, dialogService, stateManager)),
factory.createMenuItem(StandardActions.CLEANUP_ENTRIES, new CleanupAction(frame, preferencesService, dialogService, stateManager, taskExecutor)),

new SeparatorMenuItem(),

Expand All @@ -234,22 +234,22 @@ private void createMenu() {
new SeparatorMenuItem(),

factory.createSubMenu(StandardActions.ABBREVIATE,
factory.createMenuItem(StandardActions.ABBREVIATE_DEFAULT, new AbbreviateAction(StandardActions.ABBREVIATE_DEFAULT, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences())),
factory.createMenuItem(StandardActions.ABBREVIATE_DOTLESS, new AbbreviateAction(StandardActions.ABBREVIATE_DOTLESS, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences())),
factory.createMenuItem(StandardActions.ABBREVIATE_SHORTEST_UNIQUE, new AbbreviateAction(StandardActions.ABBREVIATE_SHORTEST_UNIQUE, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences()))),
factory.createMenuItem(StandardActions.ABBREVIATE_DEFAULT, new AbbreviateAction(StandardActions.ABBREVIATE_DEFAULT, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences(), abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.ABBREVIATE_DOTLESS, new AbbreviateAction(StandardActions.ABBREVIATE_DOTLESS, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences(), abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.ABBREVIATE_SHORTEST_UNIQUE, new AbbreviateAction(StandardActions.ABBREVIATE_SHORTEST_UNIQUE, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences(), abbreviationRepository, taskExecutor))),

factory.createMenuItem(StandardActions.UNABBREVIATE, new AbbreviateAction(StandardActions.UNABBREVIATE, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences()))
factory.createMenuItem(StandardActions.UNABBREVIATE, new AbbreviateAction(StandardActions.UNABBREVIATE, frame, dialogService, stateManager, preferencesService.getJournalAbbreviationPreferences(), abbreviationRepository, taskExecutor))
);

Menu lookupIdentifiers = factory.createSubMenu(StandardActions.LOOKUP_DOC_IDENTIFIER);
for (IdFetcher<?> fetcher : WebFetchers.getIdFetchers(preferencesService.getImportFormatPreferences())) {
LookupIdentifierAction<?> identifierAction = new LookupIdentifierAction<>(frame, fetcher, stateManager, undoManager);
LookupIdentifierAction<?> identifierAction = new LookupIdentifierAction<>(frame, fetcher, stateManager, undoManager, taskExecutor);
lookupIdentifiers.getItems().add(factory.createMenuItem(identifierAction.getAction(), identifierAction));
}

lookup.getItems().addAll(
lookupIdentifiers,
factory.createMenuItem(StandardActions.DOWNLOAD_FULL_TEXT, new DownloadFullTextAction(dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.DOWNLOAD_FULL_TEXT, new DownloadFullTextAction(dialogService, stateManager, preferencesService, taskExecutor)),

new SeparatorMenuItem(),

Expand All @@ -267,7 +267,7 @@ private void createMenu() {

factory.createMenuItem(StandardActions.WRITE_METADATA_TO_PDF,
new WriteMetadataToLinkedPdfsAction(dialogService, preferencesService.getFieldPreferences(), preferencesService.getFilePreferences(), preferencesService.getXmpPreferences(), entryTypesManager, abbreviationRepository, taskExecutor, stateManager)),
factory.createMenuItem(StandardActions.COPY_LINKED_FILES, new CopyFilesAction(dialogService, preferencesService, stateManager)),
factory.createMenuItem(StandardActions.COPY_LINKED_FILES, new CopyFilesAction(dialogService, preferencesService, stateManager, taskExecutor)),

new SeparatorMenuItem(),

Expand All @@ -283,7 +283,7 @@ private void createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.REBUILD_FULLTEXT_SEARCH_INDEX, new RebuildFulltextSearchIndexAction(stateManager, frame::getCurrentLibraryTab, dialogService, preferencesService.getFilePreferences()))
factory.createMenuItem(StandardActions.REBUILD_FULLTEXT_SEARCH_INDEX, new RebuildFulltextSearchIndexAction(stateManager, frame::getCurrentLibraryTab, dialogService, preferencesService.getFilePreferences(), taskExecutor))
);
SidePaneType webSearchPane = SidePaneType.WEB_SEARCH;
SidePaneType groupsPane = SidePaneType.GROUPS;
Expand Down Expand Up @@ -352,8 +352,8 @@ private Menu createSendSubMenu(ActionFactory factory,
PreferencesService preferencesService) {
Menu sendMenu = factory.createMenu(StandardActions.SEND);
sendMenu.getItems().addAll(
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsStandardEmailAction(dialogService, preferencesService, stateManager, entryTypesManager)),
factory.createMenuItem(StandardActions.SEND_TO_KINDLE, new SendAsKindleEmailAction(dialogService, preferencesService, stateManager))
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsStandardEmailAction(dialogService, preferencesService, stateManager, entryTypesManager, taskExecutor)),
factory.createMenuItem(StandardActions.SEND_TO_KINDLE, new SendAsKindleEmailAction(dialogService, preferencesService, stateManager, taskExecutor))
);

return sendMenu;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/MainToolBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void createToolBar() {
new HBox(
pushToApplicationButton,
factory.createIconButton(StandardActions.GENERATE_CITE_KEYS, new GenerateCitationKeyAction(frame, dialogService, stateManager, taskExecutor, preferencesService)),
factory.createIconButton(StandardActions.CLEANUP_ENTRIES, new CleanupAction(frame, preferencesService, dialogService, stateManager))),
factory.createIconButton(StandardActions.CLEANUP_ENTRIES, new CleanupAction(frame, preferencesService, dialogService, stateManager, taskExecutor))),

new Separator(Orientation.VERTICAL),

Expand Down
Loading

0 comments on commit a0e2564

Please sign in to comment.