source: trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/GsdlCollageApplet.java@ 11420

Last change on this file since 11420 was 11420, checked in by shaoqun, 18 years ago

add code to get the greenstone home directory

  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1package org.nzdl.gsdl.GsdlCollageApplet;
2
3import java.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_ = 1500;
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 protected Display display_ = null;
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 public void init()
95 {
96 if (verbosity_ >= 4) {
97 System.err.println("Attempting to retrieve parameters...");
98 }
99
100 // gets the parameters specified
101 String verbosity_param = getParameter("verbosity");
102 String is_java2_param = getParameter("isJava2");
103 String max_depth_param = getParameter("maxDepth");
104 String max_downloads_param = getParameter("maxDownloads");
105 String max_display_param = getParameter("maxDisplay");
106 String refresh_delay_param = getParameter("refreshDelay");
107 String image_type_param = getParameter("imageType");
108 String bgcolor_param = getParameter("bgcolor");
109 String caption_param = getParameter("caption");
110 String document_root = getParameter("documentroot");
111
112
113 // Check it isn't null
114 if ((verbosity_param!=null) && (!verbosity_param.startsWith("_"))) {
115 verbosity_ = Integer.parseInt(verbosity_param);
116 }
117 else {
118 verbosity_ = 1;
119 }
120
121 if (verbosity_ >= 4) {
122 System.err.println("Got parameters.");
123 }
124
125 if (caption_param != null && !caption_param.startsWith("_")) {
126 caption_ = caption_param;
127 }
128 else {
129 if (verbosity_ >= 4) {
130 System.err.println("No Caption: setting to a space.");
131 }
132 caption_ = " ";
133 }
134
135 if ((bgcolor_param != null) && (!bgcolor_param.startsWith("_"))) {
136 if (bgcolor_param.startsWith("#")) {
137 bgcolor_ = Color.decode(bgcolor_param);
138 }
139 else {
140 String [] c = bgcolor_param.split(",");
141 if (c.length == 3)
142 bgcolor_ = new Color(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2]));
143 }
144 if (verbosity_ >= 4){
145 System.err.println("Set BG to be " + bgcolor_.toString());
146 }
147 }
148 else {
149 if (verbosity_ >= 4) {
150 System.err.println("No BG: setting to NZDL green.");
151 }
152 bgcolor_ = new Color(150, 193, 155);
153 }
154
155 if ((image_type_param != null) && (!image_type_param.startsWith("_"))) {
156 imageType_ = image_type_param;
157 }
158
159 if ((is_java2_param == null) || (is_java2_param.equals("auto")) || (is_java2_param.startsWith("_"))) {
160 String version = System.getProperty("java.version");
161 version = version.substring(0, version.lastIndexOf("."));
162 System.err.println("VERSION: " + version);
163 float ver = (new Float(version)).floatValue();
164 isJava2_ = (ver >= 1.2) ? true : false;
165 }
166 else {
167 isJava2_ = (is_java2_param.equals("true")) ? true : false;
168 }
169
170 if ((max_depth_param != null) && (!max_depth_param.startsWith("_"))) {
171 // System.err.println("maxDepth = " + max_depth_param);
172 maxDepth_ = Integer.parseInt(max_depth_param);
173 }
174
175 if ((max_downloads_param!=null) && (!max_downloads_param.startsWith("_"))) {
176 maxDownloads_ = Integer.parseInt(max_downloads_param);
177 }
178
179 if ((max_display_param!=null) && (!max_display_param.startsWith("_"))) {
180 maxDisplay_ = Integer.parseInt(max_display_param);
181 }
182
183 if ((refresh_delay_param!=null) && (!refresh_delay_param.startsWith("_"))) {
184 refreshDelay_ = Integer.parseInt(refresh_delay_param);
185 }
186
187
188 if (document_root !=null){
189 document_root = document_root.substring(1);
190 document_root = document_root.substring(0, document_root.indexOf("/"));
191 }
192
193
194 String image_url = getParameter("imageURL");
195 String image_ignore = getParameter("imageIgnorePrefix");
196 String href_musthave = getParameter("hrefMustHave");
197 String image_mustnothave = getParameter("imageMustNotHave");
198
199 // builds starting url when incorporated with Greenstone
200 if (image_url==null)
201 {
202 String gwcgi_param = getParameter("gwcgi");
203 gwcgi_param = tidy_URL(gwcgi_param, true);
204
205 String collection_param = getParameter("collection");
206 String classifier_param = getParameter("classifier");
207
208 image_url = gwcgi_param + "a=d";
209 image_url += "&c=" + collection_param;
210 image_url += "&cl=" + classifier_param;
211 }
212
213
214 // creates a class to store the image and it's associated url
215 download_images_ = new DownloadImages(verbosity_);
216 // starts the download image thread with the starting url
217
218 download_thread_ = new DownloadUrls(this, download_images_,
219 image_url, href_musthave, image_mustnothave,
220 image_ignore, imageType_,document_root,verbosity_);
221 // starts the display image thread with the currently downloaded images
222 display_thread_ = new DisplayImages(this,download_images_, isJava2_, bgcolor_);
223 display_ = new Display(this);
224 }
225
226 /** Goes to the url associated with the image that is clicked on screen<br>
227 * Displays the url containing the image in a new window */
228 public boolean mouseDown(Event event, int x, int y)
229 {
230 // determines which image was clicked on
231 CollageImage cimage = display_thread_.clickedOnImage(x,y);
232 // if they were clicking on an image (as opposed to background)
233 if (cimage != null)
234 {
235 System.err.println("Click on image: from url = " + cimage.from_url_);
236 try {
237 // displays the associated url in a new window
238 URL from_url = new URL(cimage.from_url_ + "#" + cimage.name_);
239 getAppletContext().showDocument(from_url,"gsdlDoc");
240 }
241 catch (MalformedURLException e) {
242 e.printStackTrace();
243 }
244
245 }
246 return true;
247 }
248
249 /** Start threads for downloading and displaying images */
250 public void start()
251 {
252 download_thread_.start();
253 display_thread_.start();
254 display_.start();
255 }
256
257 /** Stops threads for downloading and displaying images */
258 public void stop()
259 {
260 download_thread_.stop();
261 display_thread_.stop();
262 display_.stop();
263
264 download_thread_ = null;
265 display_thread_ = null;
266 display_ = null;
267 }
268
269 /** Destroys threads for downloading and displaying images */
270 public void destroyed()
271 {
272 download_thread_ = null;
273 display_thread_ = null;
274 display_ = null;
275 }
276
277 /** Repaints the applet */
278 public void update(Graphics g)
279 {
280 // System.err.println("Update called");
281 paint(g);
282 }
283
284 /** Repaints the applet using the paint method of the thread that is
285 * currently displaying images */
286 public void paint(Graphics g) {
287 if (display_thread_!=null)
288 {
289 //display_thread_.display_collage();
290 display_thread_.paint(g);
291 }
292 else
293 {
294 System.err.println("Applet still trying to paint!!");
295 }
296 }
297
298
299
300 /** Ensures a URL address (as string) has a protocol, host, and file.
301 *
302 * If the URL is a CGI script URL, it should be tidied up so that it is
303 * appropriate to tage attrib=value pairs on the end. This means it
304 * must either end with a "?" or (if it contains a question-mark
305 * internally) end with a "&". */
306 String tidy_URL(String address, boolean isCGI) {
307
308 // System.err.println("tidy URL: " + address);
309
310 // make sure the URL has protocol, host, and file
311 if (address.startsWith("http")) {
312 // the address has all the necessary components
313 } else if (address.startsWith("/")) {
314 // there is not protocol and host
315 URL document = getDocumentBase();
316 String port = "";
317 if (document.getPort()!=-1) {
318 port = ":" + document.getPort();
319 }
320 address = "http://" + document.getHost() + port + address;
321 } else {
322 // this URL is relative to the directory the document is in
323 URL document = getDocumentBase();
324 String directory = document.getFile();
325 int end = directory.lastIndexOf('/');
326 String port = "";
327 if (document.getPort()!=-1) {
328 port = ":" + document.getPort();
329 }
330 directory = directory.substring(0,end + 1);
331 address = "http://" + document.getHost() + port + directory + address;
332
333 }
334
335 // if the URL is a cgi script, make sure it has a "?" in ti,
336 // and that it ends with a "?" or "&"
337 if (isCGI) {
338 if (address.indexOf((int) '?') == -1) {
339 address = address + "?";
340 } else if (!address.endsWith("?")) {
341 address = address + "&";
342 }
343 }
344
345 return address;
346 }
347
348
349}
Note: See TracBrowser for help on using the repository browser.