source: main/trunk/greenstone3/src/java/org/greenstone/applet/phind/JResultBox.java@ 38782

Last change on this file since 38782 was 38782, checked in by anupama, 4 months ago

First commit related to attempting to move (applet) Phind to Webswing. Unfortunately, Phind was an awt applet and not a JApplet, so the first step was to make a JApplet out of it as Webswing only supports swing and not awt as Dr Bainbridge had explained. Then I thought, should I make it a regular Java application, since I was going to be reworking a copy of the code anyway (the new classes have a J prefix in front of them to indicate they use Java Swing). I couldn't find Dr Bainbridge to ask, so I found out it's possible to write a class as an (J)Applet and as an Application (just need a main method), so I've tried to recreate it as an application. I think I've got it working to the point where I beleive I'm hitting the same error the original Applet hit when run through java's appletviewer commandline tool.

File size: 11.6 KB
Line 
1/**********************************************************************
2 *
3 * ResultBox.java -- a list of phrases in the Phind java interface
4 *
5 * Copyright 1997-2000 Gordon W. Paynter
6 * Copyright 2000 The New Zealand Digital Library Project
7 *
8 * A component of the Greenstone digital library software
9 * from the New Zealand Digital Library Project at the
10 * University of Waikato, New Zealand.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *********************************************************************/
27
28/*********************************************************************
29
30This class is used in the Phind java applet (Phind.java).
31
32A ResultBox holds the results of a query to phindcgi. They deal mostly
33with the information content of the query, and have methods for parsing the
34input into phrase and document items. They have little do with display:
35ResultBoxes are shown to the user through "ResultDisplay" panels, and are
36drawn using "ResultCanvas" objects.
37
38**********************************************************************/
39package org.greenstone.applet.phind;
40
41import javax.swing.JPanel;
42import javax.swing.JScrollPane;
43import javax.swing.JScrollBar;
44import javax.swing.JLabel;
45
46import java.awt.BorderLayout;
47
48import java.awt.AWTEvent;
49import java.awt.event.AdjustmentListener;
50import java.awt.event.AdjustmentEvent;
51import java.net.*;
52import java.applet.*;
53
54import java.util.Vector;
55
56import org.w3c.dom.Element;
57import org.w3c.dom.Node;
58import org.w3c.dom.NodeList;
59
60public class JResultBox extends JPanel
61 implements AdjustmentListener {
62
63 // Objects at a "higher" level than this one
64 JPhind phind;
65 JResultDisplay display;
66
67 // Surrounding objects
68 JResultBox prev, next;
69
70 // The components contained by this one.
71 JResultCanvas c;
72 JResultTitle t;
73 JScrollPane scrollpane;
74 JScrollBar s;
75 JPanel label;
76
77 // The key identifying the phrase displayed, with its text and the
78 // collection from which it is drawn
79 String searchKey, searchPhrase, searchCollection;
80
81 // The total frequency, expansion frequency, and document frequency
82 // of the phrase
83 int numberOfOccurances;
84 int numberOfExpansions;
85 int numberOfDocuments;
86 int numberOfThesaurusLinks;
87
88 // The number of phrases and documents retrieved, and the number of
89 // times the user has requested more phrases or documents.
90 int expansionsRetrieved;
91 int documentsRetrieved;
92 int thesaurusLinksRetrieved;
93 int nextPhraseBlock;
94 int nextDocumentBlock;
95 int nextThesaurusLinkBlock;
96
97 int mode;
98 final int initMode = 0;
99 public final int emptyMode = 1;
100 final int loadingMode = 2;
101 final int finishedMode = 3;
102
103 String buffer;
104 boolean finished;
105
106
107 // Create a ResultBox
108 // given details of the search that generated it.
109 JResultBox(JPhind p, String collect, String key, String phrase, JResultBox rb) {
110
111 super();
112 mode = initMode;
113
114 phind = p;
115 display = null;
116 next = null;
117 prev = rb;
118 if (prev != null) prev.next = this;
119
120 searchKey = key;
121 searchPhrase = phrase;
122 searchCollection = collect;
123
124 numberOfOccurances = -1;
125 numberOfExpansions = -1;
126 numberOfDocuments = -1;
127 numberOfThesaurusLinks = -1;
128
129 expansionsRetrieved = 0;
130 documentsRetrieved = 0;
131 thesaurusLinksRetrieved = 0;
132
133 nextPhraseBlock = 1;
134 nextDocumentBlock = 1;
135 nextThesaurusLinkBlock = 1;
136
137 // Porting from awt to swing
138 // http://fizyka.umk.pl/~jacek/docs/javatutorial/uiswing/converting/how.html
139 this.setLayout(new BorderLayout());
140
141 scrollpane = new JScrollPane(this);//,
142 //JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
143 //JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
144 s = new JScrollBar(JScrollBar.VERTICAL);
145 scrollpane.setVerticalScrollBar(s);
146
147 disableScrollbar();
148 scrollpane.getVerticalScrollBar().addAdjustmentListener(this);
149
150 c = new JResultCanvas(this);
151 this.add(c, BorderLayout.CENTER);
152
153
154 t = new JResultTitle(this);
155 this.add(t, BorderLayout.NORTH);
156
157 buffer = "";
158 finished = false;
159 mode = emptyMode;
160 }
161
162 static String describeContents(String phrase, String c) {
163 return( "\"" + phrase + "\" in " + c + ".");
164 }
165
166 String describeContents() {
167 return( describeContents(searchPhrase, searchCollection) );
168 }
169
170
171 // Reset the contents of the box
172 void resetBox( ) {
173 buffer = "";
174 finished = false;
175 c.resetCanvas();
176 disableScrollbar();
177 mode = emptyMode;
178
179 numberOfExpansions = -1;
180 numberOfDocuments = -1;
181 numberOfThesaurusLinks = -1;
182
183 expansionsRetrieved = 0;
184 documentsRetrieved = 0;
185 thesaurusLinksRetrieved = 0;
186 }
187
188 void setStatus( String status ) {
189 phind.setStatus(status);
190 }
191
192 void disableScrollbar() {
193 JScrollBar s = scrollpane.getVerticalScrollBar();
194 if (s.isEnabled()) {
195 s.setValues(0, 1, 0, 1);
196 s.setUnitIncrement(1);
197 s.setBlockIncrement(1);
198 s.setEnabled(false);
199 }
200 }
201
202 // Are there displays previous to and after this?
203 public boolean prevBoxExists () {
204 return (prev != null);
205 }
206 public boolean nextBoxExists () {
207 return (next != null);
208 }
209
210
211 // Look up a phrase
212 // Phrase lookups are passed on to the Phind applet itself.
213 void lookupPhrase(String key, String phrase, int queryMode) {
214 buffer = "";
215 finished = false;
216 phind.searchForPhrase(this, key, phrase, queryMode);
217 t.repaint();
218 }
219
220 /* // if uncommenting, check to port this from awt to swing
221 public void processEvent(AWTEvent event) {
222 // System.out.println("event: " + event.toString());
223 if ( (event.target == s) &&
224 ( (event.id == Event.SCROLL_ABSOLUTE) ||
225 (event.id == Event.SCROLL_LINE_DOWN) ||
226 (event.id == Event.SCROLL_LINE_UP) ||
227 (event.id == Event.SCROLL_PAGE_DOWN) ||
228 (event.id == Event.SCROLL_PAGE_UP) ) ) {
229 c.repaint();
230
231 } else {
232 super.processEvent(event);
233 }
234 } */
235
236 // https://stackoverflow.com/questions/2015795/using-javas-jcomponent-repaint
237 public void adjustmentValueChanged(AdjustmentEvent evt) {
238 c.repaint();
239 }
240
241 void parseXML(Element data) {
242
243 //System.out.println("phinddata:"+data.toString());
244
245 //String id="", phrase="";
246 //int df=0, ef=0, lf=0, tf=0;
247
248 searchKey = data.getAttribute("id");
249
250 numberOfDocuments = Integer.valueOf(data.getAttribute("df")).intValue();
251 numberOfExpansions = Integer.valueOf(data.getAttribute("ef")).intValue();
252 numberOfThesaurusLinks = Integer.valueOf(data.getAttribute("lf")).intValue();
253 numberOfOccurances = Integer.valueOf(data.getAttribute("tf")).intValue();
254
255 //searchPhrase = "cat"; // for now
256
257 // go through children
258 Node e = data.getFirstChild();
259 while (e!=null) {
260 String node_name = e.getNodeName();
261 if (node_name.equals("phrase")) {
262 // the value of the phrase
263 searchPhrase = getNodeText(e);
264 } else if (node_name.equals("expansionList")) {
265
266 NodeList expansions = ((Element)e).getElementsByTagName("expansion");
267 for (int i=0; i<expansions.getLength(); i++) {
268 processExpansionElement((Element)expansions.item(i));
269 }
270 } else if (node_name.equals("documentList")) {
271
272 NodeList documents = ((Element)e).getElementsByTagName("document");
273 for (int i=0; i<documents.getLength(); i++) {
274 processDocumentElement((Element)documents.item(i));
275 }
276
277
278 } else if (node_name.equals("thesaurusList")) {
279
280 NodeList thesaurai = ((Element)e).getElementsByTagName("thesaurus");
281 for (int i=0; i<thesaurai.getLength(); i++) {
282 processThesaurusElement((Element)thesaurai.item(i));
283 }
284
285 } else {
286 System.out.println("error in phinddata format, have unwanted node, "+node_name);
287 }
288 e = e.getNextSibling();
289 }
290 // finished parsing, update the getMoreDocs markers
291 c.updateMarkers();
292 }
293
294
295 /** Add an expansion tag
296 *
297 * Given a string containing an XML expansion element of the form:
298 * <expansion num="3" id="8421" prefix="PEOPLE and" suffix="" tf="3" df="3"><suffix></suffix><prefix>PEOPLE</prefix></expansion>
299 *
300 * Create a new ResultItemPhrase for display
301 *
302 * Return true if successful, otherwise false. */
303 boolean processExpansionElement(Element e) {
304
305 String num = "", id = "", tf = "", df = "",
306 prefix = "", body = "", suffix = "";
307
308 body = searchPhrase;
309
310 id = e.getAttribute("id");
311 tf = e.getAttribute("tf");
312 df = e.getAttribute("df");
313 num = e.getAttribute("num");
314
315 // get prefix child
316 Node prefix_node = getChildByTagName(e, "prefix");
317 if (prefix_node!=null) {
318 prefix = getNodeText(prefix_node);
319 }
320 Node suffix_node = getChildByTagName(e, "suffix");
321 if (suffix_node !=null) {
322 suffix = getNodeText(suffix_node);
323 }
324
325 ResultItemPhrase ri = new ResultItemPhrase(id, tf, df, prefix, body, suffix);
326
327 if (c.addResultItem(ri)) {
328 expansionsRetrieved++;
329 return true;
330 }
331
332 return false;
333 }
334
335 /** Add a document tag
336 *
337 * Given an XML Element of the form:
338 * <document num="2" hash="HASH424e64b811fdad933be69c" freq="1"><title>CONTENTS</title></document>
339 *
340 * Create a new ResultItemDocument for display
341 *
342 * Return true if successful, otherwise false. */
343
344 boolean processDocumentElement(Element d ) {
345 // why do we have num??
346 String num = "", hash = "", freq = "", title = "";
347
348 num = d.getAttribute("num");
349 freq = d.getAttribute("freq");
350 hash = d.getAttribute("hash");
351
352 Node title_node = getChildByTagName(d, "title");
353 if (title_node != null) {
354 title = getNodeText(title_node);
355 }
356 // Create a new ResultItem and add it to the display
357 ResultItemDocument ri = new ResultItemDocument(hash, title, freq);
358
359 if (c.addResultItem(ri)) {
360 documentsRetrieved++;
361 return true;
362 }
363
364 return false;
365 }
366
367 /** Add a thesaurus tag
368 *
369 * Given an XML element of the form:
370 *
371 * <thesaurus num="3" id="36506" tf="0" df="0" type="RT"><phrase>ANGLOPHONE AFRICA</phrase></thesaurus>
372 *
373 * Create a new ResultItemLink for display
374 *
375 * Return true if successful, otherwise false. */
376
377 boolean processThesaurusElement(Element t ) {
378
379 // why do we have num??? - not used anywhere
380 String num = "", id = "", tf = "", df = "", type = "", phrase = "";
381
382 id = t.getAttribute("id");
383 tf = t.getAttribute("tf");
384 df = t.getAttribute("df");
385 type = t.getAttribute("type");
386 num = t.getAttribute("num");
387
388 Node phrase_node = getChildByTagName(t, "phrase");
389 if (phrase_node !=null) {
390 phrase = getNodeText(phrase_node);
391 }
392 // Create a new ResultItem and add it to the display
393 ResultItemLink ri = new ResultItemLink(id, phrase, type, tf, df);
394
395 if (c.addResultItem(ri)) {
396 thesaurusLinksRetrieved++;
397 return true;
398 }
399
400 return false;
401 }
402
403 /** extracts the text out of a node */
404 protected String getNodeText(Node elem) {
405 elem.normalize();
406 Node n = elem.getFirstChild();
407 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
408 n=n.getNextSibling();
409 }
410 if (n==null) { // no text node
411 return "";
412 }
413 return n.getNodeValue();
414 }
415
416 /** returns the (first) child element with the given name */
417 protected Node getChildByTagName(Node n, String name) {
418
419 Node child = n.getFirstChild();
420 while (child!=null) {
421 if (child.getNodeName().equals(name)) {
422 return child;
423 }
424 child = child.getNextSibling();
425 }
426 return null; //not found
427 }
428
429
430}
431
432
433
434
Note: See TracBrowser for help on using the repository browser.