Skip to content

Commit c1e6ecc

Browse files
committed
Lab4 - done
1 parent 8ca983d commit c1e6ecc

31 files changed

+57
-18
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

lab04/lab-gui-gallery/build/resources/main/view/galleryView.fxml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<top>
1616
<HBox styleClass="form-box">
1717
<Label text="Search for photos:" prefWidth="120"/>
18+
<TextField fx:id="searchTextField" promptText="Enter search query..." prefWidth="580"/>
19+
<Button text="Search" onAction="#searchButtonClicked"/>
1820
</HBox>
1921
</top>
2022
<left>
Binary file not shown.

lab04/lab-gui-gallery/src/main/java/app/Main.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package app;
22

33
import javafx.application.Application;
4+
import util.PhotoDownloader;
45

56
public class Main {
67

7-
public static final String SCRAPER_API_KEY = "4c35963a373952b6ed2f344438750b96";
8+
public static final String SCRAPER_API_KEY = "67e4389cbeb73e8c244f519e7342fd52";
89

910

1011
public static void main(String[] args) {

lab04/lab-gui-gallery/src/main/java/controller/GalleryController.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package controller;
22

33

4+
import javafx.application.Platform;
45
import javafx.collections.FXCollections;
56
import javafx.collections.ObservableList;
7+
import javafx.event.ActionEvent;
68
import javafx.fxml.FXML;
79

810
import javafx.scene.control.ListCell;
911
import javafx.scene.control.ListView;
1012
import javafx.scene.control.TextField;
13+
import javafx.scene.image.Image;
1114
import javafx.scene.image.ImageView;
1215
import model.Gallery;
1316
import model.Photo;
17+
import util.PhotoDownloader;
1418

1519

1620
public class GalleryController {
@@ -26,6 +30,9 @@ public class GalleryController {
2630
@FXML
2731
private ListView<Photo> imagesListView;
2832

33+
@FXML
34+
private TextField searchTextField;
35+
2936
@FXML
3037
public void initialize() {
3138
imagesListView.setCellFactory(param -> new ListCell<>() {
@@ -45,6 +52,10 @@ protected void updateItem(Photo item, boolean empty) {
4552
});
4653

4754
imagesListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
55+
if (oldValue != null) {
56+
this.imageNameField.textProperty().unbindBidirectional(oldValue.nameProperty());
57+
}
58+
4859
this.bindSelectedPhoto(newValue);
4960
});
5061
}
@@ -57,8 +68,20 @@ public void setModel(Gallery gallery) {
5768
}
5869

5970
private void bindSelectedPhoto(Photo selectedPhoto) {
60-
imageView.imageProperty().bind(selectedPhoto.imageProperty());
61-
imageNameField.textProperty().bind(selectedPhoto.nameProperty());
71+
if (selectedPhoto != null) {
72+
imageView.imageProperty().bind(selectedPhoto.imageProperty());
73+
imageNameField.textProperty().bindBidirectional(selectedPhoto.nameProperty());
74+
}
75+
}
76+
77+
public void searchButtonClicked(ActionEvent event) {
78+
PhotoDownloader photoDownloader = new PhotoDownloader();
79+
80+
this.galleryModel.clear();
81+
82+
photoDownloader.searchForPhotos("Agent Cooper").subscribe(photo -> {
83+
Platform.runLater(() -> galleryModel.addPhoto(photo));
84+
});
6285
}
6386
}
6487

lab04/lab-gui-gallery/src/main/java/model/Gallery.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
public class Gallery {
1010

11-
private final List<Photo> photos = new ArrayList<>();
11+
private final ObservableList<Photo> photos = FXCollections.observableArrayList();
1212

1313
public void addPhoto(Photo photo) {
1414
photos.add(photo);
1515
}
1616

1717
public ObservableList<Photo> getPhotos() {
18-
return FXCollections.observableArrayList(photos);
18+
return this.photos;
1919
}
2020

2121
public void clear() {

lab04/lab-gui-gallery/src/main/java/util/PhotoDownloader.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.InputStream;
1212
import java.net.URL;
1313
import java.net.URLConnection;
14+
import java.util.List;
1415
import java.util.logging.Level;
1516
import java.util.logging.Logger;
1617

@@ -32,16 +33,23 @@ public Observable<Photo> getPhotoExamples() {
3233
}
3334

3435
public Observable<Photo> searchForPhotos(String searchQuery) {
35-
return Observable.fromCallable(() -> DuckDuckGoDriver.searchForImages(searchQuery))
36-
.flatMap(Observable::fromIterable)
37-
.flatMap(photoUrl -> Observable.fromCallable(() -> getPhoto(photoUrl))
38-
.onErrorResumeNext(e -> {
39-
log.log(Level.WARNING, "Could not download a photo", e);
40-
return Observable.empty();
41-
})
42-
// Uncomment line below to download each photo in separate thread
43-
// .subscribeOn(Schedulers.computation())
44-
);
36+
Observable<Photo> source = Observable.create(observer -> {
37+
try {
38+
List<String> photoUrls = DuckDuckGoDriver.searchForImages(searchQuery);
39+
for (String url : photoUrls) {
40+
observer.onNext(getPhoto(url));
41+
}
42+
43+
observer.onComplete();
44+
45+
} catch (InterruptedException e) {
46+
observer.onError(e);
47+
} catch (IOException ignored) {
48+
observer.onComplete();
49+
}
50+
});
51+
52+
return source.subscribeOn(Schedulers.computation());
4553
}
4654

4755
private Photo getPhoto(String photoUrl) throws IOException {

lab04/lab-gui-gallery/src/main/java/util/PhotoSerializer.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ public PhotoSerializer(String photoLibraryPath) throws IOException {
2626
}
2727

2828
public void registerGallery(Gallery gallery) {
29-
3029
gallery.getPhotos().addListener((ListChangeListener<? super Photo>) change -> {
3130
while (change.next()) {
3231
if (change.wasAdded()) {
33-
System.out.println("saving...");
34-
change.getAddedSubList().forEach(this::savePhoto);
32+
change.getAddedSubList().forEach((photo -> {
33+
this.savePhoto(photo);
34+
photo.nameProperty().addListener((observable, oldValue, newValue) -> {
35+
renamePhoto(oldValue, newValue);
36+
});
37+
}));
3538
} else if (change.wasRemoved()) {
3639
change.getRemoved().forEach(this::removePhoto);
3740
}

lab04/lab-gui-gallery/src/main/resources/view/galleryView.fxml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<top>
1616
<HBox styleClass="form-box">
1717
<Label text="Search for photos:" prefWidth="120"/>
18+
<TextField fx:id="searchTextField" promptText="Enter search query..." prefWidth="580"/>
19+
<Button text="Search" onAction="#searchButtonClicked"/>
1820
</HBox>
1921
</top>
2022
<left>

0 commit comments

Comments
 (0)