]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/AsyncMediaLoader.java
Version 13.4, May 2012
[GpsPrune.git] / tim / prune / function / AsyncMediaLoader.java
1 package tim.prune.function;
2
3 import java.io.File;
4
5 import tim.prune.App;
6 import tim.prune.DataSubscriber;
7 import tim.prune.GenericFunction;
8 import tim.prune.I18nManager;
9 import tim.prune.UpdateMessageBroker;
10 import tim.prune.data.AudioClip;
11 import tim.prune.data.MediaObject;
12 import tim.prune.data.Photo;
13 import tim.prune.data.Track;
14 import tim.prune.load.MediaHelper;
15 import tim.prune.load.MediaLoadProgressDialog;
16 import tim.prune.undo.UndoLoadAudios;
17 import tim.prune.undo.UndoLoadPhotos;
18
19 /**
20  * Function to load media asynchronously,
21  * either from inside a zip/kmz file or remotely
22  */
23 public class AsyncMediaLoader extends GenericFunction
24 implements Runnable, Cancellable
25 {
26         /** Archive from which points were loaded */
27         private File _zipFile = null;
28         /** Array of links */
29         private String[] _linkArray = null;
30         /** Track to use for connecting */
31         private Track _track = null;
32         /** Source file */
33         private File _sourceFile = null;
34         /** Cancelled flag */
35         private boolean _cancelled = false;
36
37
38         /**
39          * Constructor
40          * @param inApp App object
41          * @param inLinkArray array of links
42          * @param inTrack Track object for connecting points
43          * @param inSourceFile file from which data was loaded, if any
44          */
45         public AsyncMediaLoader(App inApp, File inZipFile, String[] inLinkArray, Track inTrack, File inSourceFile)
46         {
47                 super(inApp);
48                 _zipFile = inZipFile;
49                 _linkArray = inLinkArray;
50                 _track = inTrack;
51                 _sourceFile = inSourceFile;
52         }
53
54         /**
55          * Begin the load
56          */
57         public void begin()
58         {
59                 _cancelled = false;
60                 if (_linkArray != null)
61                         new Thread(this).start();
62         }
63
64         /** Cancel */
65         public void cancel() {
66                 _cancelled = true;
67         }
68
69         /**
70          * @return the name key
71          */
72         public String getNameKey() {
73                 return "function.asyncmediaload";
74         }
75
76         /**
77          * Execute the load in a separate thread
78          */
79         public void run()
80         {
81                 // Count links first so that progress bar can be shown
82                 int numLinks = 0;
83                 for (int i=0; i<_linkArray.length; i++) {
84                         if (_linkArray[i] != null) {
85                                 numLinks++;
86                         }
87                 }
88                 if (numLinks <= 0) return;
89                 // Make progress dialog
90                 MediaLoadProgressDialog progressDialog = new MediaLoadProgressDialog(_app.getFrame(), this);
91
92                 // Make array to store results
93                 MediaObject[] media = new MediaObject[numLinks];
94                 int currLink = 0;
95                 for (int i=0; i<_linkArray.length && !_cancelled; i++)
96                 {
97                         if (_linkArray[i] != null)
98                         {
99                                 MediaObject mf = MediaHelper.createMediaObject(_zipFile, _linkArray[i], _sourceFile);
100                                 if (mf != null)
101                                 {
102                                         // attach media to point and set status
103                                         _track.getPoint(i).attachMedia(mf);
104                                         mf.setOriginalStatus(MediaObject.Status.TAGGED);
105                                         mf.setCurrentStatus(MediaObject.Status.TAGGED);
106                                         media[currLink] = mf;
107                                         // update progress
108                                         if (!_app.isBusyLoading())
109                                                 progressDialog.showProgress(currLink, numLinks);
110                                         currLink++;
111                                 }
112                                 try {Thread.sleep(100);} catch (InterruptedException ie) {}
113                         }
114                 }
115                 progressDialog.close();
116
117                 // Wait until App is ready to receive media (may have to ask about append/replace etc)
118                 waitUntilAppReady();
119
120                 // Go through the loaded media and check if the points are still in the track
121                 int numPhotos = 0, numAudios = 0;
122                 for (currLink=0; currLink<numLinks; currLink++)
123                 {
124                         MediaObject mo = media[currLink];
125                         if (mo != null && _track.containsPoint(mo.getDataPoint()))
126                         {
127                                 if (mo instanceof Photo)
128                                 {
129                                         _app.getTrackInfo().getPhotoList().addPhoto((Photo) mo);
130                                         numPhotos++;
131                                 }
132                                 else if (mo instanceof AudioClip)
133                                 {
134                                         _app.getTrackInfo().getAudioList().addAudio((AudioClip) mo);
135                                         numAudios++;
136                                 }
137                         }
138                 }
139                 // Confirm and update
140                 if (numPhotos > 0) {
141                         _app.completeFunction(new UndoLoadPhotos(numPhotos, 0), "" + numPhotos + " " +
142                                 I18nManager.getText(numPhotos == 1?"confirm.jpegload.single":"confirm.jpegload.multi"));
143                 }
144                 if (numAudios > 0) {
145                         _app.completeFunction(new UndoLoadAudios(numAudios), I18nManager.getText("confirm.audioload"));
146                 }
147                 UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_ADDED_OR_REMOVED);
148         }
149
150
151         /**
152          * Wait until the App is ready
153          */
154         private void waitUntilAppReady()
155         {
156                 long waitInterval = 500; // milliseconds
157                 while (_app.isBusyLoading())
158                 {
159                         try {Thread.sleep(waitInterval);} catch (InterruptedException ie) {}
160                         waitInterval *= 1.2;
161                 }
162         }
163 }