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

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

changed the code getting the greenstone home (document root)

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