diff --git a/10 - Adding tracks/data/tracks.txt b/10 - Adding tracks/data/tracks.txt new file mode 100644 index 0000000..40bd1ab Binary files /dev/null and b/10 - Adding tracks/data/tracks.txt differ diff --git a/10 - Adding tracks/src/controller/tracks/AddController.java b/10 - Adding tracks/src/controller/tracks/AddController.java index 5648083..f3ca914 100644 --- a/10 - Adding tracks/src/controller/tracks/AddController.java +++ b/10 - Adding tracks/src/controller/tracks/AddController.java @@ -9,6 +9,7 @@ import model.AppendingObjectOutputStream; import model.Track; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; @@ -39,25 +40,38 @@ public void initialize(URL url, ResourceBundle resourceBundle) { save.setOnAction(e -> { try { - saveTrack(); + addTrack(); + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + + cancel.setOnAction(e -> closeDialog()); + + /* + save.setOnAction(e -> { + try { + // saveTrack(); } catch (IOException ex) { ex.printStackTrace(); } }); cancel.setOnAction(e -> handleCancel()); + */ } - private void saveTrack () throws IOException { - // TODO: Validate data + @FXML + private void addTrack() throws IOException { Track track = new Track(title.getText(), artist.getText()); + String dataURL = rootURL + "/data/tracks.txt"; - if (fileExists(rootURL + "/data/tracks.txt")) { - AppendingObjectOutputStream aos = new AppendingObjectOutputStream(new FileOutputStream("data/tracks.txt", true)); - aos.writeObject(track); - aos.close(); + if (fileExists(dataURL)) { + AppendingObjectOutputStream as = new AppendingObjectOutputStream(new FileOutputStream(dataURL, true)); + as.writeObject(track); + as.close(); } else { - ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("data/tracks.txt")); + ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(dataURL)); os.writeObject(track); os.close(); } @@ -70,8 +84,8 @@ private void saveTrack () throws IOException { * Called when clear button is clicked. */ @FXML - private void handleCancel() { - dialog.close(); + private void closeDialog() { + this.dialog.close(); } public void setDialogStage(Stage dialog) { diff --git a/10 - Adding tracks/src/controller/tracks/TracksController.java b/10 - Adding tracks/src/controller/tracks/TracksController.java index ef475d5..e30c1d6 100644 --- a/10 - Adding tracks/src/controller/tracks/TracksController.java +++ b/10 - Adding tracks/src/controller/tracks/TracksController.java @@ -13,12 +13,10 @@ import javafx.scene.layout.GridPane; import javafx.stage.Modality; import javafx.stage.Stage; +import model.ErrorDialog; import model.Track; -import java.io.EOFException; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.ObjectInputStream; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.ResourceBundle; @@ -54,6 +52,11 @@ public class TracksController extends BaseController private ObservableList tracksList; private Track track; + ErrorDialog errorDialog; + + public TracksController() { + errorDialog = new ErrorDialog(); + } @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -71,7 +74,13 @@ public void initialize(URL url, ResourceBundle resourceBundle) { tracksList.add(new Track("Aj tak sme frajeri", "Peter Nagy, Indigo")); */ - syncTracks(); + try { + syncTracks(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } if (tracksList != null) { tracksTable.setItems(tracksList); @@ -101,46 +110,72 @@ private void showDetails(Track track) { } private void registerListeners () { - addTrack.defaultButtonProperty().bind(addTrack.focusedProperty()); - addTrack.setOnAction((e) -> showAdd()); - dashboard.defaultButtonProperty().bind(dashboard.focusedProperty()); dashboard.setOnAction((e) -> switchScene("dashboard")); logout.defaultButtonProperty().bind(logout.focusedProperty()); logout.setOnAction((e) -> logout()); + addTrack.defaultButtonProperty().bind(addTrack.focusedProperty()); + addTrack.setOnAction(e -> { + try { + showAddDialog(); + } catch (IOException ex) { + errorDialog.display("Chyba s oknami", "Nepodarilo sa otvorit nove okno"); + } + }); + refreshBtn.defaultButtonProperty().bind(refreshBtn.focusedProperty()); - refreshBtn.setOnAction((e) -> syncTracks()); + refreshBtn.setOnAction(e -> { + try { + syncTracks(); + } catch (IOException ex) { + ex.printStackTrace(); + } catch (ClassNotFoundException ex) { + ex.printStackTrace(); + } + }); } - /** - * Shows add dialog using fxml file - */ - public void showAdd() { + private void showAddDialog() throws IOException { + // System.out.println("Zobrazi sa Add Track Dialog okno"); FXMLLoader loader = new FXMLLoader(); - try { - loader.setLocation(getLocation("tracks/add")); + loader.setLocation(getLocation("tracks/add")); + GridPane pane = loader.load(); + Stage addDialog = new Stage(); + Scene scene = new Scene(pane); - GridPane pane = loader.load(); - Scene scene = new Scene(pane); - Stage addDialog = new Stage(); + AddController controller = loader.getController(); + controller.setDialogStage(addDialog); - addDialog.setTitle("Pridať skladbu"); - // addDialog.initOwner((Stage) trackTitle.getScene().getWindow()); + addDialog.setTitle("Pridať skladbu"); + addDialog.setScene(scene); + addDialog.showAndWait(); + } - AddController controller = loader.getController(); - controller.setDialogStage(addDialog); + private void syncTracks () throws IOException, ClassNotFoundException { + String dataURL = rootURL + "/data/tracks.txt"; - addDialog.setScene(scene); - addDialog.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); + if (fileExists(dataURL)) { + ObjectInputStream is = new ObjectInputStream(new FileInputStream(dataURL)); + + tracksTable.getItems().removeAll(tracksList); + + try { + while (true) { + Track track = (Track) is.readObject(); + System.out.println(track.getTitle() + ", " + track.getArtist()); + tracksList.add(track); + } + } catch (EOFException e) { + System.out.println("Koniec suboru"); + } + + tracksTable.setItems(tracksList); } - } - private void syncTracks () { + /* String fileURL = rootURL + "/data/tracks.txt"; if (fileExists(fileURL)) { @@ -163,5 +198,6 @@ private void syncTracks () { tracksTable.setItems(tracksList); } + */ } } diff --git a/10 - Adding tracks/src/view/tracks/add.fxml b/10 - Adding tracks/src/view/tracks/add.fxml index 5f06ee0..c367e8b 100644 --- a/10 - Adding tracks/src/view/tracks/add.fxml +++ b/10 - Adding tracks/src/view/tracks/add.fxml @@ -14,13 +14,13 @@