-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix handling of csv files such that user selected file is formated, e…
…lse a warning with error lines will appear
- Loading branch information
Showing
11 changed files
with
241 additions
and
61 deletions.
There are no files selected for viewing
78 changes: 67 additions & 11 deletions
78
...java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/SettingsAppController.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,92 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Exchange; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.StockFileHandler; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.SettingsApp; | ||
| import java.nio.file.Path; | ||
| import java.util.Objects; | ||
|
|
||
| public class SettingsAppController implements AppController { | ||
|
|
||
| private SettingsApp settingsApp; | ||
| private Path userSelectedPath; | ||
| private Runnable onUserSelection; | ||
| private Runnable onSelectedFileFailed; | ||
| private String errorMessage; | ||
|
|
||
| public SettingsAppController(SettingsApp settingsApp) { | ||
| private final SettingsApp settingsApp; | ||
|
|
||
| public SettingsAppController(final SettingsApp settingsApp) { | ||
| this.settingsApp = settingsApp; | ||
| userSelectedPath = loadDefaultPath(); | ||
| refreshView(); | ||
|
|
||
| if (!settingsApp.isLoggedIn()) { | ||
| settingsApp.getOpenFileChooserButton().setOnAction(event -> { | ||
| userSelectedPath = settingsApp.getFileChooser().showOpenDialog(settingsApp.getRoot().getScene().getWindow()).toPath(); | ||
| System.out.println(userSelectedPath.toString()); | ||
| onUserSelection.run(); | ||
| }); | ||
| settingsApp.getOpenFileChooserButton().setOnAction(event -> openFileChooser()); | ||
| } | ||
| } | ||
|
|
||
| private void openFileChooser() { | ||
| var file = settingsApp.getFileChooser() | ||
| .showOpenDialog(settingsApp.getRoot().getScene().getWindow()); | ||
|
|
||
| if (file == null) { | ||
| return; // user cancelled — keep current path | ||
| } | ||
|
|
||
| Path selected = file.toPath(); | ||
|
|
||
| if (isValidStockFile(selected)) { | ||
| userSelectedPath = selected; | ||
| onUserSelection.run(); | ||
| } else { | ||
| onSelectedFileFailed.run(); | ||
| userSelectedPath = loadDefaultPath(); | ||
| } | ||
|
|
||
| refreshView(); | ||
| } | ||
|
|
||
| private boolean isValidStockFile(final Path path) { | ||
| try { | ||
| new Exchange(StockFileHandler.readFromFile(path)); | ||
| return true; | ||
| } catch (Exception e) { | ||
| errorMessage = e.getMessage(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private Path loadDefaultPath() { | ||
| try { | ||
| return Path.of(Objects.requireNonNull( | ||
| getClass().getClassLoader().getResource("stocks.csv")).toURI()); | ||
| } catch (Exception e) { | ||
| System.err.println("Failed to load default stocks.csv: " + e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private void refreshView() { | ||
| settingsApp.setPath(userSelectedPath); | ||
| settingsApp.updateContent(); | ||
| } | ||
|
|
||
| public Path getUserSelectedPath() { | ||
| return userSelectedPath; | ||
| } | ||
|
|
||
| public void setOnUserPathSelected(Runnable callback) { | ||
| public void setOnUserPathSelected(final Runnable callback) { | ||
| onUserSelection = callback; | ||
| } | ||
|
|
||
| @Override | ||
| public void nextTick() { | ||
| public void setOnSelectedFileFailed(final Runnable callback) { | ||
| onSelectedFileFailed = callback; | ||
| } | ||
|
|
||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void nextTick() {} | ||
| } |
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
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
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
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
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
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
Oops, something went wrong.