Resolve SceneBuilder config path based on the platform #42

This commit is contained in:
mkpaz 2023-05-30 19:47:40 +04:00
parent d3bb5717ac
commit 7fceac13b0
4 changed files with 36 additions and 5 deletions

@ -174,7 +174,7 @@ class SceneBuilderDialog extends ModalDialog {
var previewBox = new HBox(20, previewImg, new VBox(20, previewLbl, downloadLnk)); var previewBox = new HBox(20, previewImg, new VBox(20, previewLbl, downloadLnk));
previewBox.setAlignment(Pos.TOP_LEFT); previewBox.setAlignment(Pos.TOP_LEFT);
var browseLbl = new Label("Select SceneBuilder installation directory:"); var browseLbl = new Label("Select the SceneBuilder installation directory:");
browseLbl.getStyleClass().addAll(TEXT_CAPTION, TEXT_MUTED); browseLbl.getStyleClass().addAll(TEXT_CAPTION, TEXT_MUTED);
var browseBtn = new Button("Browse", new FontIcon(Material2OutlinedAL.FOLDER)); var browseBtn = new Button("Browse", new FontIcon(Material2OutlinedAL.FOLDER));

@ -2,6 +2,7 @@ package atlantafx.sampler.page.general;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import atlantafx.base.util.PlatformUtils;
import atlantafx.sampler.theme.SamplerTheme; import atlantafx.sampler.theme.SamplerTheme;
import atlantafx.sampler.theme.SceneBuilderTheme; import atlantafx.sampler.theme.SceneBuilderTheme;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@ -23,9 +24,11 @@ import java.util.zip.ZipOutputStream;
final class SceneBuilderInstaller { final class SceneBuilderInstaller {
private static final String CONFIG_FILE_NAME = "SceneBuilder.cfg";
private static final String THEME_PACK_FILE_NAME = "atlantafx-scene-builder.zip"; private static final String THEME_PACK_FILE_NAME = "atlantafx-scene-builder.zip";
private final Path sceneBuilderDir; private final Path sceneBuilderDir;
private Path configDir;
public SceneBuilderInstaller(Path dir) { public SceneBuilderInstaller(Path dir) {
this.sceneBuilderDir = Objects.requireNonNull(dir); this.sceneBuilderDir = Objects.requireNonNull(dir);
@ -217,11 +220,41 @@ final class SceneBuilderInstaller {
} }
private Path getConfigDir() { private Path getConfigDir() {
return sceneBuilderDir.resolve("lib/app"); if (configDir != null) {
return configDir;
}
// app image structure is documented here
// https://docs.oracle.com/en/java/javase/20/jpackage/packaging-overview.html
Path dir = sceneBuilderDir;
if (PlatformUtils.isWindows()) {
dir = sceneBuilderDir.resolve("app");
} else if (PlatformUtils.isMac()) {
dir = sceneBuilderDir.resolve("Contents/app");
} else if (PlatformUtils.isUnix()) {
dir = sceneBuilderDir.resolve("lib/app");
}
// last chance, if app image has an unknown structure
if (!Files.exists(dir.resolve(CONFIG_FILE_NAME))) {
try (var stream = Files.walk(sceneBuilderDir)) {
dir = stream
.filter(f -> Objects.equals(f.getFileName().toString(), CONFIG_FILE_NAME))
.findAny()
.map(Path::getParent)
.orElse(null);
} catch (IOException e) {
e.printStackTrace();
}
}
this.configDir = dir;
return Objects.requireNonNullElse(dir, sceneBuilderDir);
} }
private Path getConfigFile() { private Path getConfigFile() {
return getConfigDir().resolve("SceneBuilder.cfg"); return getConfigDir().resolve(CONFIG_FILE_NAME);
} }
private Path getBackupConfigFile() { private Path getBackupConfigFile() {