-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlbumImageProvider.java
85 lines (77 loc) · 3.03 KB
/
AlbumImageProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Vector;
class AlbumImageProvider extends SwingWorker<Vector<String>, Object> {
private final IMessageDisplay outputArea;
private final ConfigurationService configurationService;
private Album album;
public AlbumImageProvider(IMessageDisplay output, ConfigurationService configurationService) {
this.outputArea = output;
this.configurationService = configurationService;
}
public void setAlbum(Album album) {
this.album = album;
}
@Override
protected Vector<String> doInBackground() throws Exception {
// if not already loaded load images
if (this.album != null && !this.album.isInitiated()) {
//retrieveFakeData();
retrieveRealData();
}
if (this.album != null) {
return this.album.getImages();
}
return new Vector<String>();
}
private void retrieveFakeData() {
this.album.addImage("IMG_6762.jpg");
this.album.addImage("IMG_6763.jpg");
this.album.addImage("IMG_6764.jpg");
}
private void retrieveRealData() {
try {
// get last version info from internet
URL updateURL = new URL(this.configurationService.GetImageUrl() + this.album.getId());
Gson gson = new Gson();
JsonReader reader = new JsonReader(new InputStreamReader(updateURL.openStream()));
reader.beginArray();
while (reader.hasNext()) {
Image image = gson.fromJson(reader, Image.class);
this.album.addImage(image.getFilenameOrig());
}
reader.endArray();
reader.close();
} catch (FileNotFoundException e) {
this.outputArea.showMessage("Kein Zugang zum Internet gefunden." + "\n");
} catch (UnknownHostException e) {
this.outputArea.showMessage("Kein Zugang zum Internet gefunden." + "\n");
} catch (ConnectException e) {
this.outputArea.showMessage("Kein Zugang zum Internet gefunden." + "\n");
} catch (MalformedURLException e) {
this.outputArea.showMessage("Kein Zugang zum Internet gefunden." + "\n");
} catch (IOException e) {
this.outputArea.showMessage("Kein Zugang zum Internet gefunden." + "\n");
}
}
@Override
protected void done() {
try {
Vector<String> pics = get();
int i = 0;
for (String pic : pics) {
this.outputArea.showMessage(" " + i++ + ": " + pic + "\n", IMessageDisplay.VERBOSE);
}
this.outputArea.showMessage(+pics.size() + " bilder in album " + this.album.getName() + " gefunden\n");
} catch (Exception ignore) {
}
}
}