Skip to content

Instantly share code, notes, and snippets.

@NotArchon
Created August 22, 2022 18:06
Show Gist options
  • Save NotArchon/c990e5975af1f59c8ed0f06c2a04f3a6 to your computer and use it in GitHub Desktop.
Save NotArchon/c990e5975af1f59c8ed0f06c2a04f3a6 to your computer and use it in GitHub Desktop.
package org.discrypto.client;
import javafx.application.Platform;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import lombok.Getter;
import lombok.Setter;
import org.discrypto.client.model.User;
import org.discrypto.client.screens.Screen;
import org.discrypto.client.screens.main.MainScreen;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Client extends Application {
private static final ExecutorService VIRTUAL_EXECUTOR
= Executors.newVirtualThreadPerTaskExecutor();
@Setter @Getter private static volatile User user;
private static final File dataFolder;
/* Use Main class */
protected static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
validateDataFolder();
MainScreen main = new MainScreen();
main.stage = stage;
openScreen(main);
}
public static Scene loadScene(String fxmlName) {
return loadScene(fxmlName, null);
}
public static Scene loadScene(String fxmlName, Object controller) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getResource(fxmlName + ".fxml"));
if(controller != null)
fxmlLoader.setController(controller);
return new Scene(fxmlLoader.load());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public static void openScreen(Screen screen) {
if(screen.stage == null)
screen.stage = new Stage();
screen.stage.setTitle(screen.getTitle());
screen.stage.setScene(loadScene(screen.getFxml(), screen));
screen.beforeShow();
screen.stage.show();
screen.afterShow();
}
public static void showMessageDialog(String dialogTitle, String messageText, String buttonText, ButtonBar.ButtonData buttonData) {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle(dialogTitle);
ButtonType type = new ButtonType(buttonText, buttonData);
dialog.setContentText(messageText);
dialog.getDialogPane().getButtonTypes().add(type);
dialog.showAndWait();
}
public static URL getResource(String name) {
return Client.class.getResource(name);
}
public static Future<?> submitVirtual(Runnable virtualThreadAction) {
/* THIS ACTION WILL RUN ASYNCHRONOUSLY ON A NEW VIRTUAL THREAD. */
return VIRTUAL_EXECUTOR.submit(virtualThreadAction);
}
private static void validateDataFolder() {
try {
if(!dataFolder.exists())
throw new FileNotFoundException();
if(!dataFolder.canRead())
throw new IOException("read");
if(!dataFolder.canWrite())
throw new IOException("write");
} catch(Exception e) {
if(e instanceof FileNotFoundException) {
/* fail-safe code in case user somehow gets here */
showMessageDialog(
"Discrypto Trading Bot | Failed to find required path!",
"Please make sure the following path exists:\n\n" + dataFolder.getAbsolutePath(),
"OK",
ButtonBar.ButtonData.OK_DONE
);
} else {
/* fail-safe code in case user somehow gets here */
showMessageDialog(
"Discrypto Trading Bot | Failed to access required path!",
"Please make sure the following path is readable/writeable:\n\n" + dataFolder.getAbsolutePath() + "\n\n" + "You can also try running this program with admin privileges.",
"OK",
ButtonBar.ButtonData.OK_DONE
);
}
}
}
static {
String userHome = null;
try {
userHome = System.getProperty("user.home");
} catch(Exception e) {
/* ignored */
}
dataFolder = new File((userHome == null ? "~" : userHome) + File.separator + "Discrypto");
try {
if(!dataFolder.exists())
dataFolder.mkdir();
} catch(Exception e) {
/* ignored */
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment