source: tags/gsdl-2_70u-distribution/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/GsdlCollageApplet.java@ 11745

Last change on this file since 11745 was 11716, checked in by kjdon, 18 years ago

committed Shaoquns version of the applet for the branch - version 1.10

  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1package org.nzdl.gsdl.GsdlCollageApplet;
2
3import java.applet.Applet;
4import java.awt.*;
5import java.net.*;
6
7/**
8 *
9 * @author Katrina Edgar
10 * @author David Bainbridge
11 *
12 * Main class for the GsdlCollageApplet<br>
13 * Processes the parameters passed through the Applet<br>
14 * Builds an appropriate starting url from these parameters<br>
15 * Creates a storage class for downloaded images<br>
16 * Starts thread to download images and their associated url<br>
17 * Starts thread to display downloaded images on the applet screen<br>
18 */
19
20public class GsdlCollageApplet extends Applet {
21
22
23 /** Amount of error checking output produced <br>
24 * Ranges from 0 - no output to 3 - maximum output */
25 protected int verbosity_ = 0;
26 /** Indicates whether java2 functionality should be used <br>
27 * If true will allow advanced image processing techniques to occur,
28 * such as fading and colouring using pixel manipulation <br>
29 * If false, images will maintain an alpha value of 1 (appear solid),
30 * newer images will simply be pasted on top of existing images <br> */
31 protected boolean isJava2_ = true;
32 /** Number of nested links to follow When used with greenstone,
33 * controls to which level of the document links will be followed For
34 * example a maximum depth of 1 will mean only the top page of a
35 * document will be examined, while a depth of 2 will include sections
36 * within this document, 3 includes sections within sections, and so
37 * on */
38 protected int maxDepth_ = 3;
39
40 /** Maximum number of images to display on the canvas */
41 protected int maxDisplay_ = 25;
42
43 /** Maximum number of downloaded images to store <br> Prevents applet
44 * from using excessive amounts of bandwidth by downloading too many
45 * images simulataneously*/
46 protected int maxDownloads_ = Integer.MAX_VALUE;
47
48 /** Types of images permitted in the collage, for example gif, jpg,
49 * png... */
50 protected String imageType_ = ".jpg%.png";
51
52 /** Caption of the collage */
53 protected String caption_ = "";
54
55 /** Background color of applet screen */
56 protected Color bgcolor_ = new Color(150, 193, 155);
57
58 /** Time lapse between repainting of the applet */
59 protected int refreshDelay_ = 2000;
60
61 /** Stores an image and url pair and provides associated methods */
62 protected DownloadImages download_images_ = null;
63 /** Downloads images from a starting url, recursively follows nested
64 * links */
65 protected DownloadUrls download_thread_ = null;
66 /** Image processing and placement on applet screen */
67 protected DisplayImages display_thread_ = null;
68
69
70
71 /** Gets verbosity */
72 public int verbosity() { return verbosity_; }
73 /** Gets maximum depth for nested links */
74 public int maxDepth() { return maxDepth_; }
75
76 /** Gets maximum number of images to display */
77 public int maxDisplay() { return maxDisplay_;}
78
79 /** Gets maximum number of images to store during downloading */
80 public int maxDownloads() { return maxDownloads_; }
81 /** Gets the refresh delay used between repaint */
82 public int refreshDelay() { return refreshDelay_; }
83
84 /** Gets parameters from the applet code and stores locally.
85 * Forms a starting url for image retrieval to begin from.
86 * The starting url is formed either by:
87 * * Using the image_url parameter as provided, which is assumed to
88 * be a complete url
89 * * If this parameter does not exist, the assumption is that the
90 * collage is being incorporated with the Greenstone Digital Library
91 * Software. The starting url is formed by concatenating
92 * the gwcgi, collection and classifier parameters as provided.
93 * Then starts downloading and displaying images */
94
95 Thread paint ;
96
97 public void init()
98 {
99 if (verbosity_ >= 4) {
100 System.err.println("Attempting to retrieve parameters...");
101 }
102
103 // gets the parameters specified
104 String verbosity_param = getParameter("verbosity");
105 String is_java2_param = getParameter("isJava2");
106 String max_depth_param = getParameter("maxDepth");
107 String max_downloads_param = getParameter("maxDownloads");
108 String max_display_param = getParameter("maxDisplay");
109 String refresh_delay_param = getParameter("refreshDelay");
110 String image_type_param = getParameter("imageType");
111 String bgcolor_param = getParameter("bgcolor");
112 String caption_param = getParameter("caption");
113 String document_root = getParameter("documentroot");
114 String gwcgi = getParameter("gwcgi");
115
116
117 // Check it isn't null
118 if ((verbosity_param!=null) && (!verbosity_param.startsWith("_"))) {
119 verbosity_ = Integer.parseInt(verbosity_param);
120 }
121 else {
122 verbosity_ = 1;
123 }
124
125
126 if (verbosity_ >= 4) {
127 System.err.println("Got parameters.");
128 }
129
130 if (caption_param != null && !caption_param.startsWith("_")) {
131 caption_ = caption_param;
132 }
133 else {
134 if (verbosity_ >= 4) {
135 System.err.println("No Caption: setting to a space.");
136 }
137 caption_ = " ";
138 }
139
140 if ((bgcolor_param != null) && (!bgcolor_param.startsWith("_"))) {
141 if (bgcolor_param.startsWith("#")) {
142 bgcolor_ = Color.decode(bgcolor_param);
143 }
144 else {
145 String [] c = bgcolor_param.split(",");
146 if (c.length == 3)
147 bgcolor_ = new Color(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2]));
148 }
149 if (verbosity_ >= 4){
150 System.err.println("Set BG to be " + bgcolor_.toString());
151 }
152 }
153 else {
154 if (verbosity_ >= 4) {
155 System.err.println("No BG: setting to NZDL green.");
156 }
157 bgcolor_ = new Color(150, 193, 155);
158 }
159
160 if ((image_type_param != null) && (!image_type_param.startsWith("_"))) {
161 imageType_ = image_type_param;
162 }
163
164 if ((is_java2_param == null) || (is_java2_param.equals("auto")) || (is_java2_param.startsWith("_"))) {
165 String version = System.getProperty("java.version");
166 version = version.substring(0, version.lastIndexOf("."));
167 System.err.println("VERSION: " + version);
168 float ver = (new Float(version)).floatValue();
169 isJava2_ = (ver >= 1.2) ? true : false;
170 }
171 else {
172 isJava2_ = (is_java2_param.equals("true")) ? true : false;
173 }
174
175 if ((max_depth_param != null) && (!max_depth_param.startsWith("_"))) {
176 // System.err.println("maxDepth = " + max_depth_param);
177 maxDepth_ = Integer.parseInt(max_depth_param);
178 }
179
180 if ((max_downloads_param!=null) && (!max_downloads_param.startsWith("_"))) {
181 maxDownloads_ = Integer.parseInt(max_downloads_param);
182 System.out.println(" maxDownloads " + maxDownloads_ );
183 maxDownloads_=50;
184 }
185
186 if ((max_display_param!=null) && (!max_display_param.startsWith("_"))) {
187 maxDisplay_ = Integer.parseInt(max_display_param);
188
189 }
190
191 if ((refresh_delay_param!=null) && (!refresh_delay_param.startsWith("_"))) {
192 refreshDelay_ = Integer.parseInt(refresh_delay_param);
193 }
194
195
196 if (document_root !=null){
197 document_root = document_root.substring(1);
198 if (document_root.indexOf("/") > 0 ){
199 document_root = document_root.substring(0, document_root.indexOf("/"));
200 }
201 else{
202 document_root = gwcgi;
203 }
204 }
205
206
207 String image_url = getParameter("imageURL");
208 String image_ignore = getParameter("imageIgnorePrefix");
209 String href_musthave = getParameter("hrefMustHave");
210 String image_mustnothave = getParameter("imageMustNotHave");
211
212 // builds starting url when incorporated with Greenstone
213 if (image_url==null)
214 {
215 String gwcgi_param = getParameter("gwcgi");
216 gwcgi_param = tidy_URL(gwcgi_param, true);
217
218 String collection_param = getParameter("collection");
219 String classifier_param = getParameter("classifier");
220
221 image_url = gwcgi_param + "a=d";
222 image_url += "&c=" + collection_param;
223 image_url += "&cl=" + classifier_param;
224 }
225
226 MediaTracker trk = new MediaTracker(this);
227
228 // creates a class to store the image and it's associated url
229 download_images_ = new DownloadImages( this, verbosity_,isJava2_ );
230 // starts the download image thread with the starting url
231
232 download_thread_ = new DownloadUrls(this, download_images_,
233 image_url, href_musthave, image_mustnothave,
234 image_ignore, imageType_,document_root,verbosity_,trk);
235 // starts the display image thread with the currently downloaded images
236 display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
237
238 }
239
240
241 /** Goes to the url associated with the image that is clicked on screen<br>
242 * Displays the url containing the image in a new window */
243 public boolean mouseDown(Event event, int x, int y)
244 {
245 // determines which image was clicked on
246 CollageImage cimage = display_thread_.clickedOnImage(x,y);
247 // if they were clicking on an image (as opposed to background)
248 if (cimage != null)
249 {
250 System.err.println("Click on image: from url = " + cimage.from_url_);
251 try {
252 // displays the associated url in a new window
253 URL from_url = null;
254
255 java.util.regex.Pattern p = java.util.regex.Pattern.compile("cl=CL\\d(\\.\\d)*\\s");
256 java.util.regex.Matcher m = p.matcher(cimage.from_url_.trim()+" ");
257
258 if (m.find() ){
259 from_url = cimage.url_;
260 }
261 else{
262 from_url = new URL(cimage.from_url_ + "#" + cimage.name_);
263 }
264 getAppletContext().showDocument(from_url,"gsdlDoc");
265 }
266 catch (MalformedURLException e) {
267 e.printStackTrace();
268 }
269
270 }
271 return true;
272 }
273
274 /** Start threads for downloading and displaying images */
275 public void start()
276 {
277
278 download_thread_.start();
279
280 display_thread_.start();
281
282 paint = new Thread(new Runnable(){
283 public void run() {
284 try {
285
286 Thread curr_thread = Thread.currentThread();
287
288 while (curr_thread == paint) {
289
290 repaint();
291
292 Thread.sleep(2000);
293 curr_thread = Thread.currentThread();
294 }
295
296
297 } catch (Exception e) {
298
299 }
300 }
301
302
303 });
304
305 paint.start();
306
307 }
308
309 /** Stops threads for downloading and displaying images */
310 public void stop()
311 {
312 download_thread_.stop();
313 display_thread_.stop();
314
315 download_thread_ = null;
316 display_thread_ = null;
317
318 }
319
320 /** Destroys threads for downloading and displaying images */
321 public void destroyed()
322 {
323 download_thread_ = null;
324
325 display_thread_ = null;
326
327 }
328
329
330 /** Repaints the applet */
331 public void update(Graphics g)
332 {
333 // System.err.println("Update called");
334 paint(g);
335 }
336
337 /** Repaints the applet using the paint method of the thread that is
338 * currently displaying images */
339 public void paint(Graphics g) {
340 if (display_thread_!=null)
341 {
342
343 //display_thread_.display_collage();
344 display_thread_.paint(g);
345 }
346 else
347 {
348 System.err.println("Applet still trying to paint!!");
349 }
350
351
352
353 }
354
355
356
357 /** Ensures a URL address (as string) has a protocol, host, and file.
358 *
359 * If the URL is a CGI script URL, it should be tidied up so that it is
360 * appropriate to tage attrib=value pairs on the end. This means it
361 * must either end with a "?" or (if it contains a question-mark
362 * internally) end with a "&". */
363 String tidy_URL(String address, boolean isCGI) {
364
365 // System.err.println("tidy URL: " + address);
366
367 // make sure the URL has protocol, host, and file
368 if (address.startsWith("http")) {
369 // the address has all the necessary components
370 } else if (address.startsWith("/")) {
371 // there is not protocol and host
372 URL document = getDocumentBase();
373 String port = "";
374 if (document.getPort()!=-1) {
375 port = ":" + document.getPort();
376 }
377 address = "http://" + document.getHost() + port + address;
378 } else {
379 // this URL is relative to the directory the document is in
380 URL document = getDocumentBase();
381 String directory = document.getFile();
382 int end = directory.lastIndexOf('/');
383 String port = "";
384 if (document.getPort()!=-1) {
385 port = ":" + document.getPort();
386 }
387 directory = directory.substring(0,end + 1);
388 address = "http://" + document.getHost() + port + directory + address;
389
390 }
391
392 // if the URL is a cgi script, make sure it has a "?" in ti,
393 // and that it ends with a "?" or "&"
394 if (isCGI) {
395 if (address.indexOf((int) '?') == -1) {
396 address = address + "?";
397 } else if (!address.endsWith("?")) {
398 address = address + "&";
399 }
400 }
401
402 return address;
403 }
404
405
406}
Note: See TracBrowser for help on using the repository browser.