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