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