1 | package org.nzdl.gsdl.GsdlCollageApplet;
|
---|
2 |
|
---|
3 | import java.applet.*;
|
---|
4 | import java.awt.*;
|
---|
5 | import 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 |
|
---|
20 | public 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 is_java2_ = true;
|
---|
32 | /** Number of nested links to follow
|
---|
33 | * When used with greenstone, controls to which level of the document links will be followed
|
---|
34 | * For example a maximum depth of 1 will mean only the top page of a document will be examined,
|
---|
35 | * while a depth of 2 will include sections within this document, 3 includes sections within
|
---|
36 | * sections, and so on */
|
---|
37 | protected int max_depth_ = 1;
|
---|
38 | /** Maximum number of downloaded images to stored <br>
|
---|
39 | * Prevents applet from using excessive amounts of bandwidth by downloading too many images simulataneously*/
|
---|
40 | protected int max_downloads_ = Integer.MAX_VALUE;
|
---|
41 | /** Types of images permitted in the collage, for example gif, jpg, png... */
|
---|
42 | protected String image_type;
|
---|
43 | /** Background color of applet screen */
|
---|
44 | protected Color bgcolor_ = new Color(150, 193, 155);
|
---|
45 |
|
---|
46 | /** Time lapse between repainting of the applet */
|
---|
47 | protected int refresh_delay_ = 1500;
|
---|
48 |
|
---|
49 | /** Stores an image and url pair and provides associated methods */
|
---|
50 | protected DownloadImages download_images_ = null;
|
---|
51 | /** Downloads images from a starting url, recursively follows nested links */
|
---|
52 | protected DownloadUrls download_thread_ = null;
|
---|
53 | /** Image processing and placement on applet screen */
|
---|
54 | protected DisplayImages display_thread_ = null;
|
---|
55 |
|
---|
56 | protected Display display_ = null;
|
---|
57 |
|
---|
58 | /** Gets verbosity */
|
---|
59 | public int verbosity() { return verbosity_; }
|
---|
60 | /** Gets maximum depth for nested links */
|
---|
61 | public int maxDepth() { return max_depth_; }
|
---|
62 | /** Gets maximum number of images to store during downloading */
|
---|
63 | public int maxDownloads() { return max_downloads_; }
|
---|
64 | /** Gets the refresh delay used between repaint */
|
---|
65 | public int refreshDelay() { return refresh_delay_; }
|
---|
66 |
|
---|
67 | /** Gets parameters from the applet code and stores locally<br>
|
---|
68 | * Forms a starting url for image retrieval to begin from<br>
|
---|
69 | * The starting url is formed either by: <br>
|
---|
70 | * Using the image_url parameter as provided, which is assumed to be a complete url<br>
|
---|
71 | * If this parameter does not exist, the assumption is that the collage is being incorporated
|
---|
72 | * with the Greenstone Digital Library Software. The starting url is formed by concatenating
|
---|
73 | * the gwcgi, collection and classifier parameters as provided.<br>
|
---|
74 | * Then starts downloading and displaying images */
|
---|
75 | public void init()
|
---|
76 | {
|
---|
77 |
|
---|
78 | // gets the parameters specified
|
---|
79 | String verbosity_param = getParameter("verbosity");
|
---|
80 | String is_java2_param = getParameter("isJava2");
|
---|
81 | String max_depth_param = getParameter("maxDepth");
|
---|
82 | String max_downloads_param = getParameter("maxDownloads");
|
---|
83 | String refresh_delay_param = getParameter("refreshDelay");
|
---|
84 | String image_type_param = getParameter("imageType");
|
---|
85 | String bgcolor_param = getParameter("bgcolor");
|
---|
86 |
|
---|
87 | if (bgcolor_param != null) {
|
---|
88 | String [] c = bgcolor_param.split(",");
|
---|
89 | if (c.length == 3)
|
---|
90 | bgcolor_ = new Color(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2]));
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (image_type_param != null) {
|
---|
94 | image_type = image_type_param;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (verbosity_param!=null)
|
---|
98 | {
|
---|
99 | verbosity_ = Integer.parseInt(verbosity_param);
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (is_java2_param == null || is_java2_param.equals("auto"))
|
---|
103 | {
|
---|
104 | String version = System.getProperty("java.version");
|
---|
105 | version = version.substring(0, version.lastIndexOf("."));
|
---|
106 | System.err.println("VERSION:" + version);
|
---|
107 | float ver = (new Float(version)).floatValue();
|
---|
108 | is_java2_ = (ver >= 1.2) ? true : false;
|
---|
109 | }
|
---|
110 | else {
|
---|
111 | is_java2_ = (is_java2_param.equals("true")) ? true : false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (max_depth_param!=null)
|
---|
115 | {
|
---|
116 | max_depth_ = Integer.parseInt(max_depth_param);
|
---|
117 | }
|
---|
118 | if (max_downloads_param!=null)
|
---|
119 | {
|
---|
120 | max_downloads_ = Integer.parseInt(max_downloads_param);
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (refresh_delay_param!=null)
|
---|
124 | {
|
---|
125 | refresh_delay_ = Integer.parseInt(refresh_delay_param);
|
---|
126 | }
|
---|
127 |
|
---|
128 | String image_url = getParameter("imageURL");
|
---|
129 | String image_ignore = getParameter("imageIgnorePrefix");
|
---|
130 | String href_musthave = getParameter("hrefMustHave");
|
---|
131 | String image_mustnothave = getParameter("imageMustNotHave");
|
---|
132 |
|
---|
133 | // builds starting url when incorporated with Greenstone
|
---|
134 | if (image_url==null)
|
---|
135 | {
|
---|
136 | String gwcgi_param = getParameter("gwcgi");
|
---|
137 | gwcgi_param = tidy_URL(gwcgi_param, true);
|
---|
138 |
|
---|
139 | String collection_param = getParameter("collection");
|
---|
140 | String classifier_param = getParameter("classifier");
|
---|
141 |
|
---|
142 | image_url = gwcgi_param + "a=d";
|
---|
143 | image_url += "&c=" + collection_param;
|
---|
144 | image_url += "&cl=" + classifier_param;
|
---|
145 | }
|
---|
146 |
|
---|
147 | // creates a class to store the image and it's associated url
|
---|
148 | download_images_ = new DownloadImages(verbosity_);
|
---|
149 | // starts the download image thread with the starting url
|
---|
150 | download_thread_ = new DownloadUrls(this, download_images_,
|
---|
151 | image_url, href_musthave, image_mustnothave,
|
---|
152 | image_ignore, image_type);
|
---|
153 | // starts the display image thread with the currently downloaded images
|
---|
154 | display_thread_ = new DisplayImages(this,download_images_,is_java2_, bgcolor_);
|
---|
155 | display_ = new Display(this);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Goes to the url associated with the image that is clicked on screen<br>
|
---|
159 | * Displays the url containing the image in a new window */
|
---|
160 | public boolean mouseDown(Event event, int x, int y)
|
---|
161 | {
|
---|
162 | // determines which image was clicked on
|
---|
163 | CollageImage cimage = display_thread_.clickedOnImage(x,y);
|
---|
164 | // if they were clicking on an image (as opposed to background)
|
---|
165 | if (cimage != null)
|
---|
166 | {
|
---|
167 | //System.err.println("Click on image: from url = " + cimage.from_url_);
|
---|
168 | try {
|
---|
169 | // displays the associated url in a new window
|
---|
170 | URL from_url = new URL(cimage.from_url_ + "#" + cimage.name_);
|
---|
171 | getAppletContext().showDocument(from_url,"gsdlDoc");
|
---|
172 | }
|
---|
173 | catch (MalformedURLException e) {
|
---|
174 | e.printStackTrace();
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 | return true;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Start threads for downloading and displaying images */
|
---|
182 | public void start()
|
---|
183 | {
|
---|
184 | download_thread_.start();
|
---|
185 | display_thread_.start();
|
---|
186 | display_.start();
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Stops threads for downloading and displaying images */
|
---|
190 | public void stop()
|
---|
191 | {
|
---|
192 | download_thread_.stop();
|
---|
193 | display_thread_.stop();
|
---|
194 | display_.stop();
|
---|
195 |
|
---|
196 | download_thread_ = null;
|
---|
197 | display_thread_ = null;
|
---|
198 | display_ = null;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** Destroys threads for downloading and displaying images */
|
---|
202 | public void destroyed()
|
---|
203 | {
|
---|
204 | download_thread_ = null;
|
---|
205 | display_thread_ = null;
|
---|
206 | display_ = null;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Repaints the applet */
|
---|
210 | public void update(Graphics g)
|
---|
211 | {
|
---|
212 | // System.err.println("Update called");
|
---|
213 | paint(g);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** Repaints the applet using the paint method of the thread that is currently displaying images */
|
---|
217 | public void paint(Graphics g) {
|
---|
218 | if (display_thread_!=null)
|
---|
219 | {
|
---|
220 | //display_thread_.display_collage();
|
---|
221 | display_thread_.paint(g);
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | System.err.println("Applet still trying to paint!!");
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 |
|
---|
231 | /** Ensures a URL address (as string) has a protocol, host, and file.
|
---|
232 | *
|
---|
233 | * If the URL is a CGI script URL, it should be tidied up so that it is
|
---|
234 | * appropriate to tage attrib=value pairs on the end. This means it
|
---|
235 | * must either end with a "?" or (if it contains a question-mark
|
---|
236 | * internally) end with a "&". */
|
---|
237 | String tidy_URL(String address, boolean isCGI) {
|
---|
238 |
|
---|
239 | // System.err.println("tidy URL: " + address);
|
---|
240 |
|
---|
241 | // make sure the URL has protocol, host, and file
|
---|
242 | if (address.startsWith("http")) {
|
---|
243 | // the address has all the necessary components
|
---|
244 | } else if (address.startsWith("/")) {
|
---|
245 | // there is not protocol and host
|
---|
246 | URL document = getDocumentBase();
|
---|
247 | String port = "";
|
---|
248 | if (document.getPort()!=-1) {
|
---|
249 | port = ":" + document.getPort();
|
---|
250 | }
|
---|
251 | address = "http://" + document.getHost() + port + address;
|
---|
252 | } else {
|
---|
253 | // this URL is relative to the directory the document is in
|
---|
254 | URL document = getDocumentBase();
|
---|
255 | String directory = document.getFile();
|
---|
256 | int end = directory.lastIndexOf('/');
|
---|
257 | String port = "";
|
---|
258 | if (document.getPort()!=-1) {
|
---|
259 | port = ":" + document.getPort();
|
---|
260 | }
|
---|
261 | directory = directory.substring(0,end + 1);
|
---|
262 | address = "http://" + document.getHost() + port + directory + address;
|
---|
263 |
|
---|
264 | }
|
---|
265 |
|
---|
266 | // if the URL is a cgi script, make sure it has a "?" in ti,
|
---|
267 | // and that it ends with a "?" or "&"
|
---|
268 | if (isCGI) {
|
---|
269 | if (address.indexOf((int) '?') == -1) {
|
---|
270 | address = address + "?";
|
---|
271 | } else if (!address.endsWith("?")) {
|
---|
272 | address = address + "&";
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | return address;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | }
|
---|