1 | package org.greenstone.applet.GsdlCollageApplet;
|
---|
2 |
|
---|
3 | import org.webswing.toolkit.api.WebswingUtil;
|
---|
4 | import org.webswing.toolkit.api.action.WebActionEvent;
|
---|
5 | import org.webswing.toolkit.api.action.WebActionListener;
|
---|
6 |
|
---|
7 | import org.webswing.toolkit.api.url.*;
|
---|
8 |
|
---|
9 | //import java.applet.Applet;
|
---|
10 | import javax.swing.JApplet;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.awt.event.MouseListener;
|
---|
14 | import java.awt.event.WindowAdapter;
|
---|
15 | import java.awt.event.WindowEvent;
|
---|
16 | import java.awt.*;
|
---|
17 | import java.net.*;
|
---|
18 |
|
---|
19 | import javax.swing.JFrame;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.HashMap;
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | *
|
---|
27 | * @author Katrina Edgar
|
---|
28 | * @author David Bainbridge
|
---|
29 | *
|
---|
30 | * Main class for the GsdlCollageApplet<br>
|
---|
31 | * Processes the parameters passed through the Applet<br>
|
---|
32 | * Builds an appropriate starting url from these parameters<br>
|
---|
33 | * Creates a storage class for downloaded images<br>
|
---|
34 | * Starts thread to download images and their associated url<br>
|
---|
35 | * Starts thread to display downloaded images on the applet screen<br>
|
---|
36 | */
|
---|
37 |
|
---|
38 | public class GsdlCollageApplet extends JApplet {
|
---|
39 | static final int EXTRA_HEIGHT = 30; // for status bar or any extra elements
|
---|
40 | private int X_DIM = 600;
|
---|
41 | private int Y_DIM = 300;
|
---|
42 |
|
---|
43 | boolean isWebswing = false; // if run as webswing
|
---|
44 | // To run this GsdlCollageApplet as Application instead of as Applet
|
---|
45 | boolean isRunAsApplet = true;
|
---|
46 | // set only if JPhind object is run as an application
|
---|
47 | URL docBaseURL = null;
|
---|
48 | JLabel statusBar = null;
|
---|
49 | Map<String,String> appParams;
|
---|
50 |
|
---|
51 | // package access, used mainly for GS3
|
---|
52 | int gsdlversion = 2;
|
---|
53 | String baseURL = null;
|
---|
54 |
|
---|
55 | /** When asked to stop running, this variable will be set to true */
|
---|
56 | private boolean stop_running = false;
|
---|
57 |
|
---|
58 | /** Amount of error checking output produced <br>
|
---|
59 | * Ranges from 0 - no output to 3 - maximum output */
|
---|
60 | protected int verbosity_ = 0;
|
---|
61 | /** Indicates whether java2 functionality should be used <br>
|
---|
62 | * If true will allow advanced image processing techniques to occur,
|
---|
63 | * such as fading and colouring using pixel manipulation <br>
|
---|
64 | * If false, images will maintain an alpha value of 1 (appear solid),
|
---|
65 | * newer images will simply be pasted on top of existing images <br> */
|
---|
66 | protected boolean isJava2_ = true;
|
---|
67 | /** Number of nested links to follow When used with greenstone,
|
---|
68 | * controls to which level of the document links will be followed For
|
---|
69 | * example a maximum depth of 1 will mean only the top page of a
|
---|
70 | * document will be examined, while a depth of 2 will include sections
|
---|
71 | * within this document, 3 includes sections within sections, and so
|
---|
72 | * on */
|
---|
73 | protected int maxDepth_ = 3;
|
---|
74 |
|
---|
75 | /** Maximum number of images to display on the canvas */
|
---|
76 | protected int maxDisplay_ = 25;
|
---|
77 |
|
---|
78 | /** Maximum number of downloaded images to store <br> Prevents applet
|
---|
79 | * from using excessive amounts of bandwidth by downloading too many
|
---|
80 | * images simulataneously*/
|
---|
81 | protected int maxDownloads_ = Integer.MAX_VALUE;
|
---|
82 |
|
---|
83 | /** Types of images permitted in the collage, for example gif, jpg,
|
---|
84 | * png... */
|
---|
85 | protected String imageType_ = ".jpg%.png";
|
---|
86 |
|
---|
87 | /** Caption of the collage */
|
---|
88 | protected String caption_ = "";
|
---|
89 |
|
---|
90 | /** Background color of applet screen */
|
---|
91 | protected Color bgcolor_ = new Color(150, 193, 155);
|
---|
92 |
|
---|
93 | /** Time lapse between repainting of the applet */
|
---|
94 | protected int refreshDelay_ = 2000;
|
---|
95 |
|
---|
96 | /** Stores an image and url pair and provides associated methods */
|
---|
97 | protected DownloadImages download_images_ = null;
|
---|
98 | /** Downloads images from a starting url, recursively follows nested
|
---|
99 | * links */
|
---|
100 | protected DownloadUrls download_thread_ = null;
|
---|
101 | /** Image processing and placement on applet screen */
|
---|
102 | protected DisplayImages display_thread_ = null;
|
---|
103 |
|
---|
104 |
|
---|
105 | /** Gets the set, calculated or default width (x dimension) of the applet/application */
|
---|
106 | public int X_DIM() {
|
---|
107 | return X_DIM;
|
---|
108 | }
|
---|
109 | /** Gets the set, calculated or default height (y dimension) of the applet/application */
|
---|
110 | public int Y_DIM() {
|
---|
111 | return Y_DIM;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Gets verbosity */
|
---|
115 | public int verbosity() { return verbosity_; }
|
---|
116 | /** Gets maximum depth for nested links */
|
---|
117 | public int maxDepth() { return maxDepth_; }
|
---|
118 |
|
---|
119 | /** Gets maximum number of images to display */
|
---|
120 | public int maxDisplay() { return maxDisplay_;}
|
---|
121 |
|
---|
122 | /** Gets maximum number of images to store during downloading */
|
---|
123 | public int maxDownloads() { return maxDownloads_; }
|
---|
124 | /** Gets the refresh delay used between repaint */
|
---|
125 | public int refreshDelay() { return refreshDelay_; }
|
---|
126 |
|
---|
127 | /** Gets parameters from the applet code and stores locally.
|
---|
128 | * Forms a starting url for image retrieval to begin from.
|
---|
129 | * The starting url is formed either by:
|
---|
130 | * * Using the image_url parameter as provided, which is assumed to
|
---|
131 | * be a complete url
|
---|
132 | * * If this parameter does not exist, the assumption is that the
|
---|
133 | * collage is being incorporated with the Greenstone Digital Library
|
---|
134 | * Software. The starting url is formed by concatenating
|
---|
135 | * the gwcgi, collection and classifier parameters as provided.
|
---|
136 | * Then starts downloading and displaying images */
|
---|
137 |
|
---|
138 | Thread paint ;
|
---|
139 |
|
---|
140 | public GsdlCollageApplet() {
|
---|
141 |
|
---|
142 | super();
|
---|
143 |
|
---|
144 | // status bar that applet has, but we want one also if we run JPhind as application
|
---|
145 | // And in fact, when webswing runs our applet, we never get a status bar. So we
|
---|
146 | // create a custom status bar now even if we're running as an applet.
|
---|
147 | // When this applet is run through the appletviewer we might end up with 2 status bars.
|
---|
148 | //setLayout(new BorderLayout());
|
---|
149 | //if(!isRunAsApplet) {
|
---|
150 | statusBar = null; //new JLabel();
|
---|
151 |
|
---|
152 | //this.add(statusBar, BorderLayout.SOUTH);
|
---|
153 |
|
---|
154 | ///Window win = SwingUtilities.getWindowAncestor(this);
|
---|
155 | ///if(win instanceof JFrame) {
|
---|
156 | /// JFrame topFrame = (JFrame)win;
|
---|
157 | /// topFrame.add(statusBar, BorderLayout.SOUTH);
|
---|
158 | ///} else { //AppletViewer or browser
|
---|
159 | /// this.add(statusBar, BorderLayout.SOUTH);
|
---|
160 | ///}
|
---|
161 |
|
---|
162 | //Dimension d = statusBar.getSize();
|
---|
163 | //d.height = EXTRA_HEIGHT;
|
---|
164 | //statusBar.setPreferredSize(d);
|
---|
165 | //}
|
---|
166 |
|
---|
167 | this.addMouseListener(new CollageMouseAdapter());
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void applicationPreInit(String[] args) {
|
---|
171 | // I copied the contents of this contructor for my code for the JPhind.java constructor
|
---|
172 | // This constructor will be used when this JApplet is run as an application
|
---|
173 | // instead of as applet
|
---|
174 | this.isRunAsApplet = false;
|
---|
175 |
|
---|
176 | appParams = new HashMap<String,String>((args.length+1)/2);
|
---|
177 | // For GS2, gwcgi = param gwcgi
|
---|
178 | // For GS3, gwcgi = param gwcgi + param library
|
---|
179 | // docBaseURL = starting_url/image_url = param gwcgi
|
---|
180 |
|
---|
181 | String key = null;
|
---|
182 | String value = null;
|
---|
183 | for(int i = 0; i < args.length; i++) {
|
---|
184 | if(i%2==0) {
|
---|
185 | key = args[i].substring(2); // remove -- prefix of paramname
|
---|
186 | //System.err.println("got key: " + key);
|
---|
187 | } else {
|
---|
188 | value = args[i];
|
---|
189 | appParams.put(key, value);
|
---|
190 | //System.err.println("got value: " + value);
|
---|
191 |
|
---|
192 |
|
---|
193 | // HttpUtils.parseQueryString() deprecated, so hacking decode xtra key-value pairs
|
---|
194 | // https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection?page=1&tab=scoredesc#tab-top
|
---|
195 | // String.split() is preferred over Tokenizer but 2nd parameter behaves differently
|
---|
196 | // than I expected.
|
---|
197 | // https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29
|
---|
198 | if(key.equals("xtraParams")) {
|
---|
199 | value = value.replace("&", "&"); // just in case we have html entities
|
---|
200 | String[] param_list = value.split("&", 0); // 0 for all occurrences
|
---|
201 | for(String key_val : param_list) {
|
---|
202 | String[] paramPair = key_val.split("=", 2); // get first 2 strings, key and val
|
---|
203 | //System.err.println("key_val: " + key_val);
|
---|
204 | if(paramPair.length == 2) {
|
---|
205 | String xtraParamsKey = paramPair[0];
|
---|
206 | String xtraParamsVal = paramPair[1];
|
---|
207 | //System.err.println("key - val: " + xtraParamsKey + " - " + xtraParamsVal);
|
---|
208 | appParams.put(xtraParamsKey, xtraParamsVal);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | key = value = null;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | isWebswing = appParams.getOrDefault("webswing", "0").equals("1") ? true : false;
|
---|
218 |
|
---|
219 | // Handle getting called by webswing JavaScript (but only if run in webswing mode):
|
---|
220 | // when the user navigates away from the webswing app's web page, do cleanup
|
---|
221 | // https://www.webswing.org/docs/23.2/integrate/jslink.html#invoking-java-from-javascript
|
---|
222 | // https://vaadin.com/directory/component/webswing-vaadin1
|
---|
223 | if(isWebswing) {
|
---|
224 |
|
---|
225 | System.err.println("#### isWebswing, about to add listeners");
|
---|
226 |
|
---|
227 | WebswingUtil.getWebswingApi().addBrowserActionListener(new WebActionListener() {
|
---|
228 | //@Override
|
---|
229 | public void actionPerformed(WebActionEvent actionEvent) {
|
---|
230 |
|
---|
231 | System.err.println("******** JavaScript to Java: actionPerformed called.");
|
---|
232 | //System.err.println("actionEvent = ");
|
---|
233 |
|
---|
234 | switch (actionEvent.getActionName()) {
|
---|
235 | case "navigatingAway":
|
---|
236 | // do cleanup
|
---|
237 | System.err.println("@@@@ In Java collage addBrowserActionListener - on navigatingAway called");
|
---|
238 | GsdlCollageApplet.this.stopRunning();
|
---|
239 | break;
|
---|
240 | case "testJavaCall":
|
---|
241 | WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet got TEST call !!", null);
|
---|
242 | //WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet - testjavaCall handler got your message", null);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | });
|
---|
246 | }
|
---|
247 |
|
---|
248 | // Need these parameters set before init(), so that the display works
|
---|
249 | int w = this.getWidth();
|
---|
250 | if(appParams.get("width") != null) {
|
---|
251 | w = Integer.parseInt(appParams.get("width"));
|
---|
252 | }
|
---|
253 | int h = this.getHeight();
|
---|
254 | if(appParams.get("height") != null) {
|
---|
255 | h = Integer.parseInt(appParams.get("height"));
|
---|
256 | }
|
---|
257 | // For applet, w and h may now be set to a positive value. But if GsdlCollageApplet
|
---|
258 | // is run as an application, it won't yet be visible and not have a non-zero dimension.
|
---|
259 | // So use defaults here:
|
---|
260 | this.X_DIM = (w <= 0) ? X_DIM : w;
|
---|
261 | this.Y_DIM = (h <= 0) ? Y_DIM : h;
|
---|
262 |
|
---|
263 | // Attempting hack to circumvent division by zero error in MyAffineTransform line 39
|
---|
264 | //DisplayImages.app_x_dim_ = X_DIM;
|
---|
265 | //DisplayImages.app_y_dim_ = Y_DIM;
|
---|
266 |
|
---|
267 | try {
|
---|
268 | this.docBaseURL = new URL(appParams.get("baseurl"));
|
---|
269 | } catch(MalformedURLException mue) {
|
---|
270 | mue.printStackTrace();
|
---|
271 | System.err.println("*** Unable to instantiate URL from parameter baseurl: " + appParams.get("baseurl"));
|
---|
272 | System.exit(-1);
|
---|
273 | }
|
---|
274 |
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Overriding (J)Applet method getParameter to first check appParams map
|
---|
279 | * if Phind was run run as an application.
|
---|
280 | * https://stackoverflow.com/questions/15905127/overridden-methods-in-javadoc
|
---|
281 | */
|
---|
282 | @Override
|
---|
283 | public String getParameter(String name) {
|
---|
284 | if(!isRunAsApplet) {
|
---|
285 | return appParams.get(name);
|
---|
286 | }
|
---|
287 | else {
|
---|
288 | return super.getParameter(name);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | @Override
|
---|
293 | public URL getDocumentBase() {
|
---|
294 | if(!isRunAsApplet) { // launched as application
|
---|
295 | //System.err.println("*** docBaseURL: " + docBaseURL);
|
---|
296 | return this.docBaseURL;
|
---|
297 | } else {
|
---|
298 | return super.getDocumentBase();
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | @Override
|
---|
303 | public void showStatus(String msg) {
|
---|
304 | // Either firefox doesn't provide a status bar window for applets any more or webswing
|
---|
305 | // doesn't provide a status window, so we don't see Applet.showStatus() output appear
|
---|
306 | // in webswing-phind and in fact don't even see any Applet statusBar in webswing.
|
---|
307 | // However, since we print useful and interesting information to the status bar,
|
---|
308 | // we'll now always show and print to our manually added statusBar now
|
---|
309 | // not only if(!isRunAsApplet) when we needed to manually create a statusBar.
|
---|
310 | if(this.statusBar != null) {
|
---|
311 | this.statusBar.setText(msg);
|
---|
312 | }
|
---|
313 | if(isRunAsApplet) {
|
---|
314 | super.showStatus(msg);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | public void init()
|
---|
319 | {
|
---|
320 | if (verbosity_ >= 4) {
|
---|
321 | System.err.println("Attempting to retrieve parameters...");
|
---|
322 | }
|
---|
323 |
|
---|
324 | // gets the parameters specified
|
---|
325 | String showStatusBar = getParameter("statusbar");
|
---|
326 | if(statusBar == null) {
|
---|
327 | System.err.println("### Status bar code turned off. Ignoring statusbar settings.");
|
---|
328 | } else {
|
---|
329 | if(showStatusBar == null || showStatusBar.equals("0")) {
|
---|
330 | System.err.println("### Hiding status bar (default)");
|
---|
331 | statusBar.setVisible(false);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | String verbosity_param = getParameter("verbosity");
|
---|
335 | String is_java2_param = getParameter("isJava2");
|
---|
336 | String max_depth_param = getParameter("maxDepth");
|
---|
337 | String max_downloads_param = getParameter("maxDownloads");
|
---|
338 | String max_display_param = getParameter("maxDisplay");
|
---|
339 | String refresh_delay_param = getParameter("refreshDelay");
|
---|
340 | String image_type_param = getParameter("imageType");
|
---|
341 | String bgcolor_param = getParameter("bgcolor");
|
---|
342 | String caption_param = getParameter("caption");
|
---|
343 | String document_root = getParameter("documentroot");
|
---|
344 | String gwcgi = getParameter("gwcgi"); // for GS2
|
---|
345 | String baseurl = getParameter("baseurl"); // for GS3, URL before /library
|
---|
346 |
|
---|
347 | String gsdlVersionStr = getParameter("gsdlversion");
|
---|
348 | if(gsdlVersionStr != null) {
|
---|
349 | System.err.println("*** gsdl version: " + gsdlVersionStr);
|
---|
350 | this.gsdlversion = Integer.parseInt(gsdlVersionStr);
|
---|
351 | }
|
---|
352 |
|
---|
353 | // Check it isn't null
|
---|
354 | if ((verbosity_param!=null) && (!verbosity_param.startsWith("_"))) {
|
---|
355 | verbosity_ = Integer.parseInt(verbosity_param);
|
---|
356 | }
|
---|
357 | else {
|
---|
358 | verbosity_ = 1;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | if (verbosity_ >= 4) {
|
---|
363 | System.err.println("Got parameters.");
|
---|
364 | }
|
---|
365 |
|
---|
366 | if (caption_param != null && !caption_param.startsWith("_")) {
|
---|
367 | caption_ = caption_param;
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | if (verbosity_ >= 4) {
|
---|
371 | System.err.println("No Caption: setting to a space.");
|
---|
372 | }
|
---|
373 | caption_ = " ";
|
---|
374 | }
|
---|
375 |
|
---|
376 | if ((bgcolor_param != null) && (!bgcolor_param.startsWith("_"))) {
|
---|
377 | if (bgcolor_param.startsWith("#")) {
|
---|
378 | bgcolor_ = Color.decode(bgcolor_param);
|
---|
379 | }
|
---|
380 | else {
|
---|
381 | String [] c = bgcolor_param.split(",");
|
---|
382 | if (c.length == 3)
|
---|
383 | bgcolor_ = new Color(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2]));
|
---|
384 | }
|
---|
385 | if (verbosity_ >= 4){
|
---|
386 | System.err.println("Set BG to be " + bgcolor_.toString());
|
---|
387 | }
|
---|
388 | }
|
---|
389 | else {
|
---|
390 | if (verbosity_ >= 4) {
|
---|
391 | System.err.println("No BG: setting to NZDL green.");
|
---|
392 | }
|
---|
393 | bgcolor_ = new Color(150, 193, 155);
|
---|
394 | }
|
---|
395 |
|
---|
396 | if ((image_type_param != null) && (!image_type_param.startsWith("_"))) {
|
---|
397 | imageType_ = image_type_param;
|
---|
398 | }
|
---|
399 |
|
---|
400 | if ((is_java2_param == null) || (is_java2_param.equals("auto")) || (is_java2_param.startsWith("_"))) {
|
---|
401 | String version = System.getProperty("java.version");
|
---|
402 | version = version.substring(0, version.lastIndexOf("."));
|
---|
403 | System.err.println("VERSION: " + version);
|
---|
404 | float ver = (new Float(version)).floatValue();
|
---|
405 | isJava2_ = (ver >= 1.2) ? true : false;
|
---|
406 | }
|
---|
407 | else {
|
---|
408 | isJava2_ = (is_java2_param.equals("true")) ? true : false;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if ((max_depth_param != null) && (!max_depth_param.startsWith("_"))) {
|
---|
412 | // System.err.println("maxDepth = " + max_depth_param);
|
---|
413 | maxDepth_ = Integer.parseInt(max_depth_param);
|
---|
414 | }
|
---|
415 |
|
---|
416 | if ((max_downloads_param!=null) && (!max_downloads_param.startsWith("_"))) {
|
---|
417 | maxDownloads_ = Integer.parseInt(max_downloads_param);
|
---|
418 | System.out.println(" maxDownloads " + maxDownloads_ );
|
---|
419 | maxDownloads_=50;
|
---|
420 | }
|
---|
421 |
|
---|
422 | if ((max_display_param!=null) && (!max_display_param.startsWith("_"))) {
|
---|
423 | maxDisplay_ = Integer.parseInt(max_display_param);
|
---|
424 |
|
---|
425 | }
|
---|
426 |
|
---|
427 | if ((refresh_delay_param!=null) && (!refresh_delay_param.startsWith("_"))) {
|
---|
428 | refreshDelay_ = Integer.parseInt(refresh_delay_param);
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | if (document_root !=null){ // e.g. "/greenstone/web/images" for GS2
|
---|
433 | if(document_root.indexOf("/") == 0) {
|
---|
434 | document_root = document_root.substring(1); // takes off first slash
|
---|
435 | }
|
---|
436 | if (document_root.indexOf("/") > 0 ){ // e.g we end up with "greenstone" for GS2, "greenstone3" for GS3
|
---|
437 | document_root = document_root.substring(0, document_root.indexOf("/"));
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | String image_url = getParameter("imageURL");
|
---|
442 | String image_ignore = getParameter("imageIgnorePrefix");
|
---|
443 | String href_musthave = getParameter("hrefMustHave");
|
---|
444 | String image_mustnothave = getParameter("imageMustNotHave");
|
---|
445 |
|
---|
446 | // builds starting url when incorporated with Greenstone
|
---|
447 | if (image_url==null)
|
---|
448 | {
|
---|
449 | String collection_param = getParameter("collection");
|
---|
450 | String classifier_param = getParameter("classifier");
|
---|
451 |
|
---|
452 | if(gsdlversion == 2) {
|
---|
453 | // GS2 way
|
---|
454 | String gwcgi_param = gwcgi;
|
---|
455 | gwcgi_param = tidy_URL(gwcgi_param, true); //true to append ? to URL
|
---|
456 | image_url = gwcgi_param + "a=d";
|
---|
457 | image_url += "&c=" + collection_param;
|
---|
458 | image_url += "&cl=" + classifier_param;
|
---|
459 | } else {
|
---|
460 | // Try GS3 way
|
---|
461 | String library = getParameter("library");
|
---|
462 | //String site = getParameter("sitename");
|
---|
463 |
|
---|
464 | // starting URL (image_url) might not be base_url. We need to store base_url separately
|
---|
465 | baseURL = tidy_URL(baseurl, false);
|
---|
466 | if(!baseURL.endsWith("/")) {
|
---|
467 | baseURL += "/";
|
---|
468 | }
|
---|
469 |
|
---|
470 | // building up to baseURL/LIBNAME/collection/COLNAME/browse/
|
---|
471 | image_url = baseURL + library + "/collection/" + collection_param + "/browse/";
|
---|
472 |
|
---|
473 | int index = classifier_param.indexOf(".");
|
---|
474 | if(index == -1) {
|
---|
475 | image_url += classifier_param;
|
---|
476 | } else {
|
---|
477 |
|
---|
478 | //"CL2#" + classifier_param;
|
---|
479 | //String superClassifier = classifier_param.substring(0, index);
|
---|
480 | //image_url = image_url + superClassifier + "#" + classifier_param;
|
---|
481 |
|
---|
482 | // I'm guessing we want something like
|
---|
483 | //http://localhost:8383/greenstone3/library/collection/smallbea/browse/CL2/3, when classifier_param is CL2.3
|
---|
484 | // to get the classifier page containing the images that are to be turned into a collage?
|
---|
485 | String classifierSuffix = classifier_param.replace(".", "/");
|
---|
486 | image_url = image_url + classifierSuffix;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | MediaTracker trk = new MediaTracker(this);
|
---|
492 |
|
---|
493 | // creates a class to store the image and its associated url
|
---|
494 | download_images_ = new DownloadImages( this, verbosity_,isJava2_ );
|
---|
495 | // starts the download image thread with the starting url
|
---|
496 |
|
---|
497 | download_thread_ = new DownloadUrls(this, download_images_,
|
---|
498 | image_url, href_musthave, image_mustnothave,
|
---|
499 | image_ignore, imageType_,document_root,verbosity_,trk);
|
---|
500 |
|
---|
501 | // starts the display image thread with the currently downloaded images
|
---|
502 | display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
|
---|
503 | }
|
---|
504 |
|
---|
505 | public JLabel getStatusBar() {
|
---|
506 | return this.statusBar;
|
---|
507 | }
|
---|
508 |
|
---|
509 | // TODO: Do I need to follow
|
---|
510 | // https://stackoverflow.com/questions/5861894/how-to-synchronize-or-lock-upon-variables-in-java
|
---|
511 | public void stopRunning() {
|
---|
512 | if(verbosity_ >= 3) {
|
---|
513 | System.err.println("**** GsdlCollageApplet.stopRunning() called");
|
---|
514 | }
|
---|
515 |
|
---|
516 | stop_running = true;
|
---|
517 | if(download_thread_ != null) {
|
---|
518 | download_thread_.stopRunning();
|
---|
519 | }
|
---|
520 | if(display_thread_ != null) {
|
---|
521 | display_thread_.stopRunning();
|
---|
522 | }
|
---|
523 |
|
---|
524 | // The Display Thread?
|
---|
525 | //if(!Thread.currentThread().isInterrupted()) {
|
---|
526 | // Thread.currentThread().interrupt();
|
---|
527 | //}
|
---|
528 | }
|
---|
529 |
|
---|
530 | public boolean isStopping() {
|
---|
531 | return stop_running;
|
---|
532 | }
|
---|
533 |
|
---|
534 | protected class CollageMouseAdapter extends MouseAdapter {
|
---|
535 |
|
---|
536 | /** Goes to the url associated with the image that is clicked on screen<br>
|
---|
537 | * Displays the url containing the image in a new window */
|
---|
538 | //public boolean mouseDown(Event event, int x, int y)
|
---|
539 | public void mousePressed(MouseEvent e)
|
---|
540 | {
|
---|
541 |
|
---|
542 | System.err.println("Mouse pressed");
|
---|
543 | int x = e.getX();
|
---|
544 | int y = e.getY();
|
---|
545 |
|
---|
546 | // determines which image was clicked on
|
---|
547 | CollageImage cimage = GsdlCollageApplet.this.display_thread_.clickedOnImage(x,y);
|
---|
548 | // if they were clicking on an image (as opposed to background)
|
---|
549 | if (cimage != null)
|
---|
550 | {
|
---|
551 | System.err.println("Click on image: from url = " + cimage.from_url_);
|
---|
552 | try {
|
---|
553 | // displays the associated url in a new window
|
---|
554 | URL from_url = null;
|
---|
555 |
|
---|
556 | java.util.regex.Pattern p = java.util.regex.Pattern.compile("cl=CL\\d(\\.\\d)*\\s");
|
---|
557 | java.util.regex.Matcher m = p.matcher(cimage.from_url_.trim()+" ");
|
---|
558 |
|
---|
559 | if (m.find() ){
|
---|
560 | from_url = cimage.url_;
|
---|
561 | }
|
---|
562 | else{
|
---|
563 | from_url = new URL(cimage.from_url_ + "#" + cimage.name_);
|
---|
564 | }
|
---|
565 |
|
---|
566 | if(isRunAsApplet) {
|
---|
567 | GsdlCollageApplet.this.getAppletContext().showDocument(from_url,"gsdlDoc");
|
---|
568 | } else if(isWebswing) {
|
---|
569 | WebswingUtil.getWebswingApi().sendActionEvent("openURL",
|
---|
570 | from_url.toString() + " - " +"gsdlDoc",
|
---|
571 | null); // window name is gsdlDoc
|
---|
572 | } else {
|
---|
573 | System.err.println("@@@GsdlCollageApplet.CollageMouseAdapter.mousePressed()"
|
---|
574 | + "\n\topening url " + from_url + "\n\t"
|
---|
575 | + "for non-applet and non-webswing application is not yet implemented.");
|
---|
576 | }
|
---|
577 | }
|
---|
578 | catch (MalformedURLException ex) {
|
---|
579 | ex.printStackTrace();
|
---|
580 | }
|
---|
581 |
|
---|
582 | }
|
---|
583 | //return true;
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | /** Start threads for downloading and displaying images */
|
---|
588 | public void start()
|
---|
589 | {
|
---|
590 |
|
---|
591 | download_thread_.start();
|
---|
592 |
|
---|
593 | display_thread_.start();
|
---|
594 |
|
---|
595 | paint = new Thread(new Runnable(){
|
---|
596 | public void run() {
|
---|
597 | try {
|
---|
598 |
|
---|
599 | Thread curr_thread = Thread.currentThread();
|
---|
600 |
|
---|
601 | while (curr_thread == paint) {
|
---|
602 |
|
---|
603 | repaint();
|
---|
604 |
|
---|
605 | Thread.sleep(2000);
|
---|
606 | curr_thread = Thread.currentThread();
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | } catch (Exception e) {
|
---|
611 |
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | });
|
---|
617 |
|
---|
618 | paint.start();
|
---|
619 |
|
---|
620 | }
|
---|
621 |
|
---|
622 | /** Stops threads for downloading and displaying images */
|
---|
623 | public void stop()
|
---|
624 | {
|
---|
625 | System.err.println("\n\n*** Stopping collage Applet: stopping threads...");
|
---|
626 | //download_thread_.stop();
|
---|
627 | //display_thread_.stop();
|
---|
628 | stopRunning();
|
---|
629 |
|
---|
630 | download_thread_ = null;
|
---|
631 | display_thread_ = null;
|
---|
632 |
|
---|
633 | }
|
---|
634 |
|
---|
635 | /** Destroys threads for downloading and displaying images */
|
---|
636 | public void destroyed()
|
---|
637 | {
|
---|
638 | download_thread_ = null;
|
---|
639 |
|
---|
640 | display_thread_ = null;
|
---|
641 |
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | /** Repaints the applet */
|
---|
646 | public void update(Graphics g)
|
---|
647 | {
|
---|
648 | // System.err.println("Update called");
|
---|
649 | paint(g);
|
---|
650 | }
|
---|
651 |
|
---|
652 | /** Repaints the applet using the paint method of the thread that is
|
---|
653 | * currently displaying images */
|
---|
654 | public void paint(Graphics g) {
|
---|
655 | if (display_thread_!=null)
|
---|
656 | {
|
---|
657 |
|
---|
658 | //display_thread_.display_collage();
|
---|
659 | display_thread_.paint(g);
|
---|
660 | }
|
---|
661 | else
|
---|
662 | {
|
---|
663 | System.err.println("Applet still trying to paint!!");
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 |
|
---|
668 | }
|
---|
669 |
|
---|
670 |
|
---|
671 |
|
---|
672 | /** Ensures a URL address (as string) has a protocol, host, and file.
|
---|
673 | *
|
---|
674 | * If the URL is a CGI script URL, it should be tidied up so that it is
|
---|
675 | * appropriate to tag attrib=value pairs on the end. This means it
|
---|
676 | * must either end with a "?" or (if it contains a question-mark
|
---|
677 | * internally) end with a "&". */
|
---|
678 | String tidy_URL(String address, boolean isCGI) {
|
---|
679 |
|
---|
680 | // System.err.println("tidy URL: " + address);
|
---|
681 |
|
---|
682 | // make sure the URL has protocol, host, and file
|
---|
683 | if (address.startsWith("http")) {
|
---|
684 | // the address has all the necessary components
|
---|
685 | } else if (address.startsWith("/")) {
|
---|
686 | // there is not protocol and host
|
---|
687 | URL document = getDocumentBase();
|
---|
688 | String port = "";
|
---|
689 | if (document.getPort()!=-1) {
|
---|
690 | port = ":" + document.getPort();
|
---|
691 | }
|
---|
692 | address = "http://" + document.getHost() + port + address;
|
---|
693 | } else {
|
---|
694 | // this URL is relative to the directory the document is in
|
---|
695 | URL document = getDocumentBase();
|
---|
696 | String directory = document.getFile();
|
---|
697 | int end = directory.lastIndexOf('/');
|
---|
698 | String port = "";
|
---|
699 | if (document.getPort()!=-1) {
|
---|
700 | port = ":" + document.getPort();
|
---|
701 | }
|
---|
702 | directory = directory.substring(0,end + 1);
|
---|
703 | address = "http://" + document.getHost() + port + directory + address;
|
---|
704 |
|
---|
705 | }
|
---|
706 |
|
---|
707 | // if the URL is a cgi script, make sure it has a "?" in ti,
|
---|
708 | // and that it ends with a "?" or "&"
|
---|
709 | if (isCGI) {
|
---|
710 | if (address.indexOf((int) '?') == -1) {
|
---|
711 | address = address + "?";
|
---|
712 | } else if (!address.endsWith("?")) {
|
---|
713 | address = address + "&";
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | return address;
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | public static void printUsage() {
|
---|
722 | System.err.println("Params needed include: --gsdlversion <3/2> [--statusbar 0/1] [--baseurl <GS3 DL baseURL>/--gwcgi <library.cgi URL>] --library l --collection c --classifier cl --imageType \".jpg%.png\" --imageMustNotHave \"interfaces/\" [--hrefMustHave \"LIBRARYNAME/sites/SITENAME/collect/COLNAME\"] -documentroot greenstone3 [--webswing <1/0>] [--verbosity <v>] --maxDepth 500 --maxDisplay 25 --refreshDelay 1500 --isJava2 auto --bgcolor \"#96c29a\" [--xtraParams <key1=value1&key2=value2&...]");
|
---|
723 | System.err.println("To run as webswing application, additionally pass in --webswing 1");
|
---|
724 | }
|
---|
725 |
|
---|
726 | // To also be able to run this applet as an application, need a main method
|
---|
727 | /**
|
---|
728 | * After building the GS3 Multimedia collection, try running this Application as:
|
---|
729 | java -cp ./web/applet/GsdlCollageApplet.jar:./web/WEB-INF/lib/log4j-1.2.8.jar:./web/WEB-INF/classes:./web/ext/webswing/api/webswing-api.jar org.greenstone.applet.GsdlCollageApplet.GsdlCollageApplet --statusbar 0 --baseurl "http://localhost:8383/greenstone3/" --library library --collection smallbea --gsdlversion 3 --hrefMustHave "library/sites/localsite/collect/smallbea" --documentroot greenstone3 --verbosity 3 --imageType ".jpg%.png" --imageMustNotHave "interfaces/" --classifier "CL2.3" --maxDepth 500 --maxDisplay 25 --refreshDelay 1500 --isJava2 auto --bgcolor "#96c29a" --width 645 --height 780
|
---|
730 | */
|
---|
731 | public static void main(String[] args) {
|
---|
732 | if(args.length == 0) {
|
---|
733 | printUsage();
|
---|
734 | } else if(args[0].equals("--help") || args[0].equals("-h")) {
|
---|
735 | printUsage();
|
---|
736 | } else {
|
---|
737 | JFrame frame = new JFrame("Collage Applet as Application");
|
---|
738 | GsdlCollageApplet collageApp = new GsdlCollageApplet();
|
---|
739 | frame.getContentPane().add(collageApp, BorderLayout.CENTER);
|
---|
740 | //frame.setSize(X_DIM, Y_DIM); //Y_DIM+EXTRA_HEIGHT);
|
---|
741 | frame.setVisible(true);
|
---|
742 | // https://stackoverflow.com/questions/19433358/difference-between-dispose-and-exit-on-close-in-java
|
---|
743 | // default: https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#EXIT_ON_CLOSE
|
---|
744 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Running as an application. But don't do EXIT_ON_CLOSE in Applet mode!
|
---|
745 |
|
---|
746 | // prepare to run the collage applet as an application
|
---|
747 | collageApp.applicationPreInit(args);
|
---|
748 | // It's now figured out the dimensions based on anything specified, so set frame size
|
---|
749 | frame.setSize(collageApp.X_DIM(), collageApp.Y_DIM()); //Y_DIM+EXTRA_HEIGHT);
|
---|
750 |
|
---|
751 | // status bar code. Not ideal, but had to add it here to get relative dimensions right
|
---|
752 | JLabel statusBar = collageApp.getStatusBar();
|
---|
753 | if(statusBar != null) {
|
---|
754 | collageApp.setLayout(new BorderLayout());
|
---|
755 |
|
---|
756 | frame.add(statusBar, BorderLayout.SOUTH);
|
---|
757 | Dimension d = statusBar.getSize();
|
---|
758 | d.height = EXTRA_HEIGHT;
|
---|
759 | d.width = collageApp.getWidth();
|
---|
760 | statusBar.setPreferredSize(d);
|
---|
761 | //statusBar.setPreferredSize(new Dimension(collageApp.getWidth(), EXTRA_HEIGHT));
|
---|
762 | }
|
---|
763 |
|
---|
764 | // Run it at last: manually calling (J)Applet methods init() and start()
|
---|
765 | collageApp.init();
|
---|
766 |
|
---|
767 | collageApp.showStatus("Collage application running");
|
---|
768 | collageApp.start();
|
---|
769 |
|
---|
770 | // When we terminate the application, need to manually call the applet method stop()
|
---|
771 | //https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html#windowfocuslistener
|
---|
772 | frame.addWindowListener(new WindowAdapter() {
|
---|
773 | public void windowClosing(WindowEvent e) {
|
---|
774 | if(collageApp.isWebswing) {
|
---|
775 | WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet - quitting now", null);
|
---|
776 | //org.webswing.toolkit.api.WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet about to QUIT !!", null);
|
---|
777 | }
|
---|
778 | collageApp.showStatus("Stopping threads");
|
---|
779 | System.err.println("\n\n*** Closing collage Application: stopping threads...");
|
---|
780 | collageApp.stopRunning();
|
---|
781 |
|
---|
782 | //collageApp.stop();
|
---|
783 | //collageApp.destroy();
|
---|
784 | }
|
---|
785 | });
|
---|
786 |
|
---|
787 | }
|
---|
788 | }
|
---|
789 | }
|
---|