source: trunk/java-client/org/nzdl/gsdl/SimpleGraphicalClient/SearchPanel.java@ 2225

Last change on this file since 2225 was 2225, checked in by daven, 23 years ago

added QueryHistory, tooltips on collectionInfoButton. Altered
CSModel to retain CollectionInfo objects and longCollectionNames
for performance. Added files for a BerryBasket - to be turned on
soon. Added sorting of the QueryHistory via the TableMap and TableSorter
files - from the Java Swing tutorial - not sure about GPL status
of these 2.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/*
2 * SimpleGraphicalClient.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.nzdl.gsdl.SimpleGraphicalClient;
20
21import java.awt.*;
22import java.awt.event.*;
23import java.io.*;
24import java.io.IOException;
25import java.util.*;
26import javax.swing.*;
27import javax.swing.event.*;
28import javax.swing.text.*;
29import javax.swing.text.html.*;
30
31
32// local libraries
33import org.nzdl.gsdl.service.NzdlCollectionInfo;
34import org.nzdl.gsdl.service.NzdlQuery;
35import org.nzdl.gsdl.service.NzdlRequest;
36import org.nzdl.gsdl.service.NzdlResponse;
37import org.nzdl.gsdl.service.NzdlResultSet;
38import org.nzdl.gsdl.service.NzdlService;
39import org.nzdl.gsdl.service.NzdlServiceClient;
40import org.nzdl.gsdl.util.NzdlPreferences;
41
42
43/**
44 * A Class representing the Panel in which the Querying action happens.
45 *
46 * Does most of the actual `work' in the package.
47 *
48 * @author Dave Nichols ([email protected])
49 * @author stuart yeates ([email protected])
50 * @version $Revision: 2225 $
51 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.SimpleGraphicalClient
52 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.CSModel
53 * @see javax.swing.JPanel
54 */
55
56public class SearchPanel extends JPanel implements ActionListener, Constants
57{
58
59 CSModel csModel;
60 NzdlService nzdl;
61
62 JLabel collectionLabel;
63 JButton collectionInfoButton;
64 JTextField searchTextField;
65 JButton searchButton;
66 JComboBox collectionList;
67 JPanel queryFormulationPanel, resultsPanel, dataPanel;
68 JPanel collectionListPanel, searchTextFieldPanel, searchButtonPanel;
69 JList resultsList;
70 JScrollPane scrollResultsPane;
71 JScrollPane scrollDataPane;
72 JFrame windowParent;
73 //JEditorPane documentPane;
74 JTextPane documentPane; // make this a JTextComponent ??
75 JTextArea documentArea;
76 JTextComponent documentComponent;
77 HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
78 StyledEditorKit styledEditorKit = new StyledEditorKit();
79 GMLDocument htmlDoc = new GMLDocument();
80 DefaultStyledDocument defaultStyledDoc = new DefaultStyledDocument();
81 final static GMLDocument blankDoc = new GMLDocument();
82
83 /**
84 * Construct and initialise a new SearchPanel
85 */
86
87 public SearchPanel( CSModel newCsModel, JFrame parent)
88 {
89 super();
90 windowParent = parent;
91 csModel = newCsModel;
92 nzdl = csModel.getNzdlService();
93 setLayout( new BoxLayout(this, BoxLayout.Y_AXIS));
94 setBorder(BorderFactory.createTitledBorder("Search"));
95
96
97 searchTextField = new JTextField("Enter search terms here", 35) {
98 public boolean isRequestFocusEnabled(){
99 return true;
100 }
101 };
102 searchTextField.setText("Enter search terms here");
103 searchTextField.setFont(searchTextFieldFont);
104 searchTextField.setColumns(35);
105 searchTextField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
106 searchButton = new JButton("Search");
107
108 searchButton.addActionListener(this);
109 //searchButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
110 searchButton.setPreferredSize(new Dimension(80, 20));
111
112 collectionListPanel = new JPanel();
113 collectionListPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
114 collectionListPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
115 collectionLabel = new JLabel("Collection: ");
116
117 collectionList = new JComboBox(csModel.getCollectionList()){
118 public boolean isFocusTraversable() {
119 return false;
120 }
121 };
122 collectionList.setEditable(false);
123
124 collectionInfoButton = new JButton("Info...") {
125 public String getToolTipText(MouseEvent e) {
126 //return "more information on " + collectionList.getSelectedItem();
127 return "More information on " + csModel.getLongCollectionName((String)collectionList.getSelectedItem());
128 }
129 };
130 ToolTipManager.sharedInstance().registerComponent(collectionInfoButton);
131 collectionInfoButton.addActionListener(this);
132
133 collectionListPanel.add(collectionLabel);
134 collectionListPanel.add(collectionList);
135 collectionListPanel.add(collectionInfoButton);
136
137 searchTextFieldPanel = new JPanel();
138 searchTextFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
139 searchTextFieldPanel.add(searchTextField);
140
141 searchButtonPanel = new JPanel();
142 searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
143 searchButtonPanel.add(searchButton);
144
145 queryFormulationPanel = new JPanel();
146 queryFormulationPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
147 queryFormulationPanel.setLayout(new BoxLayout(queryFormulationPanel, BoxLayout.Y_AXIS));
148 queryFormulationPanel.add(Box.createHorizontalStrut(100));
149 queryFormulationPanel.add(collectionListPanel);
150 queryFormulationPanel.add(searchTextFieldPanel);
151 queryFormulationPanel.add(searchButtonPanel);
152
153 resultsPanel = new JPanel();
154 resultsPanel.setLayout(new BorderLayout());
155 resultsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
156 ResultModel resultListModel = csModel.getResultsModel();
157 resultsList = new JList(resultListModel);
158 resultsList.setFont(resultsFont);
159 resultsList.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
160 resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
161 scrollResultsPane = new JScrollPane(resultsList);
162 resultsPanel.add(scrollResultsPane, BorderLayout.CENTER);
163 csModel.addResult(Result.INITIAL_FAKE_RESULT);
164
165 dataPanel = new JPanel();
166 dataPanel.setLayout(new BorderLayout());
167 dataPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
168 documentPane = new JTextPane() { // to control scrolling for raw text docs
169 public boolean getScrollableTracksViewportWidth() {
170 if (getSize().width < getParent().getSize().width)
171 return true;
172 else return false;
173 }
174 public void setSize(Dimension d) {
175 if (d.width < getParent().getSize().width)
176 d.width = getParent().getSize().width;
177 super.setSize(d);
178 }
179 }; // end custom JTextPane
180
181 documentPane.setEditable(false);
182 documentPane.setFont(docFont);
183 documentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
184 //documentPane.setPreferredSize(new Dimension(100, 100));
185
186 scrollDataPane = new JScrollPane(documentPane);
187 scrollDataPane.setPreferredSize(new Dimension(300, 300));
188 dataPanel.add(scrollDataPane, BorderLayout.CENTER);
189
190 parent.getRootPane().setDefaultButton(searchButton);
191
192 resultsList.addListSelectionListener(new ResultListSelectionHandler() );
193
194 add(Box.createHorizontalStrut(400));
195 add(queryFormulationPanel);
196 add(resultsPanel);
197 add(dataPanel);
198 searchTextField.requestFocus();
199 }
200
201 /** respond to the user pressing the Search button */
202
203 public void actionPerformed(ActionEvent e) {
204 String collectionName = collectionList.getSelectedItem().toString();
205 if (e.getSource() == searchButton ) {
206 // cursor not set when RETURN pressed, only when button clicked ?
207 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
208 String queryString = searchTextField.getText();
209 System.err.println("Search started (" + collectionName + "): \"" + queryString + "\" ..." );
210 //send query to collection
211 NzdlQuery nzdlQuery = new NzdlQuery(queryString);
212 // return the first numResults that match
213 //query.setEndResults( 15 );
214 // "-1" means consider all the documents that match
215 nzdlQuery.setMaxDocs( -1 );
216 NzdlRequest request = new NzdlRequest( nzdlQuery );
217 NzdlResponse response = new NzdlResponse();
218
219 nzdl.service( collectionName, request, response );
220 NzdlResultSet results = response.getResultSet();
221 ArrayList docIDs = new ArrayList(results.getDocumentIDs());
222 // System.err.println("Size of docIDs = " + docIDs.size());
223 // paired collection of docIDs and Titles - but as Sets!
224 Map titleMetaData = nzdl.getMetaData( collectionName, docIDs, "Title" );
225 // created a paired collection of docIDs and Titles - as simple Strings!
226 HashMap titleMap = new HashMap();
227 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
228 String docID = (String) i.next();
229 //Set titleSet = (Set) titleMetaData.get( docID );
230 ArrayList titleList = new ArrayList((Set)titleMetaData.get(docID));
231 // use the first title as *the* title
232 titleMap.put(docID, titleList.get(0));
233 } // end for
234 // update the results list
235 csModel.clearResults();
236
237 if (docIDs.size() == 0 ) { // give some feedback for 0 results
238 csModel.addResult(Result.FAKE_RESULT);
239 documentPane.setStyledDocument(blankDoc);
240 }
241 else { // there are some results
242 Iterator docIDsIterator = docIDs.iterator();
243 while (docIDsIterator.hasNext()) {
244 String resultDocID = (String) docIDsIterator.next();
245 csModel.addResult(new Result((String)titleMap.get(resultDocID), resultDocID, collectionName ));
246 } //end while
247 if (NzdlPreferences.getInstance().getBoolean(Constants.DISPLAY_FIRST_DOC))
248 resultsList.setSelectedIndex(0); // should be under Preference control!
249 } // end else
250
251
252 csModel.getQueryHistoryModel().add( new QueryHistoryItem(new Query(queryString), new Date(), collectionName, "user"));
253
254 titleMap.clear();
255 titleMetaData.clear();
256 docIDs.clear();
257 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
258 } // end if
259 else {
260 if (e.getSource() == collectionInfoButton) {
261 CollectionInfoDialog cid = new CollectionInfoDialog(windowParent, collectionName, csModel);
262 }
263 else {
264 System.err.println("unknown Action event in SearchPanel");
265 }
266 }
267 } //end actionPerformed
268
269 /** handles selection events in the results list so that when the
270 user selects a new title (say) the document contents is
271 automatically displayed in the main panel */
272
273 class ResultListSelectionHandler implements ListSelectionListener {
274
275 // private Result previousSelection = null;
276
277 public void valueChanged(ListSelectionEvent e) {
278 // get the result object from the SelectedIndex
279 // assume we only get here with events from resultsList
280 int docLength = 0;
281 if (e.getValueIsAdjusting()) { return; }
282 Result result = (Result) resultsList.getSelectedValue();
283 if ((result == Result.FAKE_RESULT) || (result == Result.INITIAL_FAKE_RESULT)) {
284 documentPane.setStyledDocument( blankDoc );
285 System.err.println("clicked on fake result");
286 }
287 else { // a real result representing a real document to deal with
288
289 windowParent.setCursor(WAIT_CURSOR);
290
291 deleteDocContents(); // for memory/garbage collection
292
293 StringReader sr = new StringReader(nzdl.getDocument(result.getCollectionName(), result.getDocID()));
294
295 if (NzdlPreferences.getInstance().getBoolean(Constants.RAW_TEXT))
296 // display the document as raw text
297 displayAsRawText(sr);
298 else {
299 // treat as HTML
300 displayAsHTML(sr);
301 //htmlDoc.getImageData(nzdl, result.getCollectionName());
302 }
303
304 windowParent.setCursor(NORMAL_CURSOR);
305 }
306 } //end valueChanged
307 }//end ResultListSelectionHandler
308
309
310
311 private void deleteDocContents() {
312 //trash the old htmlDoc for memory/garbage collection
313 try {
314 if (htmlDoc != null)
315 htmlDoc.remove(0, htmlDoc.getLength());
316 if (defaultStyledDoc != null)
317 defaultStyledDoc.remove(0, defaultStyledDoc.getLength());
318 }
319 catch (Exception exception) {
320 throw new Error (exception.toString());
321 }
322 } // end deleteDocContents
323
324
325 public void switchToRawText() {
326 windowParent.setCursor(WAIT_CURSOR);
327 System.err.println("switching to raw text");
328 displayAsRawText(new StringReader(documentPane.getText()));
329 windowParent.setCursor(NORMAL_CURSOR);
330 }
331
332 public void displayAsRawText(StringReader sr) {
333 defaultStyledDoc = new DefaultStyledDocument();
334 documentPane.setEditorKit(styledEditorKit);
335 try {
336 styledEditorKit.read( sr, defaultStyledDoc, 0);
337 }
338 catch (Exception e) {
339 System.err.println(e);
340 }
341 documentPane.setDocument(defaultStyledDoc);
342 setCaretToStart(defaultStyledDoc.getLength());
343 } // end displayAsRawText
344
345
346 public void switchToHTML() {
347 windowParent.setCursor(WAIT_CURSOR);
348 System.err.println("switching to HTML");
349 displayAsHTML(new StringReader(documentPane.getText()));
350 windowParent.setCursor(NORMAL_CURSOR);
351 }
352
353 public void displayAsHTML(StringReader sr) {
354 htmlDoc = new GMLDocument();
355 documentPane.setEditorKit(htmlEditorKit);
356 try {
357 htmlEditorKit.read( sr, htmlDoc, 0);
358 }
359 catch (Exception e) {
360 System.err.println(e);
361 }
362 documentPane.setStyledDocument( htmlDoc );
363 setCaretToStart(htmlDoc.getLength());
364 } // end displayAsHTML
365
366
367 private void setCaretToStart(int docLength) {
368 // it seems as if they haven't considered
369 // documents with > MAX_INT num of chars ...
370 if (docLength > 0)
371 documentPane.setCaretPosition(1);
372 }
373
374
375 } //end SearchPanel
Note: See TracBrowser for help on using the repository browser.