1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * Phind.js -- the Phind java applet - modified to work with gsdl3 kjdon
|
---|
4 | *
|
---|
5 | *********************************************************************/
|
---|
6 |
|
---|
7 |
|
---|
8 | /*********************************************************************
|
---|
9 |
|
---|
10 | To use the applet, you'll need to embed it in a web page like this:
|
---|
11 |
|
---|
12 | <APPLET CODE="Phind.class" WIDTH=500 HEIGHT=500>
|
---|
13 |
|
---|
14 | <PARAM NAME=collection VALUE="fao.org">
|
---|
15 | <PARAM NAME=classifier VALUE="1">
|
---|
16 | <PARAM NAME=phindcgi VALUE="http://kowhai/cgi-bin/phindcgi">
|
---|
17 | <PARAM NAME=library VALUE="http://kowhai/cgi-bin/library">
|
---|
18 | <PARAM NAME=backdrop VALUE="http://kowhai/~paynter/transfer/phindtest/green1.jpg">
|
---|
19 | The Phind java applet.
|
---|
20 | </APPLET>
|
---|
21 |
|
---|
22 | There are a bunch of other parameters; these are described in the
|
---|
23 | getParameters method below. It is all done for you in Greenstone
|
---|
24 | in the document.dm macro file (the _phindapplet_ macro).
|
---|
25 |
|
---|
26 | You may have problems with Java applet security. Java applet's can only
|
---|
27 | open socket connections (including the HTTP connections the applet uses
|
---|
28 | to get data) to the same server the applet was loaded from. This means
|
---|
29 | that your phindcgi, library, and (optional) backdrop URLs must be on the
|
---|
30 | same machine that your web page was loaded from.
|
---|
31 |
|
---|
32 | **********************************************************************
|
---|
33 |
|
---|
34 | The applet comprises several classes:
|
---|
35 |
|
---|
36 | 1. Phind (this file) is the applet class, loaded by the browser.
|
---|
37 | It also handles network connections.
|
---|
38 | 2. ResultDisplay is a Panel that sits in the applet and displays
|
---|
39 | things; displays are connected in a doubly-linked list.
|
---|
40 | 3. ResultBox holds the results of a query. Result boxes are shown
|
---|
41 | to the user through ResultDisplays. Another doubly-linked list.
|
---|
42 | 4. ResultTitle acts as a caption to a ResultBox describing its contents.
|
---|
43 | 5. ResultCanvas is what the ResultBox data is drawn on. It draws the
|
---|
44 | words on the screen, and handles user input from the mouse.
|
---|
45 | 6. ResultItem represents a single result object (a phrase or document).
|
---|
46 | 7. PhindTitle is for drawing backdrops in ResultDisplays.
|
---|
47 |
|
---|
48 | **********************************************************************/
|
---|
49 |
|
---|
50 | /*
|
---|
51 | import java.awt.BorderLayout;
|
---|
52 | import java.awt.Button;
|
---|
53 | import java.awt.Choice;
|
---|
54 | import java.awt.Color;
|
---|
55 | import java.awt.Component;
|
---|
56 | import java.awt.Dimension;
|
---|
57 | import java.awt.event.ActionEvent;
|
---|
58 | import java.awt.FlowLayout;
|
---|
59 | import java.awt.Font;
|
---|
60 | import java.awt.GridLayout;
|
---|
61 | import java.awt.Image;
|
---|
62 | import java.awt.Label;
|
---|
63 | import java.awt.Panel;
|
---|
64 | import java.awt.TextField;
|
---|
65 | import java.awt.event.ActionListener;
|
---|
66 | import java.net.URL;
|
---|
67 | import java.io.DataInputStream;
|
---|
68 |
|
---|
69 | import java.net.Socket;
|
---|
70 | import java.net.InetAddress;
|
---|
71 | import java.net.UnknownHostException;
|
---|
72 | import java.io.IOException;
|
---|
73 |
|
---|
74 | import java.util.Vector;
|
---|
75 | import java.util.Date;
|
---|
76 |
|
---|
77 | import org.w3c.dom.Element;
|
---|
78 | import org.w3c.dom.Document;
|
---|
79 | //import javax.xml.parsers.*;
|
---|
80 | import org.xml.sax.InputSource;
|
---|
81 | import org.apache.xerces.parsers.DOMParser;
|
---|
82 |
|
---|
83 | */
|
---|
84 |
|
---|
85 | //public class Phind extends java.applet.Applet
|
---|
86 | // implements ActionListener {
|
---|
87 |
|
---|
88 | class Phind
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | // What is the collection called?
|
---|
92 | public String collection;
|
---|
93 |
|
---|
94 | // Which phind classifier are we using? (There may be more than one.)
|
---|
95 | public String classifier;
|
---|
96 |
|
---|
97 | // Internet address of phind resources
|
---|
98 | public String library_address, phindcgi_address;
|
---|
99 |
|
---|
100 | // Initial search term
|
---|
101 | public String initialSearch;
|
---|
102 |
|
---|
103 | // Number of phrases to retrieve at any one time
|
---|
104 | public int phraseBlockSize;
|
---|
105 |
|
---|
106 | // Appearance parameters
|
---|
107 | public boolean vertical;
|
---|
108 | public int depth;
|
---|
109 |
|
---|
110 | // Font
|
---|
111 | public int fontSize;
|
---|
112 | public String fontName;
|
---|
113 | public Font plainFont, boldFont;
|
---|
114 |
|
---|
115 | // Do we want a background image in the applet?
|
---|
116 | public boolean showImage;
|
---|
117 | public String backdrop_address;
|
---|
118 | public Image backgroundImage;
|
---|
119 | public boolean showBorder;
|
---|
120 |
|
---|
121 | // Colours
|
---|
122 | public Color panel_fg, panel_bg,
|
---|
123 | column_1_fg, column_1_bg,
|
---|
124 | column_2_fg, column_2_bg,
|
---|
125 | highlight_fg, highlight_bg,
|
---|
126 | thesaurus_fg, thesaurus_bg, thesaurus_bar_fg, thesaurus_bar_bg,
|
---|
127 | expansion_fg, expansion_bg, expansion_bar_fg, expansion_bar_bg,
|
---|
128 | document_fg, document_bg, document_bar_fg, document_bar_bg,
|
---|
129 | message_fg, message_bg;
|
---|
130 |
|
---|
131 | // Column dimensions
|
---|
132 | int column_1_width, column_2_width;
|
---|
133 |
|
---|
134 | // Where do we open new windows
|
---|
135 | String searchWindowName, documentWindowName;
|
---|
136 |
|
---|
137 | // the mode of operation
|
---|
138 | int mode;
|
---|
139 | final int initMode = 0;
|
---|
140 | final int idleMode = 1;
|
---|
141 | final int searchMode = 2;
|
---|
142 |
|
---|
143 | // Elements of the control panel
|
---|
144 | boolean showControlPanel;
|
---|
145 | Label titleLabel;
|
---|
146 | TextField wordField;
|
---|
147 | Button searchButton, prevButton, nextButton;
|
---|
148 |
|
---|
149 | // Holders for the downloaded data
|
---|
150 | Panel resultPanel;
|
---|
151 | ResultDisplay firstDisplay, lastDisplay;
|
---|
152 |
|
---|
153 | // The time at which the last query finished
|
---|
154 | Date lastQueryEndTime;
|
---|
155 |
|
---|
156 | // lastQueryEndTime is stored to ensure a 1 second gap between a query
|
---|
157 | // returning and a new one beginning. It is needed because the FAO
|
---|
158 | // folks in Rome have a huge lag, and frquently click several times
|
---|
159 | // while they wait; these clicks are turned into new queries, which
|
---|
160 | // they await again. It is no elegant solution, but it seems like the
|
---|
161 | // easiest, given that I don't know threads.
|
---|
162 | // 1. The search button is easy to disable, and is disabled when a
|
---|
163 | // socket connection is in progress.
|
---|
164 | // 2. ResutCanvas widgets can'r be similarly disabled because the
|
---|
165 | // browser hides or wipes them, which looks very bad.
|
---|
166 | // 3. I cannot just ignore clicks on canvasses because the browser
|
---|
167 | // caches the clicks while the socket connection is going on, and then
|
---|
168 | // sends them through afterwards, when the canvas is accepting clicks
|
---|
169 | // again.
|
---|
170 | // 4. Current sequence of events is to record the time the last query
|
---|
171 | // ends, then whenever a click happens make sure a second has past. if
|
---|
172 | // you double-click the the first query is sent, returns, end-tie is
|
---|
173 | // set, and the second (and any others made during query time) is
|
---|
174 | // *immediately* processed, but since 1 second hasn't past it is
|
---|
175 | // ignored.
|
---|
176 | */
|
---|
177 |
|
---|
178 | /*
|
---|
179 | public String getAppletInfo() {
|
---|
180 | return "Phind by Gordon Paynter ([email protected]). Copyright 1997-2000.";
|
---|
181 | }
|
---|
182 | */
|
---|
183 |
|
---|
184 |
|
---|
185 | constructor() {
|
---|
186 | /*
|
---|
187 | mode = initMode;
|
---|
188 |
|
---|
189 | // Read applet parameters
|
---|
190 | getParameters();
|
---|
191 |
|
---|
192 | // Initialise the user interface
|
---|
193 | setBackground(panel_bg);
|
---|
194 | lastQueryEndTime = new Date();
|
---|
195 |
|
---|
196 | // fonts used to output text
|
---|
197 | plainFont = new Font(fontName, Font.PLAIN, fontSize);
|
---|
198 | boldFont = new Font(fontName, Font.BOLD, fontSize);
|
---|
199 |
|
---|
200 | // The phind applet layout manager
|
---|
201 | setLayout(new BorderLayout());
|
---|
202 |
|
---|
203 | // Panel containing the displays is in the center of the display
|
---|
204 | resultPanel = new Panel();
|
---|
205 | if (vertical) {
|
---|
206 | resultPanel.setLayout(new GridLayout(depth,1,0,2));
|
---|
207 | } else {
|
---|
208 | System.out.println("horizontal");
|
---|
209 | resultPanel.setLayout(new GridLayout(1,depth,2,0));
|
---|
210 | }
|
---|
211 | add("Center", resultPanel);
|
---|
212 |
|
---|
213 | // Create ResultDisplays and place into the interface
|
---|
214 | ResultDisplay d1, d2 = null;
|
---|
215 | firstDisplay = new ResultDisplay(this, null);
|
---|
216 | resultPanel.add(firstDisplay);
|
---|
217 |
|
---|
218 | if (depth == 1) {
|
---|
219 | lastDisplay = firstDisplay;
|
---|
220 | } else {
|
---|
221 | d1 = firstDisplay;
|
---|
222 | for (int i = 2; i <= depth; i++) {
|
---|
223 | d2 = new ResultDisplay(this, d1);
|
---|
224 | resultPanel.add(d2);
|
---|
225 | d1 = d2;
|
---|
226 | }
|
---|
227 | lastDisplay = d2;
|
---|
228 | }
|
---|
229 |
|
---|
230 | // The control panel
|
---|
231 | initialiseControlPanel();
|
---|
232 |
|
---|
233 | // lets get started then
|
---|
234 | setStatus("Welcome to Phind.");
|
---|
235 | mode = idleMode;
|
---|
236 |
|
---|
237 | // Perform initial search, if requested
|
---|
238 | if (initialSearch.length() > 0) {
|
---|
239 | searchForWord(initialSearch);
|
---|
240 | }
|
---|
241 | */
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | // Display a message in the status bar
|
---|
246 | void setStatus(String status) {
|
---|
247 | showStatus(status);
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | public void actionPerformed(ActionEvent evt) {
|
---|
252 |
|
---|
253 | Component target = (Component)evt.getSource();
|
---|
254 | if (target==searchButton) {
|
---|
255 | System.out.println("search button pressed");
|
---|
256 | searchForWord(getSearchTerm());
|
---|
257 | } else if (target == wordField) {
|
---|
258 | System.out.println("word field entered");
|
---|
259 | searchForWord(getSearchTerm());
|
---|
260 | } else if (target == prevButton) {
|
---|
261 | System.out.println("prev button pressed");
|
---|
262 | shiftPrevious();
|
---|
263 | }else if (target == nextButton) {
|
---|
264 | System.out.println("prev button pressed");
|
---|
265 | shiftNext();
|
---|
266 | } else {
|
---|
267 | System.out.println("unknown action: " + evt.toString() );
|
---|
268 |
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | // Search for a word
|
---|
273 | //
|
---|
274 | // Called on two occasions:
|
---|
275 | // when the "Search" Button is pressed, or
|
---|
276 | // to perform an "initial search"
|
---|
277 | void searchForWord(String searchWord) {
|
---|
278 |
|
---|
279 | System.err.println("in searchforword!!");
|
---|
280 | if (mode == idleMode) {
|
---|
281 |
|
---|
282 | setSearchTerm(searchWord);
|
---|
283 |
|
---|
284 | // Convert the String from UTF8 charaters into
|
---|
285 | // an encoding that is okay for a URL.
|
---|
286 | searchWord = URLUTF8Encoder.encode(searchWord);
|
---|
287 |
|
---|
288 | // Look up the word
|
---|
289 | if (searchWord.length() > 1) {
|
---|
290 | setStatus("searching for \"" + searchWord + "\"");
|
---|
291 | firstDisplay.emptyContents();
|
---|
292 | ResultBox result = lookupPhraseOnServer(null, false, searchWord, searchWord, 2);
|
---|
293 |
|
---|
294 | // if there is an error, return
|
---|
295 | if (result == null) {
|
---|
296 | setStatus("No results for \"" + searchWord + "\"");
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | // display the result
|
---|
301 | result.display = firstDisplay.display(result);
|
---|
302 | result.setSize(result.display.getSize());
|
---|
303 | result.paintAll(result.getGraphics());
|
---|
304 | }
|
---|
305 |
|
---|
306 | enablePreviousAndNext();
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | // Search for a phrase
|
---|
312 | //
|
---|
313 | // If querymode is 2, the user has clicked on a phrase.
|
---|
314 | // If querymode is 3, the user has requested more phrases.
|
---|
315 | // If querymode is 4, the user has requested more documents.
|
---|
316 | void searchForPhrase(ResultBox source, String key, String phrase, int queryMode) {
|
---|
317 |
|
---|
318 | // System.out.println("searchForPhrase: " + key + " " + phrase + " " + queryMode);
|
---|
319 |
|
---|
320 | if (mode == idleMode) {
|
---|
321 |
|
---|
322 | // If we are going to replace the first ResultDisplay, then empty it
|
---|
323 | if (queryMode <= 2) {
|
---|
324 | if (source.display.next != null) source.display.next.emptyContents();
|
---|
325 | }
|
---|
326 |
|
---|
327 | // look up the word
|
---|
328 | setStatus("Searching for \"" + phrase + "\"");
|
---|
329 | ResultBox result = lookupPhraseOnServer(source, true, key, phrase, queryMode);
|
---|
330 | if (result == null) {
|
---|
331 | setStatus("No result for \"" + phrase + "\"");
|
---|
332 | return;
|
---|
333 | }
|
---|
334 |
|
---|
335 | // If this is not already displayed, display it in the last free spot
|
---|
336 | if (queryMode <= 2) {
|
---|
337 | result.display = lastDisplay.display(result);
|
---|
338 | result.setSize(result.display.getSize());
|
---|
339 | result.paintAll(result.getGraphics());
|
---|
340 | }
|
---|
341 |
|
---|
342 | enablePreviousAndNext();
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | // Look up a phrase (or symbol) on the server
|
---|
348 | //
|
---|
349 | // Arguments are the source of the query (a ResultBox, or null if the
|
---|
350 | // query comes from hitting the search button), the key to search for
|
---|
351 | // (the text of a phrase or a symbol number), the phrase as a string,
|
---|
352 | // and the query mode.
|
---|
353 | // Query modes are:
|
---|
354 | // 0 = obsolete
|
---|
355 | // 1 = obsolete
|
---|
356 | // 2 = get first N phrases and URLs,
|
---|
357 | // 3 = get another N phrases into same window
|
---|
358 | // 4 = get another N documents into same window
|
---|
359 | // 5 = get another N thesaurus links into same window
|
---|
360 |
|
---|
361 | ResultBox lookupPhraseOnServer(ResultBox source,
|
---|
362 | boolean keyKnown, String key, String phrase,
|
---|
363 | int queryMode) {
|
---|
364 | disableSearchButton();
|
---|
365 | mode = searchMode;
|
---|
366 | ResultBox r = null;
|
---|
367 |
|
---|
368 | if (queryMode <= 2) {
|
---|
369 | r = new ResultBox(this, collection, key, phrase, source);
|
---|
370 | } else if ((queryMode == 3) || (queryMode == 4) || (queryMode == 5)) {
|
---|
371 | r = source;
|
---|
372 | }
|
---|
373 |
|
---|
374 | try {
|
---|
375 | queryServer(keyKnown, key, queryMode, r);
|
---|
376 | } catch (Exception e) {
|
---|
377 | System.out.println("Phind query error: " + e.toString());
|
---|
378 | setStatus("Query error: " + e.toString());
|
---|
379 | mode = idleMode;
|
---|
380 | enableSearchButton();
|
---|
381 | return null;
|
---|
382 | }
|
---|
383 |
|
---|
384 | // The query is finished
|
---|
385 | setStatus(r.c.numberOfItems + " results for \"" + phrase + "\"");
|
---|
386 | mode = idleMode;
|
---|
387 | enableSearchButton();
|
---|
388 | lastQueryEndTime = new Date();
|
---|
389 |
|
---|
390 | return r;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | // Query the phindcgi program
|
---|
395 | //
|
---|
396 | // Send a query (a word or symbol number) to the server
|
---|
397 | // and pass the response to a ResultBox.
|
---|
398 |
|
---|
399 | void queryServer(boolean keyKnown, String word, int queryMode, ResultBox area)
|
---|
400 | throws IOException {
|
---|
401 |
|
---|
402 | // Build the query
|
---|
403 | String query = phindcgi_address + "c=" + collection + "&pc=" + classifier;
|
---|
404 |
|
---|
405 | if (keyKnown) {
|
---|
406 | query = query + "&ppnum=" + word;
|
---|
407 | } else {
|
---|
408 | query = query + "&ppnum=0" + "&pptext=" + word;
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | // Specify the set of results to return
|
---|
413 | int first_e = 0;
|
---|
414 | int last_e = 0;
|
---|
415 | int first_d = 0;
|
---|
416 | int last_d = 0;
|
---|
417 | int first_l = 0;
|
---|
418 | int last_l = 0;
|
---|
419 |
|
---|
420 | // the initial query
|
---|
421 | if (queryMode <= 2) {
|
---|
422 | last_e = phraseBlockSize;
|
---|
423 | last_d = phraseBlockSize;
|
---|
424 | last_l = phraseBlockSize;
|
---|
425 | }
|
---|
426 |
|
---|
427 | // add phrases to an existing result set
|
---|
428 | else if (queryMode == 3) {
|
---|
429 | first_e = area.nextPhraseBlock * phraseBlockSize;
|
---|
430 | area.nextPhraseBlock++;
|
---|
431 | last_e = area.nextPhraseBlock * phraseBlockSize;
|
---|
432 | }
|
---|
433 |
|
---|
434 | // add documents to existing result set
|
---|
435 | else if (queryMode == 4) {
|
---|
436 | first_d = area.nextDocumentBlock * phraseBlockSize;
|
---|
437 | area.nextDocumentBlock++;
|
---|
438 | last_d = area.nextDocumentBlock * phraseBlockSize;
|
---|
439 | }
|
---|
440 |
|
---|
441 | // add thesaurus links to existing result set
|
---|
442 | else if (queryMode == 5) {
|
---|
443 | first_l = area.nextThesaurusLinkBlock * phraseBlockSize;
|
---|
444 | area.nextThesaurusLinkBlock++;
|
---|
445 | last_l = area.nextThesaurusLinkBlock * phraseBlockSize;
|
---|
446 | }
|
---|
447 |
|
---|
448 | query = query + "&pfe=" + first_e + "&ple=" + last_e
|
---|
449 | + "&pfd=" + first_d + "&pld=" + last_d
|
---|
450 | + "&pfl=" + first_l + "&pll=" + last_l;
|
---|
451 |
|
---|
452 | // Send the query to the phindcgi program
|
---|
453 | System.out.println("1:sending query: " + query);
|
---|
454 | try {
|
---|
455 | URL phindcgi = new URL(query);
|
---|
456 | DataInputStream in = new DataInputStream(phindcgi.openStream());
|
---|
457 | DOMParser parser = new DOMParser();
|
---|
458 | parser.parse(new InputSource(in));
|
---|
459 | Document data_doc = parser.getDocument();
|
---|
460 | Element data_elem = data_doc.getDocumentElement();
|
---|
461 | area.parseXML(data_elem);
|
---|
462 | in.close();
|
---|
463 | } catch (Exception e) {
|
---|
464 | System.err.println( "Error sending query to phindcgi: " + e);
|
---|
465 | e.printStackTrace();
|
---|
466 | }
|
---|
467 | area.repaint();
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | // Tidy up URLs
|
---|
472 | //
|
---|
473 | // Ensure a URL address (as string) has a protocol, host, and file.
|
---|
474 | //
|
---|
475 | // If the URL is a CGI script URL, it should be tidied up so that it is
|
---|
476 | // appropriate to tage attrib=value pairs on the end. This means it
|
---|
477 | // must either end with a "?" or (if it contains a question-mark
|
---|
478 | // internally) end with a "&".
|
---|
479 | String tidy_URL(String address, boolean isCGI) {
|
---|
480 |
|
---|
481 | // System.err.println("tidy URL: " + address);
|
---|
482 |
|
---|
483 | // make sure the URL has protocol, host, and file
|
---|
484 | if (address.startsWith("http")) {
|
---|
485 | // the address has all the necessary components
|
---|
486 | } else if (address.startsWith("/")) {
|
---|
487 | // there is not protocol and host
|
---|
488 | URL document = getDocumentBase();
|
---|
489 | String port = "";
|
---|
490 | if (document.getPort()!=-1) {
|
---|
491 | port = ":" + document.getPort();
|
---|
492 | }
|
---|
493 | address = "http://" + document.getHost() + port + address;
|
---|
494 | } else {
|
---|
495 | // this URL is relative to the directory the document is in
|
---|
496 | URL document = getDocumentBase();
|
---|
497 | String directory = document.getFile();
|
---|
498 | int end = directory.lastIndexOf('/');
|
---|
499 | String port = "";
|
---|
500 | if (document.getPort()!=-1) {
|
---|
501 | port = ":" + document.getPort();
|
---|
502 | }
|
---|
503 | directory = directory.substring(0,end + 1);
|
---|
504 | address = "http://" + document.getHost() + port + directory + address;
|
---|
505 |
|
---|
506 | }
|
---|
507 |
|
---|
508 | // if the URL is a cgi script, make sure it has a "?" in ti,
|
---|
509 | // and that it ends with a "?" or "&"
|
---|
510 | if (isCGI) {
|
---|
511 | if (address.indexOf((int) '?') == -1) {
|
---|
512 | address = address + "?";
|
---|
513 | } else if (!address.endsWith("?")) {
|
---|
514 | address = address + "&";
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | return address;
|
---|
519 | }
|
---|
520 |
|
---|
521 |
|
---|
522 |
|
---|
523 | // Open an arbitrary web page
|
---|
524 | void displayWebPage(String address, String window) {
|
---|
525 | try {
|
---|
526 | URL url= new URL(address);
|
---|
527 | if (window.length() > 0) {
|
---|
528 | getAppletContext().showDocument(url, window);
|
---|
529 | } else {
|
---|
530 | getAppletContext().showDocument(url);
|
---|
531 | }
|
---|
532 | } catch (Exception e) {
|
---|
533 | System.out.println("Cannot open web page: " + e.toString());
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | // Get the applet parameters
|
---|
539 | void getParameters() {
|
---|
540 |
|
---|
541 | // What is this collection called?
|
---|
542 | collection = parameterValue("collection");
|
---|
543 | System.out.println("Phind collection: " + collection);
|
---|
544 |
|
---|
545 | // Which of the collection's classifiers are we using?
|
---|
546 | classifier = parameterValue("classifier", "1");
|
---|
547 | System.out.println("Phind classifier: " + classifier);
|
---|
548 |
|
---|
549 | // Where is the Greenstone library
|
---|
550 | library_address = parameterValue("library");
|
---|
551 | library_address = tidy_URL(library_address, true);
|
---|
552 | System.out.println("Phind library: " + library_address);
|
---|
553 |
|
---|
554 | // Where is the phind CGI script
|
---|
555 | // we assume this is relative to the greenstone library
|
---|
556 | phindcgi_address = parameterValue("library")+parameterValue("phindcgi");
|
---|
557 | phindcgi_address = tidy_URL(phindcgi_address, true);
|
---|
558 | System.out.println("Phind phindcgi: " + phindcgi_address);
|
---|
559 |
|
---|
560 |
|
---|
561 | // Is there a default search term?
|
---|
562 | initialSearch = parameterValue("initial_search", "");
|
---|
563 |
|
---|
564 | // Should we display the control panel
|
---|
565 | showControlPanel = true;
|
---|
566 | if (parameterValue("control_panel", "show").toLowerCase().equals("hide")) {
|
---|
567 | showControlPanel = false;
|
---|
568 | }
|
---|
569 |
|
---|
570 | // Should we show a background image?
|
---|
571 | backdrop_address = parameterValue("backdrop", "");
|
---|
572 | if (backdrop_address.length() > 0) {
|
---|
573 | backdrop_address = tidy_URL(backdrop_address, false);
|
---|
574 | System.out.println("Phind backdrop URL: " + backdrop_address);
|
---|
575 |
|
---|
576 | try {
|
---|
577 | URL backdrop_url = new URL(backdrop_address);
|
---|
578 | backgroundImage = getImage(backdrop_url);
|
---|
579 | showImage = true;
|
---|
580 | } catch (Exception e) {
|
---|
581 | System.out.println("Phind could not load " + backdrop_address);
|
---|
582 | showImage = false;
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | // Should we draw a border?
|
---|
587 | showBorder = parameterValue("border", "on").equals("off");
|
---|
588 |
|
---|
589 | // Are the windows arranged vertically or horizontally
|
---|
590 | if (parameterValue("orientation", "vertical").toLowerCase().startsWith("hori")) {
|
---|
591 | vertical = false;
|
---|
592 | } else {
|
---|
593 | vertical = true;
|
---|
594 | }
|
---|
595 |
|
---|
596 | // How many phind windows are there?
|
---|
597 | depth = parameterValue("depth", 3);
|
---|
598 |
|
---|
599 | // Result sort order
|
---|
600 | // Standard is "LlEeDd", expansion-first is "EeLlDd"
|
---|
601 | String order = parameterValue("resultorder", "standard");
|
---|
602 | if (!order.equals("standard")) {
|
---|
603 | int next = 20;
|
---|
604 | ResultItem.sortMessage = next;
|
---|
605 | for (int x = 0; x < order.length(); x++) {
|
---|
606 | if (order.charAt(x) == ',') {
|
---|
607 | next--;
|
---|
608 | } else if (order.charAt(x) == 'L') {
|
---|
609 | ResultItem.sortLinkItem = next;
|
---|
610 | } else if (order.charAt(x) == 'l') {
|
---|
611 | ResultItem.sortMoreLinks = next;
|
---|
612 | } else if (order.charAt(x) == 'E') {
|
---|
613 | ResultItem.sortPhraseItem = next;
|
---|
614 | } else if (order.charAt(x) == 'e') {
|
---|
615 | ResultItem.sortMorePhrases = next;
|
---|
616 | } else if (order.charAt(x) == 'D') {
|
---|
617 | ResultItem.sortDocumentItem = next;
|
---|
618 | } else if (order.charAt(x) == 'd') {
|
---|
619 | ResultItem.sortMoreDocuments = next;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | System.out.println("link: " + ResultItem.sortLinkItem);
|
---|
623 | System.out.println("exps: " + ResultItem.sortPhraseItem);
|
---|
624 | System.out.println("docs: " + ResultItem.sortDocumentItem);
|
---|
625 |
|
---|
626 | }
|
---|
627 |
|
---|
628 | // How many phrases should we fetch at any given time?
|
---|
629 | phraseBlockSize = parameterValue("blocksize", 10);
|
---|
630 |
|
---|
631 | // What font should we use?
|
---|
632 | fontSize = parameterValue("fontsize", 10);
|
---|
633 | fontName = parameterValue("fontname", "Helvetica");
|
---|
634 |
|
---|
635 | // Column dimensions
|
---|
636 | column_1_width = parameterValue("first_column_width", 6);
|
---|
637 | column_2_width = parameterValue("second_column_width", column_1_width);
|
---|
638 |
|
---|
639 | // Where do we open new windows
|
---|
640 | searchWindowName = parameterValue("search_window", "phindsearch");
|
---|
641 | documentWindowName = parameterValue("document_window", "phinddoc");
|
---|
642 |
|
---|
643 | // Colours
|
---|
644 | panel_fg = parameterValue("panel_fg", Color.black);
|
---|
645 | panel_bg = parameterValue("panel_bg", Color.white);
|
---|
646 |
|
---|
647 | highlight_bg = parameterValue("highlight_bg", Color.yellow);
|
---|
648 |
|
---|
649 | expansion_fg = parameterValue("expansion_fg", Color.black);
|
---|
650 | thesaurus_fg = parameterValue("thesaurus_fg", new Color(0, 100, 0));
|
---|
651 | document_fg = parameterValue("document_fg", Color.blue);
|
---|
652 |
|
---|
653 | thesaurus_bar_fg = parameterValue("thesaurus_bar_fg", Color.black);
|
---|
654 | expansion_bar_fg = parameterValue("expansion_bar_fg", Color.black);
|
---|
655 | document_bar_fg = parameterValue("document_bar_fg", Color.black);
|
---|
656 |
|
---|
657 | thesaurus_bar_bg = parameterValue("thesaurus_bar_bg", new Color(160, 160, 190));
|
---|
658 | expansion_bar_bg = parameterValue("expansion_bar_bg", new Color(255, 200, 200));
|
---|
659 | document_bar_bg = parameterValue("document_bar_bg", new Color(150, 193, 156));
|
---|
660 |
|
---|
661 | column_1_fg = parameterValue("first_column_fg", Color.black);
|
---|
662 | column_1_bg = parameterValue("first_column_bg", new Color(235, 245, 235));
|
---|
663 | column_2_fg = parameterValue("second_column_fg", Color.black);
|
---|
664 | column_2_bg = parameterValue("second_column_bg", new Color(200, 220, 200));
|
---|
665 |
|
---|
666 | message_fg = parameterValue("message_fg", Color.black);
|
---|
667 | message_bg = parameterValue("message_bg", Color.white);
|
---|
668 |
|
---|
669 | // Colours I don't use, yet
|
---|
670 | // thesaurus_bg = parameterValue("thesaurus_bg", Color.white);
|
---|
671 | // expansion_bg = parameterValue("expansion_bg", Color.white);
|
---|
672 | // document_bg = parameterValue("document_bg", Color.white);
|
---|
673 | }
|
---|
674 |
|
---|
675 | // Get the value of a parameter given its name.
|
---|
676 | // There are many types of parameters, hence the variety of functions.
|
---|
677 |
|
---|
678 | // Get a REQUIRED string. Stop the applet if we cannot.
|
---|
679 | String parameterValue(String name) {
|
---|
680 | try {
|
---|
681 | return getParameter(name);
|
---|
682 | } catch (Exception e) {
|
---|
683 | System.err.println("Phind: you must give a parameter for \""
|
---|
684 | + name + "\". Stopping.");
|
---|
685 | stop();
|
---|
686 | }
|
---|
687 | return "";
|
---|
688 | }
|
---|
689 |
|
---|
690 | // Get an optional parameter. Return a default if we cannot.
|
---|
691 | String parameterValue(String name, String defaultValue) {
|
---|
692 | String text = getParameter(name);
|
---|
693 | if (text == null) {
|
---|
694 | return defaultValue;
|
---|
695 | }
|
---|
696 | System.out.println("Phind " + name + ": " + text);
|
---|
697 | return text;
|
---|
698 | }
|
---|
699 |
|
---|
700 | int parameterValue(String name, int defaultValue) {
|
---|
701 | int value;
|
---|
702 | try {
|
---|
703 | value = Integer.parseInt(getParameter(name));
|
---|
704 | } catch (Exception e) {
|
---|
705 | return defaultValue;
|
---|
706 | }
|
---|
707 | System.out.println("Phind " + name + ": " + value);
|
---|
708 | return value;
|
---|
709 | }
|
---|
710 |
|
---|
711 | Color parameterValue(String name, Color defaultValue) {
|
---|
712 |
|
---|
713 | String text = getParameter(name);
|
---|
714 | if (text == null) {
|
---|
715 | return defaultValue;
|
---|
716 | }
|
---|
717 | text = text.toLowerCase();
|
---|
718 |
|
---|
719 | // a number of the form "#ddffee"
|
---|
720 | if (text.startsWith("#") && (text.length() == 7)) {
|
---|
721 | text = text.substring(1);
|
---|
722 | int r, g, b;
|
---|
723 | try {
|
---|
724 | r = Integer.parseInt(text.substring(0,2), 16);
|
---|
725 | g = Integer.parseInt(text.substring(2,4), 16);
|
---|
726 | b = Integer.parseInt(text.substring(4), 16);
|
---|
727 | return new Color(r, g, b);
|
---|
728 | } catch (Exception e) {
|
---|
729 | return defaultValue;
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | // a known Java colour string
|
---|
734 | else if (text.equals("black")) { return Color.black; }
|
---|
735 | else if (text.equals("blue")) { return Color.blue; }
|
---|
736 | else if (text.equals("cyan")) { return Color.cyan; }
|
---|
737 | else if (text.equals("darkgray")) { return Color.darkGray; }
|
---|
738 | else if (text.equals("gray")) { return Color.gray; }
|
---|
739 | else if (text.equals("green")) { return Color.green; }
|
---|
740 | else if (text.equals("lightgray")) { return Color.lightGray; }
|
---|
741 | else if (text.equals("magenta")) { return Color.magenta; }
|
---|
742 | else if (text.equals("orange")) { return Color.orange; }
|
---|
743 | else if (text.equals("pink")) { return Color.pink; }
|
---|
744 | else if (text.equals("red")) { return Color.red; }
|
---|
745 | else if (text.equals("white")) { return Color.white; }
|
---|
746 | else if (text.equals("yellow")) { return Color.yellow; }
|
---|
747 |
|
---|
748 | return defaultValue;
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | // Control panel operations
|
---|
753 |
|
---|
754 | // Initialise the control panel
|
---|
755 | void initialiseControlPanel() {
|
---|
756 |
|
---|
757 | if (showControlPanel) {
|
---|
758 | Panel p1 = new Panel();
|
---|
759 | add("North", p1);
|
---|
760 | p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
|
---|
761 |
|
---|
762 | searchButton = new Button("Search");
|
---|
763 | searchButton.setFont(boldFont);
|
---|
764 | //searchButton.setEnabled(true);
|
---|
765 | searchButton.addActionListener(this);
|
---|
766 | p1.add(searchButton);
|
---|
767 |
|
---|
768 | Label tempLabel = new Label(" for");
|
---|
769 | tempLabel.setFont(boldFont);
|
---|
770 | p1.add(tempLabel);
|
---|
771 |
|
---|
772 | wordField = new TextField(12);
|
---|
773 | wordField.setFont(boldFont);
|
---|
774 | wordField.addActionListener(this);
|
---|
775 | p1.add(wordField);
|
---|
776 |
|
---|
777 | Label temp2 = new Label(" ");
|
---|
778 | p1.add(temp2);
|
---|
779 |
|
---|
780 | prevButton = new Button("Previous");
|
---|
781 | prevButton.setFont(boldFont);
|
---|
782 | prevButton.addActionListener(this);
|
---|
783 | prevButton.setEnabled(false);
|
---|
784 |
|
---|
785 | p1.add(prevButton);
|
---|
786 |
|
---|
787 | nextButton = new Button(" Next ");
|
---|
788 | nextButton.setFont(boldFont);
|
---|
789 | nextButton.addActionListener(this);
|
---|
790 | nextButton.setEnabled(false);
|
---|
791 | p1.add(nextButton);
|
---|
792 |
|
---|
793 | }
|
---|
794 | }
|
---|
795 |
|
---|
796 | // Button and field functionality
|
---|
797 |
|
---|
798 | // Enable and disable the word field
|
---|
799 | void enableSearchButton() {
|
---|
800 | if (showControlPanel) {
|
---|
801 | searchButton.setEnabled(true);
|
---|
802 | }
|
---|
803 | }
|
---|
804 | void disableSearchButton() {
|
---|
805 | if (showControlPanel) {
|
---|
806 | searchButton.setEnabled(false);
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | // Get and set the search text in the wordField
|
---|
811 | String getSearchTerm() {
|
---|
812 | if (showControlPanel) {
|
---|
813 | return wordField.getText();
|
---|
814 | } else {
|
---|
815 | return initialSearch;
|
---|
816 | }
|
---|
817 | }
|
---|
818 | void setSearchTerm(String word) {
|
---|
819 | if (showControlPanel) {
|
---|
820 | wordField.setText(word);
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | // Enable or disable the "Previous" and "Next" buttons
|
---|
825 | void enablePreviousAndNext() {
|
---|
826 | if (showControlPanel) {
|
---|
827 | Component c = firstDisplay.current;
|
---|
828 | if (c.getClass().getName().endsWith("ResultBox")) {
|
---|
829 | if (((ResultBox) c).prevBoxExists()) {
|
---|
830 | prevButton.setEnabled(true);
|
---|
831 | } else {
|
---|
832 | prevButton.setEnabled(false);
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 | c = lastDisplay.current;
|
---|
837 | if (c.getClass().getName().endsWith("ResultBox")) {
|
---|
838 | if (((ResultBox) c).nextBoxExists()) {
|
---|
839 | nextButton.setEnabled(true);
|
---|
840 | } else {
|
---|
841 | nextButton.setEnabled(false);
|
---|
842 | }
|
---|
843 | }
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | // Shift to previous box
|
---|
848 | //
|
---|
849 | // If the user clicks "Previous" then scroll up.
|
---|
850 | void shiftPrevious() {
|
---|
851 |
|
---|
852 | Component c = firstDisplay.current;
|
---|
853 | if (c.getClass().getName().endsWith("ResultBox")) {
|
---|
854 |
|
---|
855 | ResultBox b = (ResultBox) c;
|
---|
856 | if (b.prevBoxExists()) {
|
---|
857 | b = b.prev;
|
---|
858 |
|
---|
859 | // empty all the displays
|
---|
860 | firstDisplay.emptyContents();
|
---|
861 |
|
---|
862 | // add as many result boxes as there are displays
|
---|
863 | for (int i = 1 ; ((i <= depth) && (b != null)); i++) {
|
---|
864 | lastDisplay.display(b);
|
---|
865 | b.setSize(b.display.getSize());
|
---|
866 | b.paintAll(b.getGraphics());
|
---|
867 | b = b.next;
|
---|
868 | }
|
---|
869 | }
|
---|
870 | }
|
---|
871 | enablePreviousAndNext();
|
---|
872 | }
|
---|
873 |
|
---|
874 | // Shift to next box
|
---|
875 | //
|
---|
876 | // If the user clicks "Next" then scroll down if possible
|
---|
877 | void shiftNext() {
|
---|
878 |
|
---|
879 | Component c = lastDisplay.current;
|
---|
880 | if (c.getClass().getName().endsWith("ResultBox")) {
|
---|
881 |
|
---|
882 | ResultBox b = (ResultBox) c;
|
---|
883 | if (b.nextBoxExists()) {
|
---|
884 |
|
---|
885 | // find the new "first" displayed box
|
---|
886 | c = firstDisplay.current;
|
---|
887 | b = (ResultBox) c;
|
---|
888 | b = b.next;
|
---|
889 |
|
---|
890 | // empty all the displays
|
---|
891 | firstDisplay.emptyContents();
|
---|
892 |
|
---|
893 | // add as many result boxes as there are displays
|
---|
894 | for (int i = 1 ; ((i <= depth) && (b != null)); i++) {
|
---|
895 | lastDisplay.display(b);
|
---|
896 | b.setSize(b.display.getSize());
|
---|
897 | b.paintAll(b.getGraphics());
|
---|
898 | b = b.next;
|
---|
899 | }
|
---|
900 | }
|
---|
901 | }
|
---|
902 | enablePreviousAndNext();
|
---|
903 | }
|
---|
904 |
|
---|
905 |
|
---|
906 |
|
---|
907 | */
|
---|
908 |
|
---|
909 | }
|
---|