4646import javafx .collections .FXCollections ;
4747import javafx .collections .ObservableList ;
4848import javafx .fxml .FXML ;
49- import javafx .fxml .FXMLLoader ;
5049import javafx .scene .control .*;
5150import javafx .scene .control .Alert .AlertType ;
5251import javafx .scene .control .cell .PropertyValueFactory ;
@@ -173,21 +172,20 @@ public class SettingsController {
173172 @ FXML
174173 private PasswordField authPassword ;
175174
176- private ToggleGroup toggleGroup ;
177-
178175 @ FXML
179- private RadioButton radioApiOff ;
176+ private CheckBox activateRestApiCheckBox ;
180177
181178 @ FXML
182- private RadioButton radioApiOn ;
179+ private Hyperlink swaggerHyperLink ;
183180
184181 @ FXML
185182 private TextField authPort ;
186183
187- private String propertiesFilePath = "application.properties" ;
184+ private final String propertiesFilePath = "application.properties" ;
188185
189186 private static final String GITHUB_PAGE = "https://www.github.com/doubleSlashde/KeepTime" ;
190187 private static final String GITHUB_ISSUE_PAGE = GITHUB_PAGE + "/issues" ;
188+
191189 private static final Color HYPERLINK_COLOR = Color .rgb (0 , 115 , 170 );
192190 private final ApplicationProperties applicationProperties ;
193191
@@ -238,51 +236,43 @@ private void initialize() {
238236 initExportButton ();
239237 initImportButton ();
240238
241- toggleGroup = new ToggleGroup ();
242- radioApiOff .setToggleGroup (toggleGroup );
243- radioApiOn .setToggleGroup (toggleGroup );
244-
245- Properties properties = new Properties ();
246239 try (FileInputStream input = new FileInputStream (propertiesFilePath )) {
240+ Properties properties = new Properties ();
247241 properties .load (input );
248- String apistatus = properties .getProperty ("api" );
249- if (apistatus != null ) {
250- if (apistatus .equals ("ON" )) {
251- radioApiOn .setSelected (true );
252- radioApiOff .setSelected (false );
242+ String apiStatus = properties .getProperty ("api" );
243+ if (apiStatus != null ) {
244+ if (apiStatus .equals ("ON" )) {
245+ activateRestApiCheckBox .setSelected (true );
253246 String port = properties .getProperty ("server.port" );
254247 String userName = properties .getProperty ("spring.security.user.name" );
255248 String userPassword = properties .getProperty ("spring.security.user.password" );
256249
257250 if (port != null ) {
258251 authPort .setText (port );
259-
260252 }
261253 if (userName != null ) {
262- authName .setText (LoginController .extractValue (userName ));
263- authPassword .setText (LoginController .extractValue (userPassword ));
264-
254+ authName .setText (userName );
255+ authPassword .setText (userPassword );
265256 }
266- } else if (apistatus .equals ("OFF" )) {
267- radioApiOn .setSelected (false );
268- radioApiOff .setSelected (true );
257+ } else if (apiStatus .equals ("OFF" )) {
258+ activateRestApiCheckBox .setSelected (false );
269259 }
270260 }
271261 } catch (IOException e ) {
272- LOG .warn ("There is currently no application.properties available" );
262+ LOG .debug (
263+ "There is currently no additional '{}' file available. This is fine as it should only be present when rest-api is used." ,
264+ propertiesFilePath , e );
273265 }
274266
275267 LOG .debug ("saveButton.setOnAction" );
276268
277269 saveButton .setOnAction (ae -> {
278270 LOG .info ("Save clicked" );
279271
280- RadioButton selectedRadioButton = (RadioButton ) toggleGroup .getSelectedToggle ();
281-
282- if (selectedRadioButton == radioApiOff ) {
283- handleApiOff ();
284- } else if (selectedRadioButton == radioApiOn ) {
272+ if (activateRestApiCheckBox .isSelected ()) {
285273 handleApiOn ();
274+ } else {
275+ handleApiOff ();
286276 }
287277
288278 if (!OS .isWindows ()) {
@@ -334,9 +324,7 @@ private void initialize() {
334324 });
335325
336326 LOG .debug ("cancelButton.setOnAction" );
337- cancelButton .setOnAction (ae ->
338-
339- {
327+ cancelButton .setOnAction (ae -> {
340328 LOG .info ("Cancel clicked" );
341329 thisStage .close ();
342330 });
@@ -356,7 +344,6 @@ private void initialize() {
356344 }
357345
358346 private static void setRegionSvg (Region region , double requiredWidth , double requiredHeight , RESOURCE resource ) {
359-
360347 region .setShape (SvgNodeProvider .getSvgNodeWithScale (resource , 1.0 , 1.0 ));
361348 region .setMinSize (requiredWidth , requiredHeight );
362349 region .setPrefSize (requiredWidth , requiredHeight );
@@ -418,17 +405,16 @@ protected void updateItem(final String item, final boolean empty) {
418405 licenseTableView .getColumns ().add (nameColumn );
419406 licenseTableView .getColumns ().add (licenseColumn );
420407
421- LOG .debug ("hyperlink setonaction" );
422408 gitHubHyperlink .setOnAction (ae -> {
423- LOG .debug ("hyperlink clicked" );
424409 BrowserHelper .openURL (GITHUB_PAGE );
425410 });
426-
427- LOG .debug ("roportbugbutton setonaction" );
428411 reportBugButton .setOnAction (ae -> {
429- LOG .info ("Clicked reportBugButton" );
430412 BrowserHelper .openURL (GITHUB_ISSUE_PAGE );
431413 });
414+ swaggerHyperLink .setOnAction (ae -> {
415+ String port = authPort .getText ().isEmpty () ? "8080" : authPort .getText ();
416+ BrowserHelper .openURL ("http://localhost:" + port + "/api/swagger" );
417+ });
432418 }
433419
434420 private void initImportButton () {
@@ -444,9 +430,9 @@ private void initImportButton() {
444430 confirmationAlert .setHeaderText ("Do you want to Override current Data ?" );
445431 confirmationAlert .getDialogPane ().setContent (new Label ("""
446432 Import previously exported .sql file. This will overwrite the currently used database contents - all current data will be lost!
447-
433+
448434 If you do not have a .sql file yet you need to open the previous version of KeepTime and in the settings dialog press "Export".
449-
435+
450436 You will need to restart the application after this action. If you proceed you need to select the previous exported .sql file.\
451437 """ ));
452438 confirmationAlert .showAndWait ();
@@ -578,10 +564,6 @@ public void setStage(final Stage thisStage) {
578564 this .thisStage = thisStage ;
579565 }
580566
581- private FXMLLoader createFXMLLoader (final RESOURCE fxmlLayout ) {
582- return new FXMLLoader (Resources .getResource (fxmlLayout ));
583- }
584-
585567 public ObservableList <LicenseTableRow > loadLicenseRows () {
586568 final ObservableList <LicenseTableRow > licenseRows = FXCollections .observableArrayList ();
587569 licenseRows .add (new LicenseTableRow ("Open Sans" , Licenses .APACHEV2 ));
@@ -620,7 +602,7 @@ private void showLicense(final Licenses license) {
620602
621603 private void handleApiOff () {
622604 Map <String , String > propertiesToUpdate = new HashMap <>();
623- setWebApplicationType ( "none" );
605+ propertiesToUpdate . put ( "spring.main.web-application-type" , "none" );
624606 propertiesToUpdate .put ("api" , "OFF" );
625607 propertyWrite (propertiesToUpdate );
626608 }
@@ -629,53 +611,32 @@ private void handleApiOn() {
629611 String username = authName .getText ();
630612 String password = authPassword .getText ();
631613
632- LoginController loginController = new LoginController (username , password );
633-
634- loginController .createAndSaveUser ();
635-
636614 Map <String , String > propertiesToUpdate = new HashMap <>();
637615 propertiesToUpdate .put ("spring.main.web-application-type" , "" );
638616 propertiesToUpdate .put ("server.port" , authPort .getText ());
639617 propertiesToUpdate .put ("api" , "ON" );
618+ propertiesToUpdate .put ("spring.security.user.name" , username );
619+ propertiesToUpdate .put ("spring.security.user.password" , password );
640620
641- propertiesToUpdate .put ("spring.security.user.name" , "${BASIC_AUTH_USER:" + username + "}" );
642- propertiesToUpdate .put ("spring.security.user.password" , "${BASIC_AUTH_PASSWORD:" + password + "}" );
643621 propertyWrite (propertiesToUpdate );
644622 }
645623
646- private void setWebApplicationType (String value ) {
647- propertyWrite ("spring.main.web-application-type" , value );
648- }
649-
650- public void propertyWrite (String key , String value ) {
624+ private void propertyWrite (Map <String , String > propertiesToUpdate ) {
651625 Properties properties = new Properties ();
652- try (InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (propertiesFilePath );
653- FileOutputStream outputStream = new FileOutputStream (propertiesFilePath )) {
654626
627+ try (InputStream inputStream = new FileInputStream (propertiesFilePath )){
655628 properties .load (inputStream );
656- properties .setProperty (key , value );
657- properties .store (outputStream , null );
658-
659629 } catch (IOException e ) {
660- e . printStackTrace ( );
630+ LOG . debug ( "Could not open '{}' file. This is most likely fine as it was just not needed before and will be created next." , propertiesFilePath , e );
661631 }
662- }
663-
664- private void propertyWrite (Map <String , String > propertiesToUpdate ) {
665- Properties properties = new Properties ();
666- try (InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (propertiesFilePath );
667- FileOutputStream outputStream = new FileOutputStream (propertiesFilePath )) {
668-
669- properties .load (inputStream );
670632
633+ try (FileOutputStream outputStream = new FileOutputStream (propertiesFilePath )) {
671634 for (Map .Entry <String , String > entry : propertiesToUpdate .entrySet ()) {
672635 properties .setProperty (entry .getKey (), entry .getValue ());
673636 }
674-
675- properties .store (outputStream , null );
676-
637+ properties .store (outputStream , "REST-API settings" );
677638 } catch (IOException e ) {
678- e . printStackTrace ( );
639+ LOG . error ( "Error while persisting properties: '{}'." , propertiesToUpdate , e );
679640 }
680641 }
681642
0 commit comments