Created
August 22, 2022 18:04
-
-
Save NotArchon/0d09b0e28632cfe63e2e2b704a515659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 SecurityException("read"); | |
if(!dataFolder.canWrite()) | |
throw new SecurityException("write"); | |
} catch(Exception e) { | |
if(e instanceof FileNotFoundException) { | |
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 { | |
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