From 7d6b6c9831765e1d1defa81ee223f0b8a66ceaa3 Mon Sep 17 00:00:00 2001 From: Alexander Valach Date: Sat, 4 Apr 2020 14:36:47 +0200 Subject: [PATCH] 10th lesson - adding new tracks --- 10 - Adding tracks/data/tracks.txt | Bin 0 -> 196 bytes .../src/controller/tracks/AddController.java | 34 +++++-- .../controller/tracks/TracksController.java | 92 ++++++++++++------ 10 - Adding tracks/src/view/tracks/add.fxml | 8 +- 10 - Adding tracks/src/view/tracks/all.fxml | 4 +- .../src/controller/TracksController.java | 23 ++--- 7 - TableView/src/model/Track.java | 13 +-- 7 - TableView/src/view/Tracks/All.fxml | 22 +---- .../src/controller/BaseController.java | 10 +- .../controller/tracks/TracksController.java | 18 +++- 9 - Track detail/src/view/login.fxml | 4 +- 9 - Track detail/src/view/tracks/all.fxml | 11 +++ 12 files changed, 137 insertions(+), 102 deletions(-) create mode 100644 10 - Adding tracks/data/tracks.txt diff --git a/10 - Adding tracks/data/tracks.txt b/10 - Adding tracks/data/tracks.txt new file mode 100644 index 0000000000000000000000000000000000000000..40bd1ab9ec3718a9894c8e9bd6aeae18c546346f GIT binary patch literal 196 zcmZ4UmVvdnh=DsdKP5FsFQh0jIs4{rZNByWwvJ2;Og;>3iA5!u#U&*SLOxlEWr_MZ ziFxVz!6ikRdFj?Z46G%YB{`{u40Q~Q6$K>>++O(^c?vG6d1a|ZB@DdI`K5U!MU@Ic d`H3mTl~l8Z#W5) { 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 @@