Skip to content

Commit

Permalink
Merge pull request #5008 from jmacxx/remove_awt_dependencies
Browse files Browse the repository at this point in the history
Remove awt dependencies
  • Loading branch information
ripcurlx authored Jan 8, 2021
2 parents 76e2330 + e50a182 commit be40c78
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 307 deletions.
106 changes: 11 additions & 95 deletions common/src/main/java/bisq/common/util/DesktopUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package bisq.common.util;

import java.awt.Desktop;

import java.net.URI;

import java.io.File;
Expand All @@ -27,25 +25,28 @@
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

// Taken form https://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform,
// originally net.mightypork.rpack.utils.DesktopApi
@Slf4j
class DesktopUtil {

public static boolean browse(URI uri) {
return openSystemSpecific(uri.toString()) || browseDESKTOP(uri);
return openSystemSpecific(uri.toString());
}


public static boolean open(File file) {
return openSystemSpecific(file.getPath()) || openDESKTOP(file);
return openSystemSpecific(file.getPath());
}


public static boolean edit(File file) {
// you can try something like
// runCommand("gimp", "%s", file.getPath())
// based on user preferences.
return openSystemSpecific(file.getPath()) || editDESKTOP(file);
return openSystemSpecific(file.getPath());
}


Expand All @@ -69,82 +70,10 @@ private static boolean openSystemSpecific(String what) {
}


private static boolean browseDESKTOP(URI uri) {

logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
try {
if (!Desktop.isDesktopSupported()) {
logErr("Platform is not supported.");
return false;
}

if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
logErr("BROWSE is not supported.");
return false;
}

Desktop.getDesktop().browse(uri);

return true;
} catch (Throwable t) {
logErr("Error using desktop browse.", t);
return false;
}
}


private static boolean openDESKTOP(File file) {

logOut("Trying to use Desktop.getDesktop().open() with " + file.toString());
try {
if (!Desktop.isDesktopSupported()) {
logErr("Platform is not supported.");
return false;
}

if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
logErr("OPEN is not supported.");
return false;
}

Desktop.getDesktop().open(file);

return true;
} catch (Throwable t) {
logErr("Error using desktop open.", t);
return false;
}
}


private static boolean editDESKTOP(File file) {

logOut("Trying to use Desktop.getDesktop().edit() with " + file);
try {
if (!Desktop.isDesktopSupported()) {
logErr("Platform is not supported.");
return false;
}

if (!Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
logErr("EDIT is not supported.");
return false;
}

Desktop.getDesktop().edit(file);

return true;
} catch (Throwable t) {
logErr("Error using desktop edit.", t);
return false;
}
}


@SuppressWarnings("SameParameterValue")
private static boolean runCommand(String command, String args, String file) {

logOut("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file);
log.info("Trying to exec: cmd = {} args = {} file = {}", command, args, file);

String[] parts = prepareCommand(command, args, file);

Expand All @@ -155,17 +84,17 @@ private static boolean runCommand(String command, String args, String file) {
try {
int value = p.exitValue();
if (value == 0) {
logErr("Process ended immediately.");
log.warn("Process ended immediately.");
} else {
logErr("Process crashed.");
log.warn("Process crashed.");
}
return false;
} catch (IllegalThreadStateException e) {
logErr("Process is running.");
log.info("Process is running.");
return true;
}
} catch (IOException e) {
logErr("Error running command.", e);
log.warn("Error running command. {}", e);
return false;
}
}
Expand All @@ -187,19 +116,6 @@ private static String[] prepareCommand(String command, String args, String file)
return parts.toArray(new String[parts.size()]);
}

private static void logErr(String msg, Throwable t) {
System.err.println(msg);
t.printStackTrace();
}

private static void logErr(String msg) {
System.err.println(msg);
}

private static void logOut(String msg) {
System.out.println(msg);
}

public enum EnumOS {
linux,
macos,
Expand Down
35 changes: 4 additions & 31 deletions common/src/main/java/bisq/common/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.awt.Desktop.Action;
import static java.awt.Desktop.getDesktop;
import static java.awt.Desktop.isDesktopSupported;

@Slf4j
public class Utilities {
Expand Down Expand Up @@ -292,37 +289,13 @@ public static boolean isCorrectOSArchitecture() {
}

public static void openURI(URI uri) throws IOException {
if (!isLinux()
&& isDesktopSupported()
&& getDesktop().isSupported(Action.BROWSE)) {
getDesktop().browse(uri);
} else {
// Maybe Application.HostServices works in those cases?
// HostServices hostServices = getHostServices();
// hostServices.showDocument(uri.toString());

// On Linux Desktop is poorly implemented.
// See https://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
if (!DesktopUtil.browse(uri))
throw new IOException("Failed to open URI: " + uri.toString());
}
if (!DesktopUtil.browse(uri))
throw new IOException("Failed to open URI: " + uri.toString());
}

public static void openFile(File file) throws IOException {
if (!isLinux()
&& isDesktopSupported()
&& getDesktop().isSupported(Action.OPEN)) {
getDesktop().open(file);
} else {
// Maybe Application.HostServices works in those cases?
// HostServices hostServices = getHostServices();
// hostServices.showDocument(uri.toString());

// On Linux Desktop is poorly implemented.
// See https://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
if (!DesktopUtil.open(file))
throw new IOException("Failed to open file: " + file.toString());
}
if (!DesktopUtil.open(file))
throw new IOException("Failed to open file: " + file.toString());
}

public static String getDownloadOfHomeDir() {
Expand Down
21 changes: 9 additions & 12 deletions desktop/src/main/java/bisq/desktop/app/BisqApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import javafx.application.Application;

import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

Expand All @@ -68,11 +69,9 @@
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;

import javafx.geometry.Rectangle2D;
import javafx.geometry.BoundingBox;

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -203,20 +202,21 @@ public void handleUncaughtException(Throwable throwable, boolean doShutDown) {
///////////////////////////////////////////////////////////////////////////////////////////

private Scene createAndConfigScene(MainView mainView, Injector injector) {
Rectangle maxWindowBounds = new Rectangle();
//Rectangle maxWindowBounds = new Rectangle();
Rectangle2D maxWindowBounds = new Rectangle2D(0, 0, 0, 0);
try {
maxWindowBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
maxWindowBounds = Screen.getPrimary().getBounds();
} catch (IllegalArgumentException e) {
// Multi-screen environments may encounter IllegalArgumentException (Window must not be zero)
// Just ignore the exception and continue, which means the window will use the minimum window size below
// since we are unable to determine if we can use a larger size
}
Scene scene = new Scene(mainView.getRoot(),
maxWindowBounds.width < INITIAL_WINDOW_WIDTH ?
Math.max(maxWindowBounds.width, MIN_WINDOW_WIDTH) :
maxWindowBounds.getWidth() < INITIAL_WINDOW_WIDTH ?
Math.max(maxWindowBounds.getWidth(), MIN_WINDOW_WIDTH) :
INITIAL_WINDOW_WIDTH,
maxWindowBounds.height < INITIAL_WINDOW_HEIGHT ?
Math.max(maxWindowBounds.height, MIN_WINDOW_HEIGHT) :
maxWindowBounds.getHeight() < INITIAL_WINDOW_HEIGHT ?
Math.max(maxWindowBounds.getHeight(), MIN_WINDOW_HEIGHT) :
INITIAL_WINDOW_HEIGHT);

addSceneKeyEventHandler(scene, injector);
Expand All @@ -232,9 +232,6 @@ private Scene createAndConfigScene(MainView mainView, Injector injector) {
}

private void setupStage(Scene scene) {
// configure the system tray
SystemTray.create(stage, shutDownHandler);

stage.setOnCloseRequest(event -> {
event.consume();
shutDownByUser();
Expand Down
Loading

0 comments on commit be40c78

Please sign in to comment.