1
1
package home ;
2
2
3
- import java .io .IOException ;
4
3
import java .lang .Thread .UncaughtExceptionHandler ;
5
- import java .sql .SQLException ;
6
4
import java .util .function .BiFunction ;
7
5
8
6
import org .slf4j .Logger ;
9
7
import org .slf4j .LoggerFactory ;
10
8
11
- import home .db .DbInitializer ;
12
- import home .db .dao .DaoSQLite ;
13
9
import home .gui .Gui ;
14
- import home .gui .component .CustomJFileChooser ;
15
- import home .gui .component .CustomJFileChooser .ChooserOperation ;
16
- import home .utils .Utils ;
10
+ import home .utils .LogUtils ;
17
11
18
12
public final class Main {
19
13
20
14
private static final Logger LOG = LoggerFactory .getLogger (Main .class );
21
15
22
- private static final String START_LOG_MESSAGE = "Application {} v{} started successfully." ;
23
-
24
16
private static final String DEFAULT_APP_NAME = "=VEHICLE_ACCOUNTING=" ;
25
17
private static final String DEFAULT_APP_VERSION = "UNKNOWN" ;
26
18
@@ -32,9 +24,9 @@ public static void main(String[] args) {
32
24
try {
33
25
startApplication ();
34
26
isStarted = true ;
35
- LOG .info (START_LOG_MESSAGE , appName , appVersion );
27
+ LOG .info ("Application {} v{} started successfully." , appName , appVersion );
36
28
} catch (Exception e ) {
37
- Utils .logAndShowError (LOG , null , e .getMessage (), "Application start error" , e );
29
+ LogUtils .logAndShowError (LOG , null , e .getMessage (), "Application start error" , e );
38
30
} finally {
39
31
if (!isStarted ) {
40
32
System .exit (1 );
@@ -49,14 +41,14 @@ private static void startApplication() {
49
41
Settings .readSettings ();
50
42
Gui .INSTANCE .buildGui ();
51
43
52
- initDb ();
44
+ Data . initDb ();
53
45
}
54
46
55
47
private static void setUncaughtExceptionProcessing () {
56
48
UncaughtExceptionHandler handler = new UncaughtExceptionHandler () {
57
49
@ Override
58
50
public void uncaughtException (Thread t , Throwable e ) {
59
- Utils .logAndShowError (LOG , null , e .getMessage (), "Error" , e );
51
+ LogUtils .logAndShowError (LOG , null , e .getMessage (), "Error" , e );
60
52
System .exit (1 );
61
53
}
62
54
};
@@ -65,42 +57,8 @@ public void uncaughtException(Thread t, Throwable e) {
65
57
66
58
private static void initAppDescription () {
67
59
BiFunction <String , String , String > getSafeVal = (val , def ) -> val != null ? val : def ;
68
- Package pacage = Main .class .getPackage ();
69
- appName = getSafeVal .apply (pacage .getImplementationTitle (), DEFAULT_APP_NAME );
70
- appVersion = getSafeVal .apply (pacage .getImplementationVersion (), DEFAULT_APP_VERSION );
71
- }
72
-
73
- private static void initDb () {
74
- if (Settings .hasPathToDbFile ()) {
75
- readDataFromDb ();
76
- } else {
77
- try {
78
- CustomJFileChooser .showChooser (null , ChooserOperation .CREATE_OR_OPEN );
79
- readDataFromDb ();
80
- Gui .INSTANCE .setDbLabel (Settings .getDbFilePath ());
81
- } catch (IOException e ) {
82
- throw new IllegalStateException ("Error while create/open DB file." , e );
83
- }
84
- }
85
- }
86
-
87
- private static void readDataFromDb () {
88
- try {
89
- DbInitializer .createTableIfNotExists ();
90
-
91
- Utils .runInThread ("-> read data from database" , () -> {
92
- try {
93
- Storage .INSTANCE .refresh (DaoSQLite .getInstance ().readAll ());
94
- } catch (SQLException e ) {
95
- Utils .logAndShowError (LOG , null ,
96
- "Error while read data from database: " + e .getMessage (),
97
- "Data reading error" , e );
98
- throw new IllegalStateException ("Error while read data from database: "
99
- + e .getMessage (), e );
100
- }
101
- });
102
- } catch (Exception e ) {
103
- throw new IllegalStateException ("Error while read data from database." , e );
104
- }
60
+ Package pkg = Main .class .getPackage ();
61
+ appName = getSafeVal .apply (pkg .getImplementationTitle (), DEFAULT_APP_NAME );
62
+ appVersion = getSafeVal .apply (pkg .getImplementationVersion (), DEFAULT_APP_VERSION );
105
63
}
106
64
}
0 commit comments