Skip to content

Commit

Permalink
10th lesson - adding new tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandervalach committed Apr 4, 2020
1 parent 5c587a1 commit 7d6b6c9
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 102 deletions.
Binary file added 10 - Adding tracks/data/tracks.txt
Binary file not shown.
34 changes: 24 additions & 10 deletions 10 - Adding tracks/src/controller/tracks/AddController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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) {
Expand Down
92 changes: 64 additions & 28 deletions 10 - Adding tracks/src/controller/tracks/TracksController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +52,11 @@ public class TracksController extends BaseController

private ObservableList<Track> tracksList;
private Track track;
ErrorDialog errorDialog;

public TracksController() {
errorDialog = new ErrorDialog();
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -163,5 +198,6 @@ private void syncTracks () {
tracksTable.setItems(tracksList);
}
*/
}
}
8 changes: 4 additions & 4 deletions 10 - Adding tracks/src/view/tracks/add.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

<Label id="banner" alignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="3"/>

<Label text="Názov" alignment="CENTER" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
<Label text="Názov" alignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="title" GridPane.rowIndex="1" GridPane.columnIndex="2" GridPane.columnSpan="2"/>

<Label text="Interprét" alignment="CENTER" GridPane.rowIndex="5" GridPane.columnIndex="1"/>
<Label text="Interprét" alignment="CENTER" GridPane.columnIndex="5" GridPane.rowIndex="1" />
<TextField fx:id="artist" GridPane.rowIndex="5" GridPane.columnIndex="2" GridPane.columnSpan="2"/>

<Button fx:id="save" prefWidth="130" text="Uložiť" GridPane.rowIndex="8" GridPane.columnIndex="2"/>
<Button fx:id="cancel" prefWidth="130" text="Zrušiť" GridPane.rowIndex="8" GridPane.columnIndex="3"/>
<Button fx:id="save" prefWidth="130" text="Uložiž" GridPane.rowIndex="8" GridPane.columnIndex="2" />
<Button fx:id="cancel" prefWidth="130" text="Zrušiť" GridPane.rowIndex="8" GridPane.columnIndex="3" />

</GridPane>
4 changes: 2 additions & 2 deletions 10 - Adding tracks/src/view/tracks/all.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

<HBox spacing="10" alignment="bottom_left" GridPane.columnIndex="0"
GridPane.rowIndex="2" GridPane.columnSpan="2" minHeight="50">
<Button fx:id="addTrack" prefWidth="120" text="+"/>
<Button fx:id="refreshBtn" prefWidth="120" text="Načítať"/>
<Button fx:id="addTrack" prefWidth="120" text="Pridať skladbu" />
<Button fx:id="refreshBtn" prefWidth="120" text="Načítať" />
<Button fx:id="dashboard" prefWidth="120" text="Domov"/>
<Button fx:id="logout" prefWidth="120" text="Odhlásiť sa"/>
</HBox>
Expand Down
23 changes: 8 additions & 15 deletions 7 - TableView/src/controller/TracksController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.GridPane;
import model.Track;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class TracksController extends BaseController
{
@FXML
private TableView<Track> tracksTable;
private TableView tracksTable;

@FXML
private TableColumn<Track, String> title;
Expand All @@ -30,29 +28,24 @@ public class TracksController extends BaseController
@FXML
private Button logout;

private ObservableList<Track> tracksList;
ObservableList<Track> tracksList;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
super.initialize(url, resourceBundle);

public TracksController () {
tracksList = FXCollections.observableArrayList();
tracksList.add(new Track("Tame Me Home, Country Roads", "John Denver"));
tracksList.add(new Track("Iridescent", "Linkin Park"));
tracksList.add(new Track("Three Little Birds", "Bob Marley & The Wailers"));
tracksList.add(new Track("Aj tak sme frajeri", "Peter Nagy, Indigo"));
}
tracksList.add(new Track("Barbie girl", "Aqua"));

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
super.initialize(url, resourceBundle);
tracksTable.setItems(tracksList);

title.setCellValueFactory(cellData -> cellData.getValue().getTitleProperty());
artist.setCellValueFactory(cellData -> cellData.getValue().getArtistProperty());

if (tracksList != null) {
tracksTable.setItems(tracksList);
} else {
tracksTable.managedProperty().bind(tracksTable.visibleProperty());
}

registerListeners();
}

Expand Down
13 changes: 4 additions & 9 deletions 7 - TableView/src/model/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/***
* Definujeme si najprv model Track
*/
public class Track {
// Attribúty nech sú typu String
private String title;
private String artist;

public Track () {

}

public Track(String title, String artist) {
this.title = title;
this.artist = artist;
Expand All @@ -20,9 +20,7 @@ public String getTitle() {
return title;
}

// Do tabuľky vstupuje StringProperty
public StringProperty getTitleProperty() {
// StringProperty je abstraktná trieda, preto ju inicializujeme pomocou SimpleStringProperty - konkrétna trieda
return new SimpleStringProperty(title);
}

Expand All @@ -34,14 +32,11 @@ public String getArtist() {
return artist;
}

// Do tabuľky vstupuje StringProperty
public StringProperty getArtistProperty() {
// StringProperty je abstraktná trieda, preto ju inicializujeme pomocou SimpleStringProperty - konkrétna trieda
return new SimpleStringProperty(artist);
}

public void setArtist(String artist) {
this.artist = artist;
}

}
22 changes: 2 additions & 20 deletions 7 - TableView/src/view/Tracks/All.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@

<TableView fx:id="tracksTable" GridPane.rowIndex="1" GridPane.columnIndex="1">
<columns>
<TableColumn fx:id="title" prefWidth="300" text="Názov"/>
<TableColumn fx:id="artist" prefWidth="300" text="Interprét"/>
<TableColumn fx:id="title" prefWidth="200" text="Názov skladby" />
<TableColumn fx:id="artist" prefWidth="200" text="Interprét" />
</columns>
</TableView>

<!--
<AnchorPane GridPane.rowIndex="1" GridPane.columnIndex="2">
<GridPane vgap="10.0" hgap="10.0">
<Label text="Odosielateľ - prijímateľ:" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label fx:id="lblReciever" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Label text="Suma:" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Label fx:id="lblSum" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Label text="Dátum:" GridPane.columnIndex="1" GridPane.rowIndex="3"/>
<Label fx:id="lblDate" GridPane.columnIndex="2" GridPane.rowIndex="3"/>
<Label text="Poznámka:" GridPane.columnIndex="1" GridPane.rowIndex="4"/>
<Label fx:id="lblNote" GridPane.columnIndex="2" GridPane.rowIndex="4"/>
</GridPane>
</AnchorPane>
-->

<HBox spacing="10" alignment="bottom_left" GridPane.columnIndex="0"
GridPane.rowIndex="2" GridPane.columnSpan="2" minHeight="50">
<Button fx:id="dashboard" prefWidth="120" text="Domov"/>
Expand Down
Loading

0 comments on commit 7d6b6c9

Please sign in to comment.