Repository navigation
Expand file tree
/
Copy pathProgram.java
More file actions
87 lines (76 loc) · 2.93 KB
/
Copy pathProgram.java
File metadata and controls
87 lines (76 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.DesktopApplicationClientJava;
import com.DesktopApplicationClientJava.Api.ApiClient;
import com.DesktopApplicationClientJava.navigation.Navigator;
import com.DesktopApplicationClientJava.services.ServiceRegistry;
import com.DesktopApplicationClientJava.session.Session;
import com.DesktopApplicationClientJava.utils.ControllerWiring;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Program extends Application {
private static final String INITIAL_PAGE_PATH =
System.getenv().getOrDefault("INITIAL_PAGE_PATH", "/fxml/LoginForm.fxml");
private static final boolean MOCK_DATA =
Boolean.parseBoolean(System.getenv().getOrDefault("MOCK_DATA", "false"));
private static final double WINDOW_WIDTH = 800.0;
private static final double WINDOW_HEIGHT = 600.0;
private static final String ICON_BASE = "/images/icons/png/app-";
private static final List<Integer> ICON_SIZES = List.of(16, 32, 48, 64, 128, 256);
private final Session session = new Session();
private final ApiClient apiClient = new ApiClient(session);
private final ServiceRegistry services = createServices();
@Override
public void start(Stage stage) throws IOException {
Navigator navigator = new Navigator(stage, session, services);
FXMLLoader loader = new FXMLLoader(getClass().getResource(INITIAL_PAGE_PATH));
loader.setControllerFactory(
clazz -> {
try {
Object controller = clazz.getDeclaredConstructor().newInstance();
ControllerWiring.wire(controller, navigator, session, services);
return controller;
} catch (Exception e) {
throw new RuntimeException("Cannot create controller: " + clazz, e);
}
});
Parent root = loader.load();
Scene scene = new Scene(root);
loadStyleSheet(scene, "/styles.css");
loadIcons(stage);
stage.setScene(scene);
stage.setTitle("Госхрон" + (MOCK_DATA ? " [MOCK]" : ""));
stage.setWidth(WINDOW_WIDTH);
stage.setHeight(WINDOW_HEIGHT);
stage.centerOnScreen();
stage.show();
}
private ServiceRegistry createServices() {
if (MOCK_DATA) {
return ServiceRegistry.mock(session);
}
return new ServiceRegistry(apiClient, session);
}
private void loadStyleSheet(Scene scene, String path) {
var url = getClass().getResource(path);
if (url != null) {
scene.getStylesheets().add(url.toExternalForm());
}
}
private void loadIcons(Stage stage) {
for (int size : ICON_SIZES) {
try (InputStream in = getClass().getResourceAsStream(ICON_BASE + size + ".png")) {
if (in != null) {
stage.getIcons().add(new Image(in));
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}