11package de .doubleslash .keeptime ;
22
33import java .io .IOException ;
4+ import java .io .PrintWriter ;
5+ import java .io .StringWriter ;
46import java .time .LocalDate ;
57import java .util .List ;
68import java .util .Optional ;
2325import de .doubleslash .keeptime .viewpopup .GlobalScreenListener ;
2426import de .doubleslash .keeptime .viewpopup .ViewControllerPopup ;
2527import javafx .application .Application ;
28+ import javafx .event .EventHandler ;
2629import javafx .fxml .FXMLLoader ;
2730import javafx .scene .Parent ;
2831import javafx .scene .Scene ;
32+ import javafx .scene .control .Alert ;
33+ import javafx .scene .control .Alert .AlertType ;
34+ import javafx .scene .control .Label ;
35+ import javafx .scene .control .TextArea ;
36+ import javafx .scene .layout .GridPane ;
2937import javafx .scene .layout .Pane ;
38+ import javafx .scene .layout .Priority ;
3039import javafx .scene .paint .Color ;
3140import javafx .stage .Stage ;
3241import javafx .stage .StageStyle ;
42+ import javafx .stage .WindowEvent ;
3343
3444@ SpringBootApplication
3545public class Main extends Application {
3646
37- private final Logger log = LoggerFactory .getLogger (this . getClass () );
47+ private static final Logger LOG = LoggerFactory .getLogger (Main . class );
3848
3949 public static final String VERSION = "v0.0.2" ;
4050
@@ -45,9 +55,13 @@ public class Main extends Application {
4555 private Model model ;
4656 private Controller controller ;
4757
58+ private ViewController viewController ;
59+
60+ private GlobalScreenListener globalScreenListener ;
61+
4862 @ Override
4963 public void init () throws Exception {
50- log .info ("Starting KeepTime {}" , VERSION );
64+ LOG .info ("Starting KeepTime {}" , VERSION );
5165 final DefaultExceptionHandler defaultExceptionHandler = new DefaultExceptionHandler ();
5266 defaultExceptionHandler .register ();
5367
@@ -58,46 +72,59 @@ public void init() throws Exception {
5872 }
5973
6074 @ Override
61- public void start (final Stage primaryStage ) throws Exception {
75+ public void start (final Stage primaryStage ) {
76+ LOG .info ("Initialising the UI" );
77+ try {
78+ initUI (primaryStage );
79+ LOG .info ("UI successfully initialised." );
80+ } catch (final Exception e ) {
81+ LOG .error ("There was an error while initialising the UI" , e );
6282
63- log .debug ("Reading configuration" );
83+ final Alert alert = new Alert (AlertType .ERROR );
84+ alert .setTitle ("Error" );
85+ alert .setHeaderText ("Could not start application" );
86+ alert .setContentText ("Please send the error with your logs folder to a developer" );
6487
65- // TODO there should just be one instance of settings in the repo
66- final List <Settings > settingsList = model .getSettingsRepository ().findAll ();
67- final Settings settings ;
68- if (settingsList .isEmpty ()) {
69- settings = new Settings ();
70- settings .setTaskBarColor (Model .TASK_BAR_COLOR .get ());
88+ final StringWriter sw = new StringWriter ();
89+ final PrintWriter pw = new PrintWriter (sw );
90+ e .printStackTrace (pw );
91+ final String exceptionText = sw .toString ();
7192
72- settings .setDefaultBackgroundColor (Model .DEFAULT_BACKGROUND_COLOR .get ());
73- settings .setDefaultFontColor (Model .DEFAULT_FONT_COLOR .get ());
93+ final Label label = new Label ("The exception stacktrace was:" );
7494
75- settings .setHoverBackgroundColor (Model .HOVER_BACKGROUND_COLOR .get ());
76- settings .setHoverFontColor (Model .HOVER_FONT_COLOR .get ());
77- settings .setUseHotkey (false );
78- settings .setDisplayProjectsRight (true );
79- model .getSettingsRepository ().save (settings );
80- } else {
81- settings = settingsList .get (0 );
95+ final TextArea textArea = new TextArea (exceptionText );
96+ textArea .setEditable (false );
97+ textArea .setWrapText (true );
98+
99+ textArea .setMaxWidth (Double .MAX_VALUE );
100+ textArea .setMaxHeight (Double .MAX_VALUE );
101+ GridPane .setVgrow (textArea , Priority .ALWAYS );
102+ GridPane .setHgrow (textArea , Priority .ALWAYS );
103+
104+ final GridPane expContent = new GridPane ();
105+ expContent .setMaxWidth (Double .MAX_VALUE );
106+ expContent .add (label , 0 , 0 );
107+ expContent .add (textArea , 0 , 1 );
108+
109+ alert .getDialogPane ().setExpandableContent (expContent );
110+ alert .showAndWait ();
111+ System .exit (1 );
82112 }
113+ }
83114
84- Model .DEFAULT_BACKGROUND_COLOR .set (settings .getDefaultBackgroundColor ());
85- Model .DEFAULT_FONT_COLOR .set (settings .getDefaultFontColor ());
86- Model .HOVER_BACKGROUND_COLOR .set (settings .getHoverBackgroundColor ());
87- Model .HOVER_FONT_COLOR .set (settings .getHoverFontColor ());
88- Model .TASK_BAR_COLOR .set (settings .getTaskBarColor ());
89- Model .USE_HOTKEY .set (settings .isUseHotkey ());
90- Model .DISPLAY_PROJECTS_RIGHT .set (settings .isDisplayProjectsRight ());
115+ private void initUI (final Stage primaryStage ) throws Exception {
116+
117+ readSettings ();
91118
92119 final List <Work > todaysWorkItems = model .getWorkRepository ().findByCreationDate (LocalDate .now ());
93- log .info ("Found {} past work items" , todaysWorkItems .size ());
120+ LOG .info ("Found {} past work items" , todaysWorkItems .size ());
94121 model .getPastWorkItems ().addAll (todaysWorkItems );
95122
96123 final List <Project > projects = model .getProjectRepository ().findAll ();
97124
98- log .debug ("Found '{}' projects" , projects .size ());
125+ LOG .debug ("Found '{}' projects" , projects .size ());
99126 if (projects .isEmpty ()) {
100- log .info ("Adding default project" );
127+ LOG .info ("Adding default project" );
101128 projects .add (Model .DEFAULT_PROJECT );
102129 model .getProjectRepository ().save (Model .DEFAULT_PROJECT );
103130 }
@@ -110,37 +137,58 @@ public void start(final Stage primaryStage) throws Exception {
110137 final Optional <Project > findAny = projects .stream ().filter (Project ::isDefault ).findAny ();
111138 if (findAny .isPresent ()) {
112139 model .setIdleProject (findAny .get ());
113- log .debug ("Using project '{}' as default project." , model .getIdleProject ());
140+ LOG .debug ("Using project '{}' as default project." , model .getIdleProject ());
114141 }
115142
116143 primaryStage .setOnHiding (we -> {
117144 popupViewStage .close ();
118- globalScreenListener .register ( false ); // deregister, as this will keep app running
145+ globalScreenListener .shutdown ( ); // deregister, as this will keep app running
119146 });
120147
121- try {
122- initialiseUI (primaryStage );
123- } catch (final Exception e ) {
124- log .error (e .getMessage ());
125- }
148+ initialiseUI (primaryStage );
149+ initialisePopupUI (primaryStage );
150+ }
126151
127- try {
128- initialisePopupUI (primaryStage );
129- } catch (final Exception e ) {
130- log .error (e .getMessage ());
152+ private void readSettings () {
153+ LOG .debug ("Reading configuration" );
154+
155+ final List <Settings > settingsList = model .getSettingsRepository ().findAll ();
156+ final Settings settings ;
157+ if (settingsList .isEmpty ()) {
158+ settings = new Settings ();
159+ settings .setTaskBarColor (Model .TASK_BAR_COLOR .get ());
160+
161+ settings .setDefaultBackgroundColor (Model .ORIGINAL_DEFAULT_BACKGROUND_COLOR );
162+ settings .setDefaultFontColor (Model .ORIGINAL_DEFAULT_FONT_COLOR );
163+
164+ settings .setHoverBackgroundColor (Model .ORIGINAL_HOVER_BACKGROUND_COLOR );
165+ settings .setHoverFontColor (Model .ORIGINAL_HOVER_Font_COLOR );
166+ settings .setUseHotkey (false );
167+ settings .setDisplayProjectsRight (false );
168+ settings .setHideProjectsOnMouseExit (true );
169+ model .getSettingsRepository ().save (settings );
170+ } else {
171+ settings = settingsList .get (0 );
131172 }
132- }
133173
134- GlobalScreenListener globalScreenListener ;
174+ Model .DEFAULT_BACKGROUND_COLOR .set (settings .getDefaultBackgroundColor ());
175+ Model .DEFAULT_FONT_COLOR .set (settings .getDefaultFontColor ());
176+ Model .HOVER_BACKGROUND_COLOR .set (settings .getHoverBackgroundColor ());
177+ Model .HOVER_FONT_COLOR .set (settings .getHoverFontColor ());
178+ Model .TASK_BAR_COLOR .set (settings .getTaskBarColor ());
179+ Model .USE_HOTKEY .set (settings .isUseHotkey ());
180+ Model .DISPLAY_PROJECTS_RIGHT .set (settings .isDisplayProjectsRight ());
181+ Model .HIDE_PROJECTS_ON_MOUSE_EXIT .set (settings .isHideProjectsOnMouseExit ());
182+ }
135183
136184 private void initialisePopupUI (final Stage primaryStage ) throws IOException {
137- // TODO register only if it is enabled
185+ LOG .debug ("Initialising popup UI." );
186+
138187 globalScreenListener = new GlobalScreenListener ();
139- // TODO stop and close stage when main stage is shutdown
188+
140189 Model .USE_HOTKEY .addListener ((a , b , newValue ) -> globalScreenListener .register (newValue ));
141190 globalScreenListener .register (Model .USE_HOTKEY .get ());
142191
143- // Platform.setImplicitExit(false); // TODO maybe not needed as other view will be available
144192 popupViewStage = new Stage ();
145193 popupViewStage .initOwner (primaryStage );
146194 // Load root layout from fxml file.
@@ -159,44 +207,40 @@ private void initialisePopupUI(final Stage primaryStage) throws IOException {
159207 viewControllerPopupController .setStage (popupViewStage );
160208 viewControllerPopupController .setControllerAndModel (controller , model );
161209 globalScreenListener .setViewController (viewControllerPopupController );
162-
163210 }
164211
165- ViewController viewController ;
166-
167- private void initialiseUI (final Stage primaryStage ) {
168- try {
169- Pane mainPane ;
170-
171- // Load root layout from fxml file.
172- final FXMLLoader loader = new FXMLLoader ();
173- loader .setLocation (Resources .getResource (RESOURCE .FXML_VIEW_LAYOUT ));
174- loader .setControllerFactory (springContext ::getBean );
175- mainPane = loader .load ();
176- primaryStage .initStyle (StageStyle .TRANSPARENT );
177- // Show the scene containing the root layout.
178- final Scene mainScene = new Scene (mainPane , Color .TRANSPARENT );
212+ private void initialiseUI (final Stage primaryStage ) throws IOException {
213+ LOG .debug ("Initialising main UI." );
214+ Pane mainPane ;
179215
180- primaryStage .setTitle ("KeepTime" );
181- primaryStage .setScene (mainScene );
182- primaryStage .setAlwaysOnTop (true );
183- primaryStage .setResizable (false );
184-
185- primaryStage .setOnCloseRequest (actionEvent -> log .info ("On close request" ));
186-
187- viewController = loader .getController ();
188- // Give the controller access to the main app.
189- viewController .setStage (primaryStage );
216+ // Load root layout from fxml file.
217+ final FXMLLoader loader = new FXMLLoader ();
218+ loader .setLocation (Resources .getResource (RESOURCE .FXML_VIEW_LAYOUT ));
219+ loader .setControllerFactory (springContext ::getBean );
220+ mainPane = loader .load ();
221+ primaryStage .initStyle (StageStyle .TRANSPARENT );
222+ // Show the scene containing the root layout.
223+ final Scene mainScene = new Scene (mainPane , Color .TRANSPARENT );
190224
191- viewController . setController ( controller , model );
225+ // Image(Resources.getResource(RESOURCE.ICON_MAIN).toString())); // TODO use an app icon
192226
193- primaryStage .show ();
227+ primaryStage .setTitle ("KeepTime" );
228+ primaryStage .setScene (mainScene );
229+ primaryStage .setAlwaysOnTop (true );
230+ primaryStage .setResizable (false );
194231
195- } catch (
232+ primaryStage .setOnCloseRequest (new EventHandler <WindowEvent >() {
233+ @ Override
234+ public void handle (final WindowEvent event ) {
235+ LOG .info ("On close request" );
236+ }
237+ });
238+ primaryStage .show ();
239+ viewController = loader .getController ();
240+ // Give the controller access to the main app.
241+ viewController .setStage (primaryStage );
242+ viewController .setController (controller , model );
196243
197- final Exception e ) {
198- log .error ("Error: " + e .toString (), e );
199- }
200244 }
201245
202246 @ Override
0 commit comments