Verified Owner
Top Description Fields Constructors Methods
org.jabref.gui.desktop.os

public abstract Class NativeDesktop

extends Object
Class Inheritance
Known Direct Subclasses
org.jabref.gui.desktop.os.OSX, org.jabref.gui.desktop.os.Windows, org.jabref.gui.desktop.os.DefaultDesktop, org.jabref.gui.desktop.os.Linux
Annotations
@org.jabref.architecture.AllowedToUseAwt
Imports
java.awt.Desktop, java.io.IOException, java.net.URI, java.nio.file.Files, .Path, java.util.List, .Locale, .Objects, .Optional, java.util.function.Consumer, java.util.regex.Pattern, org.jabref.architecture.AllowedToUseAwt, org.jabref.gui.DialogService, org.jabref.gui.clipboard.ClipBoardManager, org.jabref.gui.externalfiletype.ExternalFileType, .ExternalFileTypes, org.jabref.gui.frame.ExternalApplicationsPreferences, org.jabref.gui.preferences.GuiPreferences, org.jabref.gui.util.UiTaskExecutor, org.jabref.logic.FilePreferences, org.jabref.logic.importer.util.IdentifierParser, org.jabref.logic.l10n.Localization, org.jabref.logic.os.OS, org.jabref.logic.util.Directories, .HeadlessExecutorService, .URLUtil, org.jabref.logic.util.io.FileUtil, org.jabref.model.database.BibDatabaseContext, org.jabref.model.entry.BibEntry, org.jabref.model.entry.field.Field, .StandardField, org.jabref.model.entry.identifier.DOI, .Identifier, com.airhacks.afterburner.injection.Injector, com.google.common.annotations.VisibleForTesting, org.slf4j.LoggerFactory

This class contains bundles OS specific implementations for file directories and file/application open handling methods. In case the default does not work, subclasses provide the correct behavior.

We cannot use a static logger instance here in this class as the Logger first needs to be configured in the JabKit#initLogging. The configuration of tinylog will become immutable as soon as the first log entry is issued. See https://tinylog.org/v2/configuration/.

See https://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform for more implementation hints. https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html cannot be used as we don't want to rely on AWT.

For non-GUI things, see org.jabref.logic.os.OS.

Field Summary

Modifier and TypeField and Description

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv void
desktopBrowse(URI uri)

Opens the URI via the AWT desktop integration.

private static void
executeCommand(String command, String absolutePath, DialogService dialogService)

public static NativeDesktop
get()

public abstract Path

Returns:

the path
getApplicationDirectory
()

Returns the path to the system's applications folder.

public Path

Returns:

the path
getDefaultFileChooserDirectory
()

Get the user's default file chooser directory

public void
moveToTrash(Path path)

Moves the given file to the trash.

public boolean
public static void
openBrowser(String
the URL to open
url
,
ExternalApplicationsPreferences externalApplicationsPreferences)

Opens the given URL using the system browser

public static void
openBrowser(String
the URL to open
url
,
ExternalApplicationsPreferences externalApplicationsPreferences, Consumer<IOException>
invoked (from a background thread) when opening fails after this method has already returned; synchronous failures throw instead
onAsyncFailure
)

Opens the given URL using the system browser

pack-priv static void
openBrowser(String url, ExternalApplicationsPreferences externalApplicationsPreferences, Consumer<IOException> onAsyncFailure, NativeDesktop desktop)

public static void
openBrowser(URI url, ExternalApplicationsPreferences externalApplicationsPreferences)

public static void
openBrowserShowPopup(String
the URL to open
url
,
DialogService dialogService, ExternalApplicationsPreferences externalApplicationsPreferences)

Opens the url with the users standard Browser.

public static void
openConsole(Path
Location the console should be opened at.
file
,
GuiPreferences preferences, DialogService dialogService)

Opens a new console starting on the given file location

public abstract void
openConsole(String absolutePath, DialogService dialogService)

public static void
openCustomDoi(String link, GuiPreferences preferences, DialogService dialogService)

private static void
openDoi(String doi, GuiPreferences preferences)

public static boolean

Returns:

false if the link couldn't be resolved, true otherwise.
openExternalFileAnyFormat
(final BibDatabaseContext
The database this file belongs to.
databaseContext
,
ExternalApplicationsPreferences externalApplicationsPreferences, FilePreferences filePreferences, String
The filename.
link
,
final Optional<ExternalFileType> type)

Open an external file, attempting to use the correct viewer for it.

private static void
public static void
openExternalViewer(BibDatabaseContext databaseContext, GuiPreferences preferences, String initialLink, Field initialField, DialogService dialogService, BibEntry entry)

Open a http/pdf/ps viewer for the given link string.

public abstract void
openFile(String filePath, String fileType, ExternalApplicationsPreferences externalApplicationsPreferences)

public abstract void
openFileWithApplication(String
The filename.
filePath
,
String
Link to the app that opens the file.
application
)

Opens a file on an Operating System, using the given application.

public static void
openFolderAndSelectFile(Path
the location of the file
fileLink
,
ExternalApplicationsPreferences externalApplicationsPreferences, DialogService dialogService)

Opens a file browser of the folder of the given file.

public abstract void
private static void
openIsbn(String isbn, GuiPreferences preferences)

public abstract void
openUrlWithSystemHandler(String url)

Hands the URL string, unmodified, to the OS's URL-aware handler.

private static void
showManualOpenPopup(String url, DialogService dialogService, IOException exception)

pack-priv boolean
supportsDesktopBrowse()

Whether the AWT desktop integration can open URLs.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

Constructor Detail

NativeDesktopback to summary
public NativeDesktop()

Method Detail

desktopBrowseback to summary
pack-priv void desktopBrowse(URI uri) throws IOException

Opens the URI via the AWT desktop integration. Overridable for tests.

executeCommandback to summary
private static void executeCommand(String command, String absolutePath, DialogService dialogService)
getback to summary
public static NativeDesktop get()
getApplicationDirectoryback to summary
public abstract Path getApplicationDirectory()

Returns the path to the system's applications folder.

Returns:Path

the path

getDefaultFileChooserDirectoryback to summary
public Path getDefaultFileChooserDirectory()

Get the user's default file chooser directory

Returns:Path

the path

moveToTrashback to summary
public void moveToTrash(Path path)

Moves the given file to the trash.

Exceptions
UnsupportedOperationException:
if the current platform does not support the Desktop.Action#MOVE_TO_TRASH action
See Also
Desktop#moveToTrash(java.io.File)
moveToTrashSupportedback to summary
public boolean moveToTrashSupported()
openBrowserback to summary
public static void openBrowser(String url, ExternalApplicationsPreferences externalApplicationsPreferences) throws IOException

Opens the given URL using the system browser

Parameters
url:String

the URL to open

openBrowserback to summary
public static void openBrowser(String url, ExternalApplicationsPreferences externalApplicationsPreferences, Consumer<IOException> onAsyncFailure) throws IOException

Opens the given URL using the system browser

Parameters
url:String

the URL to open

onAsyncFailure:Consumer<IOException>

invoked (from a background thread) when opening fails after this method has already returned; synchronous failures throw instead

openBrowserback to summary
pack-priv static void openBrowser(String url, ExternalApplicationsPreferences externalApplicationsPreferences, Consumer<IOException> onAsyncFailure, NativeDesktop desktop) throws IOException
Annotations
@VisibleForTesting
openBrowserback to summary
public static void openBrowser(URI url, ExternalApplicationsPreferences externalApplicationsPreferences) throws IOException
openBrowserShowPopupback to summary
public static void openBrowserShowPopup(String url, DialogService dialogService, ExternalApplicationsPreferences externalApplicationsPreferences)

Opens the url with the users standard Browser. If that fails a popup will be shown to instruct the user to open the link manually and the link gets copied to the clipboard

Parameters
url:String

the URL to open

openConsoleback to summary
public static void openConsole(Path file, GuiPreferences preferences, DialogService dialogService) throws IOException

Opens a new console starting on the given file location

Parameters
file:Path

Location the console should be opened at.

openConsoleback to summary
public abstract void openConsole(String absolutePath, DialogService dialogService) throws IOException
openCustomDoiback to summary
public static void openCustomDoi(String link, GuiPreferences preferences, DialogService dialogService)
openDoiback to summary
private static void openDoi(String doi, GuiPreferences preferences) throws IOException
openExternalFileAnyFormatback to summary
public static boolean openExternalFileAnyFormat(final BibDatabaseContext databaseContext, ExternalApplicationsPreferences externalApplicationsPreferences, FilePreferences filePreferences, String link, final Optional<ExternalFileType> type) throws IOException

Open an external file, attempting to use the correct viewer for it. If the "file" is an online link, instead open it with the browser

Parameters
databaseContext:BibDatabaseContext

The database this file belongs to.

link:String

The filename.

Returns:boolean

false if the link couldn't be resolved, true otherwise.

openExternalFilePlatformIndependentback to summary
private static void openExternalFilePlatformIndependent(Optional<ExternalFileType> fileType, String filePath, ExternalApplicationsPreferences externalApplicationsPreferences) throws IOException
openExternalViewerback to summary
public static void openExternalViewer(BibDatabaseContext databaseContext, GuiPreferences preferences, String initialLink, Field initialField, DialogService dialogService, BibEntry entry) throws IOException

Open a http/pdf/ps viewer for the given link string.

Opening a PDF file at the file field is done at org.jabref.gui.fieldeditors.LinkedFileViewModel#open

openFileback to summary
public abstract void openFile(String filePath, String fileType, ExternalApplicationsPreferences externalApplicationsPreferences) throws IOException
openFileWithApplicationback to summary
public abstract void openFileWithApplication(String filePath, String application) throws IOException

Opens a file on an Operating System, using the given application.

Parameters
filePath:String

The filename.

application:String

Link to the app that opens the file.

openFolderAndSelectFileback to summary
public static void openFolderAndSelectFile(Path fileLink, ExternalApplicationsPreferences externalApplicationsPreferences, DialogService dialogService) throws IOException

Opens a file browser of the folder of the given file. If possible, the file is selected

Parameters
fileLink:Path

the location of the file

Exceptions
IOException:
if the default file browser cannot be opened
openFolderAndSelectFileback to summary
public abstract void openFolderAndSelectFile(Path file) throws IOException
openIsbnback to summary
private static void openIsbn(String isbn, GuiPreferences preferences) throws IOException
openUrlWithSystemHandlerback to summary
public abstract void openUrlWithSystemHandler(String url) throws IOException

Hands the URL string, unmodified, to the OS's URL-aware handler. Used when Desktop.browse is unsupported, not applicable, or failed; the URL must never be run through Path.of, which mangles it.

showManualOpenPopupback to summary
private static void showManualOpenPopup(String url, DialogService dialogService, IOException exception)
supportsDesktopBrowseback to summary
pack-priv boolean supportsDesktopBrowse()

Whether the AWT desktop integration can open URLs. Overridable for tests.