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