-
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.
Merge pull request #116 from einaskoi/per/fileHandling
Per/file handling
- Loading branch information
Showing
14 changed files
with
349 additions
and
119 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
15 changes: 15 additions & 0 deletions
15
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exceptions/StockFileParseException.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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Model.Exceptions; | ||
|
|
||
| public class StockFileParseException extends Exception { | ||
| private final int lineNumber; | ||
| private final String lineContent; | ||
|
|
||
| public StockFileParseException(int lineNumber, String lineContent, String reason) { | ||
| super(String.format("Parse error on line %d: %s%n Content : \"%s\"", lineNumber, reason, lineContent)); | ||
| this.lineNumber = lineNumber; | ||
| this.lineContent = lineContent; | ||
| } | ||
|
|
||
| public int getLineNumber() { return lineNumber; } | ||
| public String getLineContent() { return lineContent; } | ||
| } |
Oops, something went wrong.